Filter inside nested (pipeline) aggregation

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

Appreciate all the help!

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