How can I filter with multiple condition in aggregation bucket?

I want agg bucket which filtered multiple condition like below.
but It is returning Expected [START_OBJECT] error

{"error":{"root_cause":[{"type":"parsing_exception","reason":"Expected [START_OBJECT] under [filter], but got a [START_ARRAY] in [in_global_gen_filtered]","line":31,"col":21}],"type":"parsing_exception","reason":"Expected [START_OBJECT] under [filter], but got a [START_ARRAY] in [in_global_gen_filtered]","line":31,"col":21},"status":400}

How can I filter with multiple condition in aggregation bucket?

curl -X GET "localhost:9700/some_index/_search" -H 'Content-Type: application/json' -d'

{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "filter": [
            { "terms":{ "some_field_1": [ "y" ] } },
            { "terms":{ "some_field_2": [ "y" ] } }
          ]
        }
      },
      "functions": []
    }
  },
  "from": 0,
  "size": 10,
  "aggs": {
    "in_global": {
      "global": {},
      "aggs": {
        "global_gen": {
          "terms": {
            "field": "gender",
            "size": 30,
            "shard_size": 30
          }
        },
        "in_global_gen_filtered": {
          "filter": [
            { "terms":{ "some_field_3": [ 'y' ] } },
            { "terms":{ "some_field_4": [ 'y' ] } }
          ],
          "aggs": {
            "global_gen_filtered": {
              "terms": {
                "field": "gender",
                "size": 30,
                "shard_size": 30
              }
            }
          }
        }
      }
    }
    
  }
}

'

For the aggs filter, use a bool query with a filter array which contains the 2 terms query.
The same way you did it within the function score.

Thank you @dadoonet,
I tried below query, but got error.

        "in_global_gen_filtered": {
          "query": {
            "bool": {
              "filter": [
                { "terms":{ "some_field_1": [ "y" ] } },
                { "terms":{ "some_field_2": [ "y" ] } }
              ]
            }
          }

and got

{
  "error": {
    "root_cause": [
      {
        "type": "named_object_not_found_exception",
        "reason": "[31:20] unable to parse BaseAggregationBuilder with name [query]: parser not found"
      }
    ],
    "type": "named_object_not_found_exception",
    "reason": "[31:20] unable to parse BaseAggregationBuilder with name [query]: parser not found"
  },
  "status": 400
}

and start with bool

  "in_global_gen_filtered": {
            "bool": {
              "filter": [
                { "terms":{ "some_field_1": [ "y" ] } },
                { "terms":{ "some_field_2": [ "y" ] } }
              ]
          }

and got

{
  "error": {
    "root_cause": [
      {
        "type": "named_object_not_found_exception",
        "reason": "[31:21] unable to parse BaseAggregationBuilder with name [bool]: parser not found"
      }
    ],
    "type": "named_object_not_found_exception",
    "reason": "[31:21] unable to parse BaseAggregationBuilder with name [bool]: parser not found"
  },
  "status": 400
}

Am I missing something?

@dadoonet,
maybe I can get the result I want with post_filter, too.

but I want to know if this can be done with global and multiple filter as you suggested.

@dadoonet,
It works with you suggestion,

"in_global_gen_filtered": {
          "filter": {
            "bool": {
              "filter": [
                { "terms":{ "some_field_1": [ "y" ] } },
                { "terms":{ "some_field_2": [ "y" ] } }
              ]
            }
          }

Thank you!!

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