# Filter based on the doc\_count with aggregations (2)

**URL:** <https://discuss.elastic.co/t/filter-based-on-the-doc-count-with-aggregations-2/147734>\
**Category:** Elasticsearch\
**Created:** [September 7, 2018, 2:37pm UTC](https://discuss.elastic.co/t/filter-based-on-the-doc-count-with-aggregations-2/147734 "2018-09-07T14:37:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![caub](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/caub/32/33783_2.png) [@caub](https://discuss.elastic.co/u/caub)\
**Post date:** [September 7, 2018, 2:37pm UTC](https://discuss.elastic.co/t/filter-based-on-the-doc-count-with-aggregations-2/147734/1 "2018-09-07T14:37:40Z")

</div>

I'd like to continue [Filter based on the doc\_count with aggregations](https://discuss.elastic.co/t/filter-based-on-the-doc-count-with-aggregations/62677)

I made the following query:

```auto
GET heartbeat*/_search?size=0
{
  "size": 200,
  "query": {
    "bool": {
      "filter": [
        {
          "match": {
            "monitor.status": "down"
          }
        },
        {
          "range": {
            "@timestamp": {
              "gte": "now-1h"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "hosts": {
      "terms": {
        "field": "monitor.host"
      },
      "aggs": {
        "time": {
          "date_histogram": {
            "field": "@timestamp",
            "interval": "180s"
          },
          "aggs": {
            "criticals": {
              "bucket_selector": {
                "buckets_path": {
                  "doc_count": "_count"
                },
                "script": "doc_count > 2"
              }
            }
          }
        }
      }
    }
  }
}

```

which gives this error:

```auto
{
  "error": {
    "root_cause": [],
    "type": "search_phase_execution_exception",
    "reason": "",
    "phase": "fetch",
    "grouped": true,
    "failed_shards": [],
    "caused_by": {
      "type": "script_exception",
      "reason": "compile error",
      "script_stack": [
        "doc_count > 2",
        "^---- HERE"
      ],
      "script": "doc_count > 2",
      "lang": "painless",
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "Variable [doc_count] is not defined."
      }
    }
  },
  "status": 503
}

```

My query work if I remove the last aggregation, but I'd like to filter out bucket with doc\_count \<=2

```auto
  "buckets": [
    {
      "key_as_string": "2018-09-07T13:36:00.000Z",
      "key": 1536327360000,
      "doc_count": 2
    },
    {
      "key_as_string": "2018-09-07T13:39:00.000Z",
      "key": 1536327540000,
      "doc_count": 1
    },
    {
      "key_as_string": "2018-09-07T13:42:00.000Z",
      "key": 1536327720000,
      "doc_count": 0
    },

```

using ES 6.4.0

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [September 7, 2018, 3:03pm UTC](https://discuss.elastic.co/t/filter-based-on-the-doc-count-with-aggregations-2/147734/2 "2018-09-07T15:03:22Z")

</div>

You need to use `params.doc_count` to access variables like `doc_count` in your script. The script should be:

```auto
"params.doc_count > 2"

```

---

<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:** [October 5, 2018, 3:03pm UTC](https://discuss.elastic.co/t/filter-based-on-the-doc-count-with-aggregations-2/147734/3 "2018-10-05T15:03:24Z")

</div>

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