Commit a3a0b024 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Merge branch 'v1-aggregation-counts' into 'v1.0.0'

Fixed incorrect materials count on terms aggregations. #609

See merge request !455
parents 4981d37b 0ff90e91
Pipeline #115293 passed with stages
in 33 minutes and 20 seconds
......@@ -1119,6 +1119,10 @@ class Bucket(BaseModel):
None, description=strip('''The entries that were requested for each value.'''))
count: int = Field(
None, description=strip('''The amount of entries with this value.'''))
nested_count: int = Field(
None, description=strip('''
The amount of nested entries with this values. Is the same as count for
aggregations on non nested quantities.'''))
metrics: Optional[Dict[str, int]]
value: Union[float, str]
......
......@@ -653,11 +653,13 @@ def _api_to_es_aggregation(
agg = cast(QuantityAggregation, agg)
longest_nested_key = None
is_nested = False
quantity = validate_quantity(agg.quantity, doc_type=doc_type, loc=['aggregation', 'quantity'])
for nested_key in doc_type.nested_object_keys:
if agg.quantity.startswith(nested_key):
es_aggs = es_aggs.bucket('nested_agg:%s' % name, 'nested', path=nested_key)
longest_nested_key = nested_key
is_nested = True
es_agg = None
......@@ -752,6 +754,9 @@ def _api_to_es_aggregation(
es_agg.metric('entries', A('top_hits', size=agg.entries.size, **kwargs))
if is_nested:
es_agg.bucket(f'agg:parents:{name}', A('reverse_nested'))
elif isinstance(agg, DateHistogramAggregation):
if not quantity.annotation.mapping['type'] in ['date']:
raise QueryValidationError(
......@@ -845,7 +850,11 @@ def _es_to_api_aggregation(
else:
value = es_bucket.key[quantity.search_field]
count = es_bucket.doc_count
nested_count = es_bucket.doc_count
if f'agg:parents:{name}' in es_bucket:
count = es_bucket[f'agg:parents:{name}'].doc_count
else:
count = nested_count
metrics = {}
for metric in agg.metrics: # type: ignore
metrics[metric] = es_bucket['metric:' + metric].value
......@@ -860,7 +869,9 @@ def _es_to_api_aggregation(
values.add(value)
if len(metrics) == 0:
metrics = None
return Bucket(value=value, entries=entries, count=count, metrics=metrics)
return Bucket(
value=value, entries=entries, count=count, nested_count=nested_count,
metrics=metrics)
data = [get_bucket(es_bucket) for es_bucket in es_agg.buckets]
......
......@@ -73,6 +73,11 @@ def test_materials_aggregations(client, data, test_user_auth, aggregation, total
response_json, 'test_agg_name', aggregation_obj, total=total, size=size,
default_key='material_id')
# make sure that (nested) terms aggregation produce sensible counts
if 'terms' in aggregation:
for bucket in response_json['aggregations']['test_agg_name']['terms']['data']:
assert bucket['count'] <= 6 # the total number of materials, counting entries we would surpass this
@pytest.mark.parametrize(
'query,aggs,agg_lengths,total,status_code',
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment