Skip to content

Commit

Permalink
C4-301 fix nested stats aggs
Browse files Browse the repository at this point in the history
  • Loading branch information
willronchetti committed Sep 30, 2020
1 parent f131284 commit ed51aad
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
41 changes: 31 additions & 10 deletions src/encoded/search/lucene_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ def fix_nested_aggregations(search, es_mapping):
"""
aggs_ptr = search.aggs['all_items']
for agg in aggs_ptr:
if NESTED in agg:
if NESTED in agg and 'stats' not in agg: # stats aggs are already correct
(search.aggs['all_items'][agg] # create a sub-bucket, preserving the boolean qualifiers
.bucket('primary_agg',
'nested', path=find_nested_path(aggs_ptr.aggs[agg]['primary_agg'].field, es_mapping))
Expand Down Expand Up @@ -778,17 +778,38 @@ def build_facets(cls, search, facets, search_filters, string_query, request, doc
facet["number_step"] = "any"
facet_filters = cls.generate_filters_for_terms_agg_from_search_filters(query_field, search_filters,
string_query)
# stats aggregations could be nested too
if nested_path:
facet['aggregation_type'] = 'nested:stats'
aggs[facet['aggregation_type'] + ':' + agg_name] = {
AGGS: {
'primary_agg': {
NESTED: {
PATH: nested_path
},
AGGS: {
'primary_agg': {
'stats': {
'field': query_field
}
}
}
}
},
FILTER: {BOOL: facet_filters}
}

aggs[facet['aggregation_type'] + ":" + agg_name] = {
AGGS: {
'primary_agg': {
'stats': {
'field': query_field
else:
aggs[facet['aggregation_type'] + ":" + agg_name] = {
AGGS: {
'primary_agg': {
'stats': {
'field': query_field
}
}
}
},
FILTER: {BOOL: facet_filters}
}
},
FILTER: {BOOL: facet_filters}
}

else:
if nested_path:
Expand Down
14 changes: 13 additions & 1 deletion src/encoded/search/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,13 @@ def format_facets(self, es_results):
# Used for fields on which can do range filter on, to provide min + max bounds
for k in aggregations[full_agg_name]['primary_agg'].keys():
result_facet[k] = aggregations[full_agg_name]['primary_agg'][k]

# nested stats aggregations have a second "layer" for reverse_nested
elif facet['aggregation_type'] == 'nested:stats':
result_facet['total'] = aggregations[full_agg_name]['primary_agg']['doc_count']
for k in aggregations[full_agg_name]['primary_agg']['primary_agg'].keys():
result_facet[k] = aggregations[full_agg_name]['primary_agg']['primary_agg'][k]

else: # 'terms' assumed.

# Shift the bucket location
Expand Down Expand Up @@ -831,10 +838,15 @@ def format_facets(self, es_results):

if len(aggregations[full_agg_name].keys()) > 2:
result_facet['extra_aggs'] = {k: v for k, v in aggregations[full_agg_name].items() if
k not in ('doc_count', "primary_agg")}
k not in ('doc_count', 'primary_agg')}

result.append(result_facet)

# TODO ALEX: Client will reject 'nested:stats' so overwritten here
for facet in result:
for k, v in facet.items():
if k == 'aggregation_type' and v == 'nested:stats':
facet[k] = 'stats'
return result

@staticmethod
Expand Down

0 comments on commit ed51aad

Please sign in to comment.