Mercurial > code > home > repos > sco-bot
view scobot/index/access.py @ 16:7a87ba2f00d9
reformat, fix some types, make more async
author | drewp@bigasterisk.com |
---|---|
date | Fri, 19 Jul 2024 00:49:38 -0700 |
parents | 6ed25bcaaf1f |
children |
line wrap: on
line source
import logging from pathlib import Path import shutil from typing import cast from whoosh.index import create_in, open_dir from scobot.index.schema import schema log = cast(logging.Logger, None) # set by flow class SearchIndex: def __init__(self, indexDir: Path, delete_existing=True): if delete_existing: shutil.rmtree(indexDir, ignore_errors=True) indexDir.mkdir(parents=True, exist_ok=True) self.ix = create_in(indexDir, schema) else: self.ix = open_dir(indexDir) self.writer = self.ix.writer() def addDoc(self, **kw): self.writer.add_document(**kw) def commit(self): self.writer.commit() with self.ix.searcher() as searcher: log.info(f'index doc count = {searcher.doc_count()}') class SearchIndexRO: def __init__(self, indexDir: Path): self.ix = open_dir(indexDir, readonly=True) self.searcher = self.ix.searcher() print(f'{self.searcher.doc_count()=}')