@@ -41,12 +41,24 @@ def _hit_to_row(hit: Mapping[str, Any]) -> Mapping[str, Any]:
4141 return row
4242
4343
44- def _search_response_to_documents (response : Mapping [str , Any ]) -> list [Mapping [str , Any ]]:
45- return [_hit_to_row (hit ) for hit in response .get ("hits" , {}).get ("hits" , [])]
46-
47-
48- def _search_response_to_df (response : Mapping [str , Any ] | Any ) -> pd .DataFrame :
49- return pd .DataFrame (_search_response_to_documents (response ))
44+ def _search_response_to_documents (
45+ response : Mapping [str , Any ], aggregations : list [str ] | None = None
46+ ) -> list [Mapping [str , Any ]]:
47+ hits = response .get ("hits" , {}).get ("hits" , [])
48+ if not hits and aggregations :
49+ hits = [
50+ aggregation_hit
51+ for aggregation_name in aggregations
52+ for aggregation_hit in response .get ("aggregations" , {})
53+ .get (aggregation_name , {})
54+ .get ("hits" , {})
55+ .get ("hits" , [])
56+ ]
57+ return [_hit_to_row (hit ) for hit in hits ]
58+
59+
60+ def _search_response_to_df (response : Mapping [str , Any ] | Any , aggregations : list [str ] | None = None ) -> pd .DataFrame :
61+ return pd .DataFrame (_search_response_to_documents (response = response , aggregations = aggregations ))
5062
5163
5264@_utils .check_optional_dependency (opensearchpy , "opensearchpy" )
@@ -128,8 +140,16 @@ def search(
128140 documents = [_hit_to_row (doc ) for doc in documents_generator ]
129141 df = pd .DataFrame (documents )
130142 else :
143+ aggregations = (
144+ list (search_body .get ("aggregations" , {}).keys () or search_body .get ("aggs" , {}).keys ())
145+ if search_body
146+ else None
147+ )
131148 response = client .search (index = index , body = search_body , filter_path = filter_path , ** kwargs )
132- df = _search_response_to_df (response )
149+ df = _search_response_to_df (
150+ response = response ,
151+ aggregations = aggregations ,
152+ )
133153 return df
134154
135155
0 commit comments