# HG changeset patch # User drewp@bigasterisk.com # Date 1721375985 25200 # Node ID 0d72635fc5019686d980635daa2794856bbd95af # Parent 7a87ba2f00d9308b8aa8a9175b9788fe73df821e reloadIndexIfChanged diff -r 7a87ba2f00d9 -r 0d72635fc501 scobot/service/query.py --- a/scobot/service/query.py Fri Jul 19 00:49:38 2024 -0700 +++ b/scobot/service/query.py Fri Jul 19 00:59:45 2024 -0700 @@ -45,24 +45,37 @@ # rebuild(client, embedding_fn, dim=embedding_fn.dim) # search(q, embedding_fn, client) +indexPath = Path('data/build/index0') + @asynccontextmanager async def lifespan(app: FastAPI): - app.state.index = SearchIndexRO(Path('data/build/index0')) + reloadIndexIfChanged(app) yield + app = FastAPI(lifespan=lifespan) + +def reloadIndexIfChanged(app: FastAPI): + if ((not hasattr(app.state, 'indexMtime')) + or (app.state.indexMtime != indexPath.stat().st_mtime)): + print('reloading index') + app.state.indexMtime = indexPath.stat().st_mtime + app.state.index = SearchIndexRO(indexPath) + + @app.get("/sco/query") def read_query1(q: str): + reloadIndexIfChanged(app) index = app.state.index query = QueryParser("phrase", index.ix.schema).parse(q) pprint(query) results = index.searcher.search(query) - docs=[] + docs = [] for res in results: - doc=dict(res) + doc = dict(res) doc['snippetHtml'] = html.escape(doc['phrase']) docs.append(doc) return {"results": docs}