# Bucket selection (Elasticsearch 6.8)

**URL:** <https://discuss.elastic.co/t/bucket-selection-elasticsearch-6-8/287697>\
**Category:** Elasticsearch\
**Created:** [October 26, 2021, 1:03pm UTC](https://discuss.elastic.co/t/bucket-selection-elasticsearch-6-8/287697 "2021-10-26T13:03:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dev\_test](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dev_test/32/79287_2.png) [@dev\_test](https://discuss.elastic.co/u/dev_test)\
**Post date:** [October 26, 2021, 1:03pm UTC](https://discuss.elastic.co/t/bucket-selection-elasticsearch-6-8/287697/1 "2021-10-26T13:03:41Z")

</div>

Hi, I'm running this query

```auto
GET assignment/_search?size=0
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "user_post": "post"
          }
        },
        {
          "nested": {
            "path": "box",
            "query": {
              "bool": {
                "must": [
                  {
                    "match_all": {}
                  }
                ]
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "objects": {
      "terms": {
        "field": "_id"
      },
      "aggs": {
        "box": {
          "nested": {
            "path": "box"
          },
          "aggs": {
            "box_counts": {
              "value_count": {
                "field": "box.box_id"
              }
            }
          }
        }
      }
    }
  }
}

```

and i'm getting this result

```auto
"aggregations" : {
    "objects" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "1_7",
          "doc_count" : 1,
          "box" : {
            "doc_count" : 1,
            "box_counts" : {
              "value" : 1
            }
          }
        },
        {
          "key" : "2_8",
          "doc_count" : 1,
          "box" : {
            "doc_count" : 1,
            "box_counts" : {
              "value" : 1
            }
          }
        },
        {
          "key" : "3_19",
          "doc_count" : 1,
          "box" : {
            "doc_count" : 1,
            "box_counts" : {
              "value" : 2
            }
          }
        },
        {
          "key" : "3_9",
          "doc_count" : 1,
          "box" : {
            "doc_count" : 1,
            "box_counts" : {
              "value" : 1
            }
          }
        },
        {
          "key" : "4_10",
          "doc_count" : 1,
          "box" : {
            "doc_count" : 1,
            "box_counts" : {
              "value" : 1
            }
          }
        }
      ]
    }
  }
}

```

i want the buckets having box\_counts.value \>= 2. To achieve this i'm trying to use bucket selector and the query becomes

```auto

GET assignment/_search?size=0
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "user_post": "post"
          }
        },
        {
          "nested": {
            "path": "box",
            "query": {
              "bool": {
                "must": [
                  {
                    "match_all": {}
                  }
                ]
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "objects": {
      "terms": {
        "field": "_id"
      },
      "aggs": {
        "box": {
          "nested": {
            "path": "box"
          },
          "aggs": {
            "box_counts": {
              "value_count": {
                "field": "box.box_id"
              }
            },
            "boxes_filter": {
              "bucket_selector": {
                "buckets_path": {
                  "required_value": "box_counts"
                },
                "script": "required_value >= 2"
              }
            }
          }
        }
      }
    }
  }
}

```

and this results in error with the following message

```auto
{
  "error": {
    "root_cause": [],
    "type": "search_phase_execution_exception",
    "reason": "",
    "phase": "fetch",
    "grouped": true,
    "failed_shards": [],
    "caused_by": {
      "type": "class_cast_exception",
      "reason": "class org.elasticsearch.search.aggregations.bucket.nested.InternalNested cannot be cast to class org.elasticsearch.search.aggregations.InternalMultiBucketAggregation (org.elasticsearch.search.aggregations.bucket.nested.InternalNested and org.elasticsearch.search.aggregations.InternalMultiBucketAggregation are in unnamed module of loader 'app')"
    }
  },
  "status": 503
}

```

kindly help me to achieve the goal

---

<div class="post-metadata">

**Author:** ![Tommy\_Monk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tommy_monk/32/83712_2.png) [@Tommy\_Monk](https://discuss.elastic.co/u/Tommy_Monk)\
**Post date:** [November 12, 2021, 11:59am UTC](https://discuss.elastic.co/t/bucket-selection-elasticsearch-6-8/287697/2 "2021-11-12T11:59:24Z")

</div>

I'm also experiencing the same issue, did you find a solution ?

---

<div class="post-metadata">

**Author:** ![dev\_test](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dev_test/32/79287_2.png) [@dev\_test](https://discuss.elastic.co/u/dev_test)\
**Post date:** [November 16, 2021, 10:29am UTC](https://discuss.elastic.co/t/bucket-selection-elasticsearch-6-8/287697/3 "2021-11-16T10:29:39Z")

</div>

I didn't find the correct way to use Bucket Selection, however i got my desired result by adding scripts in query filter (no aggregated function used).

---

<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:** [December 14, 2021, 10:30am UTC](https://discuss.elastic.co/t/bucket-selection-elasticsearch-6-8/287697/4 "2021-12-14T10:30:04Z")

</div>

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