view scobot/index/access.py @ 13:403eff4a16c8

fix up indexer flow and fastapi server
author drewp@bigasterisk.com
date Thu, 11 Jul 2024 21:32:24 -0700
parents 6622bacb0b84
children 6ed25bcaaf1f
line wrap: on
line source

from pathlib import Path
import shutil

from whoosh.index import create_in, open_dir

from scobot.index.schema import schema

log = None  # set by flow


class SearchIndex:

    def __init__(self, indexDir: Path, delete_existing=True):
        if delete_existing:
            shutil.rmtree(indexDir)
            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()=}')