from __future__ import annotations import time from .query_builder import build_search_query,build_autocomplete_query async def search(es,config,q,filters=None,page=1,size=10,com_facets=True): body=build_search_query(q,filters,page,size,com_facets,config); start=time.monotonic() resp=await es.search(index=config.es_index,body=body) return {"raw":resp,"took_ms":int((time.monotonic()-start)*1000)} async def autocomplete(es,config,q,size=8): resp=await es.search(index=config.es_index,body=build_autocomplete_query(q,size,config)) seen=set(); r=[] for hit in resp["hits"]["hits"]: for f in config.suggest_fields: val=hit["_source"].get(f) if val and val not in seen: seen.add(val); r.append(val) return r[:size] async def get_doc(es,config,doc_id): try: return (await es.get(index=config.es_index,id=doc_id))["_source"] except: return None async def get_stats(es,config): c=await es.count(index=config.es_index); h=await es.cluster.health() return {"index":config.es_index,"total_docs":c.get("count",0),"es_status":h.get("status","unknown")}