ElasticSearch average aggregation in results -
ElasticSearch average aggregation in results -
i want average cost in top 10 sale products, next query:
{ "aggs": { "top_sale_avg_price": { "avg": {"field": "price"}, "aggs": { "top_sale_hits": { "top_hits": { "sort": [{"buy_count": {"order": "desc"}}], "size": 10 } } } } } }
then error, says "aggregationinitializationexception[aggregator [top_sale_avg_price] of type [avg] cannot take sub-aggregations]"
top hits find 10 documents best match query (i.e. score highest). sort performed on result set - top hits won't work you.
you can't nest top_hits
, avg
within each other - can nest within bucket (e.g. terms, histogram, range, etc.)
to meet requirements, run 2 queries. first find matching documents (top 10 sale products):
{ "fields" : ["_id"], "sort": [ { "buy_count": { "order": "desc" }} ], "size":10, "query": { "match_all": {} } }'
the sec query average sale cost - you'll need populate ids.
{ "filter" : { "ids": { "values": [ "07cfbc09-360b-10d8-e053-28ab48a5c296", "07cf438e-7830-19ec-e053-28ab48a59c0b"]} }, "aggs" : { "top_sale_avg_price" : { "avg": {"field" : "price" } } } }'
elasticsearch
Comments
Post a Comment