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

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?

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