Elasticsearch aggregations, get additional field in bucket

(note: the same query is asked on SO: http://stackoverflow.com/questions/30590729/elasticsearch-aggregations-get-additional-field-in-bucket)

I query ES index to filter results and get aggregations by selected terms. A sample query is like this:

GET buyer_requests/vehicle_requests/_search
{
  "query": {
    "filtered": {
        "filter": {
          "and": [
            {
              "terms": {
                "vehicle.make.raw": [
                  "Audi",
                  "BMW",
                  "Chevrolet"
                ]
              }
            },
            {
              "range": {
                "style.price": {
                  "gte": 15000,
                  "lte": 20000
                }
              }
            },
            {
              "geo_distance": {
                "distance": "20000km",
                "info.pin": {
                  "lat": 42,
                  "lon": 21
                }
              }
            }
          ]
        }
    }
  }, 
  "aggs": {
    "makes": {
      "filter": {
        "range": {
          "style.price": {
            "gte": 5000,
            "lte": 40000
          }
        }
      },
      "aggs": {
        "makes": {
          "terms": {
            "field": "vehicle.make.raw",
            "order": {
              "_term": "asc"
            }
          }
        }
      }
    },
    "model": {
      "filter": {
        "and": [
          {
            "terms": {
              "vehicle.make.raw": [
                "Audi",
                "BMW",
                "Chevrolet"
              ]
            }
          }
        ]
      },
      "aggs": {
        "models": {
          "terms": {
            "field": "vehicle.model.raw",
            "size": 10,
            "order": {
              "_term": "asc"
            }
          }
        }
      }
    }
  }
}

The result I get is something like:

How can I get in "buckets" section on "models" terms another field from result set. I want to get reference to Makes so the result would look like this:

"model": {
   "doc_count": 7,
   "models": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
         {
            "key": "3 Series",
            "make": "bmw",                  <----------- this key
            "doc_count": 3
         },
         {
            "key": "4 Series",
            "make": "bmw",                  <----------- this key
            "doc_count": 4
         },
         {
           "key": "Camaro",
           "make": "chevrolet",             <----------- this key
           "doc_count": 2
         }
      ]
   }
}

Thank you very much.
Petar.

You cannot do this with aggregations because there is no guarantee that all the documents in a models bucket will have the same make. Instead you could nest another terms aggregation inside the models aggregation for the make field.

I just noticed you already have an aggregation for makes, so why not nest your models aggregation inside that? Although this will slightly change what you are asking for from 'the top 10 models' to `the top 10 makes and each of their top 10 models', so you may want to stick with the original suggestion, depending on your use-case

1 Like