Bucket selection (Elasticsearch 6.8)

Hi, I'm running this query

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

"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


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

{
  "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

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

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).

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