@@ -67,7 +67,7 @@ def call(query:, lookahead:, args:, field:, context:)
6767 aggregations_node ,
6868 field : field ,
6969 context : context ,
70- index_definitions : query . initial_search_index_definitions ,
70+ grouping_missing_value_placeholders_by_index_leaf_path : field . type . unwrap_fully . grouping_missing_value_placeholders_by_index_leaf_path ,
7171 grouping_adapter : CompositeGroupingAdapter ,
7272 # Filters on root aggregations applied to the search query body itself instead of
7373 # using a filter aggregation, like sub-aggregations do, so we don't want a filter
@@ -100,7 +100,7 @@ def extract_aggregation_node(lookahead, field, graphql_query)
100100 )
101101 end
102102
103- def build_aggregation_query_for ( aggregations_node , field :, context :, index_definitions :, grouping_adapter :, nested_path : [ ] , unfiltered : false )
103+ def build_aggregation_query_for ( aggregations_node , field :, context :, grouping_missing_value_placeholders_by_index_leaf_path :, grouping_adapter :, nested_path : [ ] , unfiltered : false )
104104 aggregation_name = name_of ( _ = aggregations_node . ast_nodes . first )
105105
106106 # Get the AST node for the `nodes` subfield (e.g. from `fooAggregations { nodes { ... } }`)
@@ -134,9 +134,9 @@ def build_aggregation_query_for(aggregations_node, field:, context:, index_defin
134134
135135 Query . new (
136136 name : aggregation_name ,
137- groupings : build_groupings_from ( node_node , aggregation_name , index_definitions : index_definitions , from_field_path : nested_path ) ,
137+ groupings : build_groupings_from ( node_node , aggregation_name , grouping_missing_value_placeholders_by_index_leaf_path : grouping_missing_value_placeholders_by_index_leaf_path , from_field_path : nested_path ) ,
138138 computations : build_computations_from ( node_node , context : context , from_field_path : nested_path ) ,
139- sub_aggregations : build_sub_aggregations_from ( node_node , context : context , index_definitions : index_definitions , parent_nested_path : nested_path ) ,
139+ sub_aggregations : build_sub_aggregations_from ( node_node , context : context , grouping_missing_value_placeholders_by_index_leaf_path : grouping_missing_value_placeholders_by_index_leaf_path , parent_nested_path : nested_path ) ,
140140 needs_doc_count : count_detail_node . selected? || node_node . selects? ( element_names . count ) ,
141141 needs_doc_count_error : needs_doc_count_error ,
142142 paginator : build_paginator_for ( aggregations_node ) ,
@@ -215,7 +215,7 @@ def computation_for(fn_node, field_path, context:)
215215 )
216216 end
217217
218- def build_groupings_from ( node_node , aggregation_name , index_definitions :, from_field_path : [ ] )
218+ def build_groupings_from ( node_node , aggregation_name , grouping_missing_value_placeholders_by_index_leaf_path :, from_field_path : [ ] )
219219 grouped_by_node = selection_above_grouping_fields ( node_node , element_names . grouped_by , aggregation_name )
220220
221221 build_clauses_from ( grouped_by_node ) do |node , field , field_path |
@@ -230,19 +230,15 @@ def build_groupings_from(node_node, aggregation_name, index_definitions:, from_f
230230 # Non-date/time grouping
231231 missing_value_placeholder = field . type . unwrap_fully . grouping_missing_value_placeholder
232232 [ FieldTermGrouping . new ( field_path : field_path , missing_value_placeholder : missing_value_placeholder ) ]
233- elsif indexed_as_leaf_field? ( field_path , index_definitions )
233+ elsif grouping_missing_value_placeholders_by_index_leaf_path . key? ( index_path = FieldPathEncoder . join ( field_path . filter_map ( & :name_in_index ) ) )
234234 # A custom resolver can expose a single indexed value as an object. We group on that value
235235 # rather than on the selected subfields, which have no values of their own in the index.
236- [ FieldTermGrouping . new ( field_path : field_path , missing_value_placeholder : nil ) ]
236+ missing_value_placeholder = grouping_missing_value_placeholders_by_index_leaf_path [ index_path ]
237+ [ FieldTermGrouping . new ( field_path : field_path , missing_value_placeholder : missing_value_placeholder ) ]
237238 end
238239 end
239240 end
240241
241- def indexed_as_leaf_field? ( field_path , index_definitions )
242- path = FieldPathEncoder . join ( field_path . filter_map ( &:name_in_index ) )
243- index_definitions . any? { |index_def | index_def . fields_by_path . key? ( path ) }
244- end
245-
246242 # Builds a grouping for each selected `as_geo_tile` operation, validating its zoom level
247243 # without treating the selected tile properties as additional grouping dimensions.
248244 def geo_tile_groupings_from ( field_path :, node :)
@@ -342,7 +338,7 @@ def name_of(ast_node)
342338 ast_node . alias || ast_node . name
343339 end
344340
345- def build_sub_aggregations_from ( node_node , context :, index_definitions :, parent_nested_path : [ ] )
341+ def build_sub_aggregations_from ( node_node , context :, grouping_missing_value_placeholders_by_index_leaf_path :, parent_nested_path : [ ] )
346342 key_sub_agg_pairs =
347343 build_clauses_from ( node_node . selection ( element_names . sub_aggregations ) ) do |node , field , field_path |
348344 if field . type . elasticgraph_category == :nested_sub_aggregation_connection
@@ -353,7 +349,7 @@ def build_sub_aggregations_from(node_node, context:, index_definitions:, parent_
353349 node ,
354350 field : field ,
355351 context : context ,
356- index_definitions : index_definitions ,
352+ grouping_missing_value_placeholders_by_index_leaf_path : grouping_missing_value_placeholders_by_index_leaf_path ,
357353 grouping_adapter : sub_aggregation_grouping_adapter ,
358354 nested_path : nested_path
359355 )
0 commit comments