# Query with Many Aggregations

**URL:** <https://discuss.elastic.co/t/query-with-many-aggregations/67141>\
**Category:** Elasticsearch\
**Created:** [November 24, 2016, 10:41pm UTC](https://discuss.elastic.co/t/query-with-many-aggregations/67141 "2016-11-24T22:41:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alberto.Biason](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Alberto.Biason](https://discuss.elastic.co/u/Alberto.Biason)\
**Post date:** [November 24, 2016, 10:41pm UTC](https://discuss.elastic.co/t/query-with-many-aggregations/67141/1 "2016-11-24T22:41:19Z")

</div>

Good Evening,

I have this situation:

- products  
|--- product  
|------ title  
|------ category  
|------ subcategory  
|------ brand  
|------ attributes  
|--------- attribute  
|------------ id  
|------------ value  
|------------ type

I did a products's query by category and subcategory with brand aggregation and specific attributes aggregation:

```auto
{
  "size": 0
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "category": "Tecnologia e Eletrônicos"
          }
        },
        {
          "match": {
            "subcategory": "TV"
          }
        }
      ]
    }
  },
  "aggregations": {
    "brands": {
      "terms": {
        "field": "brand",
        "size": 1000
      }
    },
    "inches": {
      "terms": {
        "query": {
          "bool": {
            "must": [
              {
                "match": {
                  "attributes.id": "polegadas"
                }
              }
            ]
          }
        },
        "field": "attributes.value",
        "size": 1000
      }
    }
  }
}

```

Can someyone help me?

---

<div class="post-metadata">

**Author:** ![guilherme\_maranhao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guilherme_maranhao/32/101483_2.png) [@guilherme\_maranhao](https://discuss.elastic.co/u/guilherme_maranhao)\
**Post date:** [November 25, 2016, 10:55am UTC](https://discuss.elastic.co/t/query-with-many-aggregations/67141/2 "2016-11-25T10:55:39Z")

</div>

Hi Alberto,

What is your problem specifically? The aggregations are not working as you expected?

Guilherme

---

<div class="post-metadata">

**Author:** ![Alberto.Biason](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Alberto.Biason](https://discuss.elastic.co/u/Alberto.Biason)\
**Post date:** [November 25, 2016, 11:54am UTC](https://discuss.elastic.co/t/query-with-many-aggregations/67141/3 "2016-11-25T11:54:55Z")

</div>

Hi, Guilherme.

In my query, the result is:

- All brands (OK!);
- All attributes's values (NOK).

I expected all possible inches (polegadas).

Can you help me?

---

<div class="post-metadata">

**Author:** ![guilherme\_maranhao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guilherme_maranhao/32/101483_2.png) [@guilherme\_maranhao](https://discuss.elastic.co/u/guilherme_maranhao)\
**Post date:** [November 25, 2016, 12:06pm UTC](https://discuss.elastic.co/t/query-with-many-aggregations/67141/4 "2016-11-25T12:06:53Z")

</div>

According to your product mapping, the attributes property doesn't contain an id and value. They are nested to the attribute object, aren't they? If that's the case, "[attributes.id](http://attributes.id)" and "attributes.value" won't work. You should use "[attributes.attribute.id](http://attributes.attribute.id)".

Try this solution

If even this change doesn't work, maybe you'll need to define your attributes object with :type :nested, as you need to retrieve the attributes.attribute.value and [attributes.attribute.id](http://attributes.attribute.id) together. The flat structure won't enable it.

[https://www.elastic.co/guide/en/elasticsearch/reference/5.0/nested.html#\_using\_literal\_nested\_literal\_fields\_for\_arrays\_of\_objects](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/nested.html#_using_literal_nested_literal_fields_for_arrays_of_objects)

---

<div class="post-metadata">

**Author:** ![guilherme\_maranhao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guilherme_maranhao/32/101483_2.png) [@guilherme\_maranhao](https://discuss.elastic.co/u/guilherme_maranhao)\
**Post date:** [November 25, 2016, 12:13pm UTC](https://discuss.elastic.co/t/query-with-many-aggregations/67141/5 "2016-11-25T12:13:00Z")

</div>

Sorry, the `attribute` object should be defined as :type 'nested', not the `attributes`.

---

<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:** [December 23, 2016, 12:13pm UTC](https://discuss.elastic.co/t/query-with-many-aggregations/67141/6 "2016-12-23T12:13:14Z")

</div>

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