Return both aggregation AND field value in ES

Let’s say that each document in my ES index has following fields:

ASIN, name, referenceNumber, videoViews, timeWatched

There can be many documents with the same ASIN fields.

I’m doing teams aggregation, that aggregate documents in my ES cluster based on ASIN field.
This Aggregation counts sum of videoViews and sum of timeWatched for each ASIN bucket.

ElasticSearch returns aggregation response where ASIN - a key for each bucket, and sum of timeWatched and sum ofvideoViews are values for this key.

My simple question is how to make aggregation return name and referenceNumber as well? If I know that documents with same ASIN will have same name and referenceNumber?

If you know all documents with the same ASIN also have the same name and referenceNumber the easiest solution would be to use a top_hits agg inside the terms agg: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html

Set the size to 1, which returns the "first" document. Since all ASIN/name/referenceNumber's correspond to each other, you don't have to worry about sorting. That'll give you the complete document, which you can grab any fields that you want from.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.