Mercurial > code > home > repos > sco-bot
comparison scobot/service/query.py @ 14:b9c2b7fedbcd
fix up deployment and connect ui to server again
author | drewp@bigasterisk.com |
---|---|
date | Thu, 11 Jul 2024 22:11:41 -0700 |
parents | 403eff4a16c8 |
children | 6ed25bcaaf1f |
comparison
equal
deleted
inserted
replaced
13:403eff4a16c8 | 14:b9c2b7fedbcd |
---|---|
1 import html | |
1 from scobot.index.access import SearchIndexRO | 2 from scobot.index.access import SearchIndexRO |
2 from whoosh.qparser import QueryParser | 3 from whoosh.qparser import QueryParser |
3 import json | 4 import json |
4 from pathlib import Path | 5 from pathlib import Path |
5 from pprint import pprint | 6 from pprint import pprint |
45 # search(q, embedding_fn, client) | 46 # search(q, embedding_fn, client) |
46 | 47 |
47 | 48 |
48 @asynccontextmanager | 49 @asynccontextmanager |
49 async def lifespan(app: FastAPI): | 50 async def lifespan(app: FastAPI): |
50 app.state.index = SearchIndexRO('/tmp/scoindex') | 51 app.state.index = SearchIndexRO('data/index') |
51 yield | 52 yield |
52 | 53 |
53 app = FastAPI(lifespan=lifespan) | 54 app = FastAPI(lifespan=lifespan) |
54 | 55 |
55 @app.get("/sco/query") | 56 @app.get("/sco/query") |
56 def read_query1(q: str): | 57 def read_query1(q: str): |
57 index = app.state.index | 58 index = app.state.index |
58 | 59 |
59 query = QueryParser("phrase", index.ix.schema).parse(q) | 60 query = QueryParser("phrase", index.ix.schema).parse(q) |
60 pprint(query) | 61 pprint(query) |
61 results = list(index.searcher.search(query)) | 62 results = index.searcher.search(query) |
62 return {"results": results} | 63 docs=[] |
64 for res in results: | |
65 doc=dict(res) | |
66 doc['snippetHtml'] = html.escape(doc['phrase']) | |
67 docs.append(doc) | |
68 return {"results": docs} |