# Doc\_count type cast error while using bucket aggregations

**URL:** <https://discuss.elastic.co/t/doc-count-type-cast-error-while-using-bucket-aggregations/182185>\
**Category:** Elasticsearch\
**Created:** [May 22, 2019, 9:17am UTC](https://discuss.elastic.co/t/doc-count-type-cast-error-while-using-bucket-aggregations/182185 "2019-05-22T09:17:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![TEJASWI\_CHUNDURI](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tejaswi_chunduri/32/46271_2.png) [@TEJASWI\_CHUNDURI](https://discuss.elastic.co/u/TEJASWI_CHUNDURI)\
**Post date:** [May 22, 2019, 9:17am UTC](https://discuss.elastic.co/t/doc-count-type-cast-error-while-using-bucket-aggregations/182185/1 "2019-05-22T09:17:24Z")

</div>

Using kibana 6.5.4

When checking the doc\_count . it is replying with cast issue .. I need the results only doc\_count \<2

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/a/2ae689a91abc0698ad6d2c0013a720dfa81e579e.png)

```
{
  "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": [
        "params.doc_count <= 1",
        "^---- HERE"
      ],
      "script": "params.doc_count <= 1",
      "lang": "painless",
      "caused_by": {
        "type": "class_cast_exception",
        "reason": "Cannot cast from [boolean] to [java.lang.Number]."
      }
    }
  },
  "status": 503
}
```

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [May 24, 2019, 12:35pm UTC](https://discuss.elastic.co/t/doc-count-type-cast-error-while-using-bucket-aggregations/182185/2 "2019-05-24T12:35:30Z")

</div>

The BucketScript pipeline agg is expected to emit a number, not a boolean which is why your script is failing.

What are you trying to achieve? You could emit a 0 or 1 if you're just wanting to set some kind of flag.

If you're wanting to remove buckets that are under a certain doc count, the `bucket_selector` pipeline agg is what you want: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-selector-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-selector-aggregation.html)

---

<div class="post-metadata">

**Author:** ![TEJASWI\_CHUNDURI](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tejaswi_chunduri/32/46271_2.png) [@TEJASWI\_CHUNDURI](https://discuss.elastic.co/u/TEJASWI_CHUNDURI)\
**Post date:** [May 27, 2019, 8:13am UTC](https://discuss.elastic.co/t/doc-count-type-cast-error-while-using-bucket-aggregations/182185/3 "2019-05-27T08:13:28Z")

</div>

Hi polyfractal,

```
Please check the below where i used bucket selected where i need doc_count < 2 .. but response has the doc_counts

```

![image](https://us1.discourse-cdn.com/elastic/original/3X/b/a/ba82b6d60171dfa0f88b9c97dc1b7448529b9fe3.png)

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [May 28, 2019, 1:25pm UTC](https://discuss.elastic.co/t/doc-count-type-cast-error-while-using-bucket-aggregations/182185/4 "2019-05-28T13:25:26Z")

</div>

Your bucket selector is a level too low. E.g. it is embedded under the `"3"` terms aggregation, meaning all buckets generated by `"3"` will be pruned. You'll need to move the selector up higher if you want it to affect the next level up.

Also note that the bucket selector saves the bucket when "true" is returned. So `params.count < 2` means "save buckets that have a doc\_count less than 2".

---

<div class="post-metadata">

**Author:** ![TEJASWI\_CHUNDURI](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tejaswi_chunduri/32/46271_2.png) [@TEJASWI\_CHUNDURI](https://discuss.elastic.co/u/TEJASWI_CHUNDURI)\
**Post date:** [May 28, 2019, 1:57pm UTC](https://discuss.elastic.co/t/doc-count-type-cast-error-while-using-bucket-aggregations/182185/5 "2019-05-28T13:57:08Z")

</div>

```
POST /appscan*/_search
{
  "aggs": {
    "2": {
      "terms": {
        "field": "url_type_uniqref.keyword",
        "size": 35,
        "order": {
          "_count": "asc"
        }
      },
      "aggs": {
        "3": {
          "terms": {
            "field": "fversion.keyword",
            "size": 20,
            "order": {
              "_key": "desc"
            }
          },
            "bbukc": {
              "bucket_selector": {
                "buckets_path": {
                  "the_count": "_count"
                },
                "script": "params.the_count < 2"
              }
            },
          "aggs": {
            "1": {
              "max": {
                "field": "issue_group_item_cvss_score"
              }
            }
          }
        }
      }
    }

```

{  
"error": {  
"root\_cause": [  
{  
"type": "parsing\_exception",  
"reason": "Found two aggregation type definitions in [3]: [terms] and [bbukc]",  
"line": 20,  
"col": 22  
}  
],  
"type": "parsing\_exception",  
"reason": "Found two aggregation type definitions in [3]: [terms] and [bbukc]",  
"line": 20,  
"col": 22  
},  
"status": 400  
}

---

<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 25, 2019, 1:57pm UTC](https://discuss.elastic.co/t/doc-count-type-cast-error-while-using-bucket-aggregations/182185/6 "2019-06-25T13:57:12Z")

</div>

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