# ElasticSearch: Aggregations by field of single (filtered) element of nested array

**URL:** <https://discuss.elastic.co/t/elasticsearch-aggregations-by-field-of-single-filtered-element-of-nested-array/78651>\
**Category:** Elasticsearch\
**Created:** [March 15, 2017, 7:04am UTC](https://discuss.elastic.co/t/elasticsearch-aggregations-by-field-of-single-filtered-element-of-nested-array/78651 "2017-03-15T07:04:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Teimuraz\_Kantaria](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/teimuraz_kantaria/32/16351_2.png) [@Teimuraz\_Kantaria](https://discuss.elastic.co/u/Teimuraz_Kantaria)\
**Post date:** [March 15, 2017, 7:04am UTC](https://discuss.elastic.co/t/elasticsearch-aggregations-by-field-of-single-filtered-element-of-nested-array/78651/1 "2017-03-15T07:04:15Z")

</div>

There is a list of products.

Each product (document) has such data like id, name, etc.

Also, each document holds specific information per each client (such as **availability** :  
given product can be available for one client, and not available for another). This specific information is a nested array.

**'availability'** may hold following values: ' **sold\_out**', ' **available**', ' **dead**';

Here is an ES mapping:

```
"mappings": {
    "products": {
        "properties": {
            ...
            "clients": {
                "type": "nested",
                "include_in_parent": true,
                "properties": {
                    ...
                    "availability": {
                        "type": "string",
                        "index": "not_analyzed" 
                    }           
                }
            } 
        }
    }
}    

```

I need to get a count of products which are **sold\_out** , **available** and **dead** for given client.  
(There is client filter in search params)

```
...
"query": {
    "bool": { 
      "filter": [
        { "term": {"clients.id": 123456789}},        
      ]
    }
  }
...

```

How to achieve this?

---

<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:** [April 12, 2017, 7:04am UTC](https://discuss.elastic.co/t/elasticsearch-aggregations-by-field-of-single-filtered-element-of-nested-array/78651/2 "2017-04-12T07:04:28Z")

</div>

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