from __future__ import annotations def build_search_response(raw,took_ms,page,size,config): h=raw["hits"]; total=h["total"]["value"]; aggs=raw.get("aggregations",{}) res=[] for hit in h["hits"]: s=dict(hit["_source"]) for k,frags in hit.get("highlight",{}).items(): s[k]=" … ".join(frags) s["_id"]=hit["_id"]; s["_score"]=round(hit.get("_score") or 0,4); res.append(s) facets={k:[{"valor":str(b.get("key",b.get("key_as_string",""))),"total":b["doc_count"]} for b in v.get("buckets",[])] for k,v in aggs.items()} return {"total":total,"pagina":page,"tamanho":size,"took_ms":took_ms,"resultados":res,"facets":facets or None}