# Filter aggregations for nested documents

**URL:** <https://discuss.elastic.co/t/filter-aggregations-for-nested-documents/32238>\
**Category:** Elasticsearch\
**Created:** [October 15, 2015, 6:09am UTC](https://discuss.elastic.co/t/filter-aggregations-for-nested-documents/32238 "2015-10-15T06:09:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaleemullah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kaleemullah/32/5286_2.png) [@kaleemullah](https://discuss.elastic.co/u/kaleemullah)\
**Post date:** [October 15, 2015, 6:09am UTC](https://discuss.elastic.co/t/filter-aggregations-for-nested-documents/32238/1 "2015-10-15T06:09:56Z")

</div>

I've below nested mappings:

```
PUT /store
{
  "mappings": {
    "product": {
      "properties": {
        "specs": {
          "type": "nested",
          "properties": {
            "key": {
              "type": "string",
              "index": "not_analyzed"
            },
            "value": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      }
    }
  }
}

POST /store/product/1
{
  "name": "Galaxy S4",
  "manufacturer": "Samsung",
  "type": "phone",
  "specs": [
    {
      "key": "os",
      "value": "Android"
    },
    {
      "key": "core",
      "value": 4
    },
    {
      "key": "ram",
      "value": "2GB"
    }
  ]
}

POST /store/product/2
{
  "name": "iPhone 5",
  "manufacturer": "Apple",
  "type": "phone",
  "specs": [
    {
      "key": "os",
      "value": "iOS"
    },
    {
      "key": "core",
      "value": 2
    },
    {
      "key": "ram",
      "value": "1GB"
    }
  ]
}

POST /store/product/3
{
  "name": "WF210ANW/XAA",
  "manufacturer": "Samsung",
  "type": "washing-machine",
  "specs": [
    {
      "key": "capacity",
      "value": 3.5
    },
    {
      "key": "loadType",
      "value": "front"
    },
    {
      "key": "presets",
      "value": 6
    }
  ]
}

POST /store/product/4
{
  "name": "WT4801CW",
  "manufacturer": "LG",
  "type": "washing-machine",
  "specs": [
    {
      "key": "capacity",
      "value": 3.7
    },
    {
      "key": "loadType",
      "value": "top"
    },
    {
      "key": "presets",
      "value": 9
    }
  ]
}

GET /store/_search?search_type=count
{
  "aggs": {
    "specs": {
      "nested": {
        "path": "specs"
      },
      "aggs": {
        "key": {
          "terms": {
            "field": "specs.key"
          },
          "aggs": {
            "value": {
              "terms": {
                "field": "specs.value"
              }
            }
          }
        }
      }
    }
  }
}

```

My question is if a user select 'android' from OS bucket then how can I filter other buckets i.e 'Core' and 'RAM' for 'android' option? and also 'ios' should also stay in OS bucket so that user can change search option when he needs to.

Thank you.

---

<div class="post-metadata">

**Author:** ![Dariusp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dariusp/32/31415_2.png) [@Dariusp](https://discuss.elastic.co/u/Dariusp)\
**Post date:** [October 16, 2015, 10:57am UTC](https://discuss.elastic.co/t/filter-aggregations-for-nested-documents/32238/2 "2015-10-16T10:57:44Z")

</div>

you should specify filters for each aggregations:

```
{
  "aggs": {
    "agg_os-filtered": {
      "filter": {
        "terms": {
          "core": [
            "2"
          ]
        }
      },
      "aggregations": {
        "agg_os": {
          "terms": {
            "field": "os"
          }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![kaleemullah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kaleemullah/32/5286_2.png) [@kaleemullah](https://discuss.elastic.co/u/kaleemullah)\
**Post date:** [October 17, 2015, 2:10am UTC](https://discuss.elastic.co/t/filter-aggregations-for-nested-documents/32238/3 "2015-10-17T02:10:34Z")

</div>

Thanks @Dariusp but it does not work because fields are dynamic and we really don't know field name to filter and aggregate. As you can see in my question, aggregations are being defined as:

```
  "aggs": {
    "key": {
      "terms": {
        "field": "specs.key"
      }
   }

```

So, all the specs keys will be aggregate.

---

<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 5, 2017, 11:44pm UTC](https://discuss.elastic.co/t/filter-aggregations-for-nested-documents/32238/4 "2017-07-05T23:44:13Z")

</div>


