# Aggregation on Searcresult with terms (Buckets inside Buckets)

**URL:** <https://discuss.elastic.co/t/aggregation-on-searcresult-with-terms-buckets-inside-buckets/24934>\
**Category:** Elasticsearch\
**Created:** [July 6, 2015, 9:29am UTC](https://discuss.elastic.co/t/aggregation-on-searcresult-with-terms-buckets-inside-buckets/24934 "2015-07-06T09:29:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Delphi400](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@Delphi400](https://discuss.elastic.co/u/Delphi400)\
**Post date:** [July 6, 2015, 9:29am UTC](https://discuss.elastic.co/t/aggregation-on-searcresult-with-terms-buckets-inside-buckets/24934/1 "2015-07-06T09:29:54Z")

</div>

Hello,

i have documents with this json description

```
...         
            "Attributes": {
                "type": "nested",
                "properties": {
                    "Identifier": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "Value": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                }
            },
...

```

where i do my search with the java API. Now i like to make some Aggregation on my search result.  
with  
AggregationBuilder aggregation = AggregationBuilders.nested("Identifier").path("Attributes");  
aggregation.subAggregation(AggregationBuilders.terms("agg\_ident").field("Attributes.Identifier"));  
i get my Identiefiers.

but i need the count of the Values too.

For example if i have data like this:  
attr6 valueA  
attr6 valueA  
attr6 valueA  
attr6 valueB  
attr6 valueB  
attr7 valueA  
attr7 valueA

i need the Result  
I have attr6 with valueA 3 Times  
I have attr6 with valueB 2 Times  
I have attr7 with valueA 2 Times

If i try  
AggregationBuilder aggregation = AggregationBuilders.nested("Identifier").path("Attributes");  
aggregation.subAggregation(AggregationBuilders.terms("agg\_ident").field("Attributes.Identifier"));  
aggregation.subAggregation(AggregationBuilders.terms("agg\_werte").field("Attributes.Value"));  
i get 2 rsults but i lose my relation.

How can i resolve this?

---

<div class="post-metadata">

**Author:** ![Delphi400](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@Delphi400](https://discuss.elastic.co/u/Delphi400)\
**Post date:** [July 7, 2015, 7:52am UTC](https://discuss.elastic.co/t/aggregation-on-searcresult-with-terms-buckets-inside-buckets/24934/2 "2015-07-07T07:52:34Z")

</div>

I Still try to resolve this problem.

I need a Bucket inside a Bucket as explain here:  
[https://www.elastic.co/guide/en/elasticsearch/guide/current/\_buckets\_inside\_buckets.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/_buckets_inside_buckets.html)

and this is my request:

```
{
...
"aggs" : {
    "Identifier" : {
      "nested" : {
        "path" : "Attributes"
      },
      "aggs" : {
        "ident" : {
          "terms" : {"field" : "Attributes.Identifier"}
          },
        "werte" : {
          "terms" : {"field" : "Attributes.Value"}
          }
      }
    }
  }
}

```

but i get 2 buckets:

```
  "aggregations":
    {
        "Identifier":
        {
            "doc_count": 411,
            "ident":
            {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets":
                [
                    {
                        "key": "113",
                        "doc_count": 193
                    },
                    {
                        "key": "6",
                        "doc_count": 193
                    },
                    ....
                ]
            },
            "werte":
            {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets":
                [
                    {
                        "key": "24",
                        "doc_count": 147
                    },
                    {
                        "key": "12",
                        "doc_count": 39
                    },
                    ..
                ]
            }
        }
    }

```

where is my mistake?

---

<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:** [July 7, 2015, 8:16am UTC](https://discuss.elastic.co/t/aggregation-on-searcresult-with-terms-buckets-inside-buckets/24934/3 "2015-07-07T08:16:49Z")

</div>

> [@Delphi400](#):
>
> {..."aggs" : { "Identifier" : { "nested" : { "path" : "Attributes" }, "aggs" : { "ident" : { "terms" : {"field" : "Attributes.Identifier"} }, "werte" : { "terms" : {"field" : "Attributes.Value"} } } } }}

If you what to know the values of `Attributes.Value` for each value of `Attributes.Identifier`, try this instead:

```json
{
    ...
    "aggs" : {
        "Identifier" : {
            "nested" : {
                "path" : "Attributes"
            },
            "aggs" : {
                "ident" : {
                    "terms" : {"field" : "Attributes.Identifier"},
                    "aggs": {
                        "werte" : {
                            "terms" : {"field" : "Attributes.Value"}
                        }
                    }
               }
           }
       }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Delphi400](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@Delphi400](https://discuss.elastic.co/u/Delphi400)\
**Post date:** [July 7, 2015, 9:29am UTC](https://discuss.elastic.co/t/aggregation-on-searcresult-with-terms-buckets-inside-buckets/24934/4 "2015-07-07T09:29:11Z")

</div>

Thanks for your answer this works perfect. 😀

This was my code:

AggregationBuilder aggregation = AggregationBuilders.nested("Identifier").path("Attributes");  
AggregationBuilder aggregation1 = AggregationBuilders.terms("ident").field("Attributes.Identifier");  
AggregationBuilder aggregation2 = AggregationBuilders.terms("werte").field("Attributes.Value");

aggregation.subAggregation(aggregation1);  
aggregation.subAggregation(aggregation2);

But of course agg2 has to be sub of agg1 and this together has to be sub of agg

aggregation1.subAggregation(aggregation2);  
aggregation.subAggregation(aggregation1);

Thanks for the Help!

---

<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:03am UTC](https://discuss.elastic.co/t/aggregation-on-searcresult-with-terms-buckets-inside-buckets/24934/5 "2017-07-06T00:03:16Z")

</div>


