1
votes

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?

1

1 Answers

2
votes

Elasticsearch terms aggs work in many ways similar to a SQL Group by, so you could for instance do multiple levels of aggs, ex. ASIN -> name -> referencenumber. This will also cover cases where you have different names with same ASIN etc.

{
    "aggs": {
       "AsinAgg": {
          "terms": {
              "field": "ASIN", "size":0
          },
          "aggs": {
              "nameAgg": {
                 "terms": {
                      "field": "name", "size": 0
                  },
                  "aggs" {.....}
              }
          }
       }
    }
}

Another way can be to use the top_hits within an agg and return the top 1 hit. If you are sure they have the same name and referenceNumber, you should be ok with just getting a single top hit. I would not recommend doing this even though it could work in specific cases. Read up on top_hits here: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html