# Elasticsearch aggregations, get additional field in bucket

**URL:** <https://discuss.elastic.co/t/elasticsearch-aggregations-get-additional-field-in-bucket/1689>\
**Category:** Elasticsearch\
**Created:** [June 2, 2015, 8:22am UTC](https://discuss.elastic.co/t/elasticsearch-aggregations-get-additional-field-in-bucket/1689 "2015-06-02T08:22:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Petar\_Pavlovic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/petar_pavlovic/32/465_2.png) [@Petar\_Pavlovic](https://discuss.elastic.co/u/Petar_Pavlovic)\
**Post date:** [June 2, 2015, 8:22am UTC](https://discuss.elastic.co/t/elasticsearch-aggregations-get-additional-field-in-bucket/1689/1 "2015-06-02T08:22:20Z")

</div>

(note: the same query is asked on SO: [http://stackoverflow.com/questions/30590729/elasticsearch-aggregations-get-additional-field-in-bucket](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:

![](https://sea2.discourse-cdn.com/elastic/uploads/default/original/0/f/0f026343d8ba7f84cd70dec6417772b3509308e7.jpg)

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.

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [June 2, 2015, 8:31am UTC](https://discuss.elastic.co/t/elasticsearch-aggregations-get-additional-field-in-bucket/1689/2 "2015-06-02T08:31:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [June 2, 2015, 8:35am UTC](https://discuss.elastic.co/t/elasticsearch-aggregations-get-additional-field-in-bucket/1689/3 "2015-06-02T08:35:26Z")

</div>

> [@Petar\_Pavlovic](#):
>
> "makes": {  
> "terms": {  
> "field": "vehicle.make.raw",  
> "order": {  
> "\_term": "asc"  
> }  
> }  
> }  
> }

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:10am UTC](https://discuss.elastic.co/t/elasticsearch-aggregations-get-additional-field-in-bucket/1689/4 "2017-07-06T00:10:18Z")

</div>


