# How can I filter with multiple condition in aggregation bucket?

**URL:** https://discuss.elastic.co/t/how-can-i-filter-with-multiple-condition-in-aggregation-bucket/189883
**Category:** Elasticsearch
**Created:** [July 11, 2019, 12:38am UTC](https://discuss.elastic.co/t/how-can-i-filter-with-multiple-condition-in-aggregation-bucket/189883 "2019-07-11T00:38:05Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![hangu\_choi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hangu_choi/32/49745_2.png) [@hangu\_choi](https://discuss.elastic.co/u/hangu_choi)
#### Post date: [July 11, 2019, 12:38am UTC](https://discuss.elastic.co/t/how-can-i-filter-with-multiple-condition-in-aggregation-bucket/189883/1 "2019-07-11T00:38:06Z")

</div>

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

```auto
{"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?**

```auto
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
              }
            }
          }
        }
      }
    }
    
  }
}

'

```

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 11, 2019, 2:25am UTC](https://discuss.elastic.co/t/how-can-i-filter-with-multiple-condition-in-aggregation-bucket/189883/2 "2019-07-11T02:25:02Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![hangu\_choi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hangu_choi/32/49745_2.png) [@hangu\_choi](https://discuss.elastic.co/u/hangu_choi)
#### Post date: [July 11, 2019, 2:38am UTC](https://discuss.elastic.co/t/how-can-i-filter-with-multiple-condition-in-aggregation-bucket/189883/3 "2019-07-11T02:38:56Z")

</div>

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

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

```

and got

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

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

```

and got

```auto
{
  "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?

---

<div class="post-metadata">

### Author: ![hangu\_choi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hangu_choi/32/49745_2.png) [@hangu\_choi](https://discuss.elastic.co/u/hangu_choi)
#### Post date: [July 11, 2019, 2:43am UTC](https://discuss.elastic.co/t/how-can-i-filter-with-multiple-condition-in-aggregation-bucket/189883/4 "2019-07-11T02:43:37Z")

</div>

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

---

<div class="post-metadata">

### Author: ![hangu\_choi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hangu_choi/32/49745_2.png) [@hangu\_choi](https://discuss.elastic.co/u/hangu_choi)
#### Post date: [July 11, 2019, 3:50am UTC](https://discuss.elastic.co/t/how-can-i-filter-with-multiple-condition-in-aggregation-bucket/189883/5 "2019-07-11T03:50:57Z")

</div>

@dadoonet,  
It works with you suggestion,

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

```

Thank you!!

---

<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: [August 8, 2019, 3:51am UTC](https://discuss.elastic.co/t/how-can-i-filter-with-multiple-condition-in-aggregation-bucket/189883/6 "2019-08-08T03:51:11Z")

</div>

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