@@ -41,12 +41,24 @@ def _hit_to_row(hit: Mapping[str, Any]) -> Mapping[str, Any]:
41
41
return row
42
42
43
43
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
+ dict (aggregation_hit , _aggregation_name = aggregation_name )
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 ))
50
62
51
63
52
64
@_utils .check_optional_dependency (opensearchpy , "opensearchpy" )
@@ -128,8 +140,16 @@ def search(
128
140
documents = [_hit_to_row (doc ) for doc in documents_generator ]
129
141
df = pd .DataFrame (documents )
130
142
else :
143
+ aggregations = (
144
+ list (search_body .get ("aggregations" , {}).keys () or search_body .get ("aggs" , {}).keys ())
145
+ if search_body
146
+ else None
147
+ )
131
148
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
+ )
133
153
return df
134
154
135
155
0 commit comments