# Filter inside nested (pipeline) aggregation

**URL:** https://discuss.elastic.co/t/filter-inside-nested-pipeline-aggregation/180435
**Category:** Elasticsearch
**Created:** [May 9, 2019, 6:31pm UTC](https://discuss.elastic.co/t/filter-inside-nested-pipeline-aggregation/180435 "2019-05-09T18:31:41Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![van\_d39](https://avatars.discourse-cdn.com/v4/letter/v/2acd7d/32.png) [@van\_d39](https://discuss.elastic.co/u/van_d39)
#### Post date: [May 9, 2019, 6:31pm UTC](https://discuss.elastic.co/t/filter-inside-nested-pipeline-aggregation/180435/1 "2019-05-09T18:31:42Z")

</div>

I have a query that tries to bucket the records by year field and then for each bucket, I want to

- Create a sub count (numerator) with a filter criteria and get cardinality (unique count) of custom\_id
- Create a sub count (denominator) with a filter criteria and get cardinality (unique count) of custom\_id
- Divide numerator and denominator

What I am not able to do is add the filter criteria for both numerator and denominator sub buckets.

Here is what my query looks like -

```
GET <index_name>/_search
{
  "size": 0,
  "query": { . . <ABLE TO USE bool QUERY HERE>. . },
  "aggs": {
    "test": {
      "date_histogram": {
        "field": "datetimefield",
        "interval": "year",
        "format": "yyyy"
      },
      "aggs": {
        "filter": { <UNABLE TO USE THE SAME bool QUERY here> },
        "denominator": {
          "cardinality": {
            "field": "custom_id.keyword"
          }
        },
        "numerator": {
          "cardinality": {
            "field": "custom_id.keyword"
          }
        },
        "division": {
          "bucket_script": {
            "buckets_path": {
              "denom": "denominator",
              "num": "numerator"
            },
            "script": "params.num / params.denom"
          }
        }
      }
    }
  }
}

```

The reason why I need a filter block for numerator and denominator is because I have different set of criteria for both of them. Is it possible to do what I am trying to achieve here?

I've posted the same question here - [https://stackoverflow.com/questions/56065107/filter-inside-aggregation-for-elasticsearch-query](https://stackoverflow.com/questions/56065107/filter-inside-aggregation-for-elasticsearch-query)

Appreciate all 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: [June 6, 2019, 6:31pm UTC](https://discuss.elastic.co/t/filter-inside-nested-pipeline-aggregation/180435/2 "2019-06-06T18:31:55Z")

</div>

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