# Cannot get aggregation result without filter

**URL:** https://discuss.elastic.co/t/cannot-get-aggregation-result-without-filter/189642
**Category:** Elasticsearch
**Created:** [July 10, 2019, 1:47am UTC](https://discuss.elastic.co/t/cannot-get-aggregation-result-without-filter/189642 "2019-07-10T01:47:56Z")
**Posts on this page:** 8
**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 10, 2019, 1:47am UTC](https://discuss.elastic.co/t/cannot-get-aggregation-result-without-filter/189642/1 "2019-07-10T01:47:56Z")

</div>

## I want a filtered docs result and not filtered aggregation result in same query.

I've been used elasticsearch 1.75 and able to get this result, but I can't do this in elasticsearch 7.  
If I am missing something, please let me know.

## es 1.75

### es 1.75 create index and add docs

almost same as es7, I wanted put original curl sample, but because of text limit, removed it.  
if someone want to it, I can add it in comment.

### es 1.75 query

```auto
curl -X GET "localhost:9200/some_index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must":{
            "match_all": {}
          }
        }
      },
      "functions": []
    }
  },
  "filter": {
     "and": [
      { "terms":{ "active": [true] } },
      { "terms":{ "gender": ["m"] } }
      
    ]
  },
  "from": 0,
  "size": 10,
  "aggs": {
    "in_gen": {
      "filter": {
        "match_all": {}
      },
      "aggs": {
        "gen": {
          "terms": {
            "field": "gender",
            "size": 30,
            "shard_size": 30
          }
        }
      }
    },
    "in_age": {
      "filter": {
        "match_all": {}
      },
      "aggs": {
        "age": {
          "terms": {
            "field": "age",
            "size": 30,
            "shard_size": 30
          }
        }
      }
    }
  }
}
'

```

### es 1.75 result

- hit docs are filtered result (count: 3) with `active: true` and `gender: 'M'`
- aggregation result is for all (count: 5)

```auto
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        ...
        "_source": {
          "user_id": 1,
          "active": true,
          "gender": "M",
          "age": 30
        }
      },
      {
        ...
        "_source": {
          "user_id": 2,
          "active": true,
          "gender": "M",
          "age": 37
        }
      },
      {
        ...
        "_source": {
          "user_id": 3,
          "active": true,
          "gender": "M",
          "age": 23
        }
      }
    ]
  },
  "aggregations": {
    "in_gen": {
      "doc_count": 5,
      "gen": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "m",
            "doc_count": 3
          },
          {
            "key": "f",
            "doc_count": 2
          }
        ]
      }
    },
    "in_age": {
      "doc_count": 5,
      "age": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": 23,
            "doc_count": 1
          },
          {
            "key": 27,
            "doc_count": 1
          },
          {
            "key": 30,
            "doc_count": 1
          },
          {
            "key": 31,
            "doc_count": 1
          },
          {
            "key": 37,
            "doc_count": 1
          }
        ]
      }
    }
  }
}

```

## es 7 (7.1.0)

```auto
Version: 7.1.0, Build: default/tar/606a173/2019-05-16T00:43:15.323135Z, JVM: 1.8.0_112

```

### es7 - create index

```auto
curl -X DELETE "localhost:9700/some_index"

curl -X PUT "localhost:9700/some_index" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "number_of_shards": 5,
    "number_of_replicas": 1,
    "analysis": {}
  },
  "mappings": {
    "dynamic": false,
    "properties": {
      "user_id": {
        "type": "integer"
      },
      "active": {
        "type": "boolean"
      },
      "gender": {
        "type": "keyword"
      },
      "age": {
        "type": "integer"
      }
    }
  }
}
'

```

### es7 - add docs

```auto

curl -X PUT "localhost:9700/some_index/_doc/1" -H "Content-Type: application/json" -d'
{
  "user_id": 1,
  "active": true,
  "gender": "M",
  "age": 30
}
'
... 4 more docs here. (I couldn't write because of text limitation)

```

### es7 - query

```auto
curl -X GET "localhost:9700/some_index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "filter": [
            { "terms":{ "active": [true] } },
            { "terms":{ "gender": ["M"] } }
          ]
        }
      },
      "functions": []
    }
  },
  "from": 0,
  "size": 10,
  "aggs": {
    "in_gen": {
      "filter": {
        "match_all": {}
      },
      "aggs": {
        "gen": {
          "terms": {
            "field": "gender",
            "size": 30,
            "shard_size": 30
          }
        }
      }
    },
    "in_age": {
      "filter": {
        "match_all": {}
      },
      "aggs": {
        "age": {
          "terms": {
            "field": "age",
            "size": 30,
            "shard_size": 30
          }
        }
      }
    }
  }
}
'

```

### es7 result

- hit docs are filtered result (count: 3) with `active: true` and `gender: 'M'`
- aggregation result is filtered (count: 3) with `active: true` and `gender: 'M'`

#### How can I get aggregation result for all? (count: 5)

```auto
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0,
    "skipped": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 0,
    "hits": [
      {
        "_index": "some_index",
        "_type": "_doc",
        "_id": "3",
        "_score": 0,
        "_source": {
          "user_id": 3,
          "active": true,
          "gender": "M",
          "age": 23
        }
      },
      {
        "_index": "some_index",
        "_type": "_doc",
        "_id": "2",
        "_score": 0,
        "_source": {
          "user_id": 2,
          "active": true,
          "gender": "M",
          "age": 37
        }
      },
      {
        "_index": "some_index",
        "_type": "_doc",
        "_id": "1",
        "_score": 0,
        "_source": {
          "user_id": 1,
          "active": true,
          "gender": "M",
          "age": 30
        }
      }
    ]
  },
  "aggregations": {
    "in_gen": {
      "doc_count": 3,
      "gen": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "M",
            "doc_count": 3
          }
        ]
      }
    },
    "in_age": {
      "doc_count": 3,
      "age": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": 23,
            "doc_count": 1
          },
          {
            "key": 30,
            "doc_count": 1
          },
          {
            "key": 37,
            "doc_count": 1
          }
        ]
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)
#### Post date: [July 10, 2019, 4:35am UTC](https://discuss.elastic.co/t/cannot-get-aggregation-result-without-filter/189642/2 "2019-07-10T04:35:45Z")

</div>

Hi @hangu_choi ,

Look like structure is not correct according to the documentation:  
[https://www.elastic.co/guide/en/elasticsearch/reference/7.2/query-dsl-function-score-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.2/query-dsl-function-score-query.html)  
you need to have the filter inside functions.

```
"query": { 
    "function_score": { 
        "query": { "match_all": {} },
        "functions": [ 
            { "filter": {"terms": {"active": [true]}}},
            {"filter": {"terms": {"gender": ["M"]}}}
....

```

there's alternative and more light syntaxe if you don't use function score as there's no boost or score changes, but maybe it's only for the example and in your real request you have boost etc... blablablabla 🤐.  
[https://www.elastic.co/guide/en/elasticsearch/reference/7.2/query-filter-context.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.2/query-filter-context.html)

---

<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 10, 2019, 4:59am UTC](https://discuss.elastic.co/t/cannot-get-aggregation-result-without-filter/189642/3 "2019-07-10T04:59:12Z")

</div>

@gabriel_tessier

Thank you for reply.  
I use function\_score query.  
I just omitted it because original query structure is too long.

and as I understand, `filter` inside of functions affect final score.  
but I want to filter result without affecting score.

so, I used filter in `bool` query.

---

<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 10, 2019, 4:59am UTC](https://discuss.elastic.co/t/cannot-get-aggregation-result-without-filter/189642/4 "2019-07-10T04:59:43Z")

</div>

I think you should use a global agg first which will use all data whatever the query. See [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-global-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-global-aggregation.html)

---

<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 10, 2019, 5:00am UTC](https://discuss.elastic.co/t/cannot-get-aggregation-result-without-filter/189642/5 "2019-07-10T05:00:39Z")

</div>

Thank you @dadoonet,  
It seems what I am looking for. I'll see it.

---

<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:34am UTC](https://discuss.elastic.co/t/cannot-get-aggregation-result-without-filter/189642/6 "2019-07-11T00:34:26Z")

</div>

Thank you @dadoonet, `global` solved first issues I encountered.

but I have one more problem.  
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 do 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":{ "active": [true] } },
            { "terms":{ "gender": ["M"] } }
          ]
        }
      },
      "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":{ "active": [true] } },
            { "terms":{ "age": [30] } }
          ],
          "aggs": {
            "global_gen_filtered": {
              "terms": {
                "field": "gender",
                "size": 30,
                "shard_size": 30
              }
            }
          }
        }
      }
    },
    "in_gen": {
      "filter": {
        "match_all": {}
      },
      "aggs": {
        "gen": {
          "terms": {
            "field": "gender",
            "size": 30,
            "shard_size": 30
          }
        }
      }
    },
    "in_age": {
      "filter": {
        "match_all": {}
      },
      "aggs": {
        "age": {
          "terms": {
            "field": "age",
            "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/cannot-get-aggregation-result-without-filter/189642/7 "2019-07-11T02:25:55Z")

</div>

I answered in the new question you opened.

---

<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, 2:26am UTC](https://discuss.elastic.co/t/cannot-get-aggregation-result-without-filter/189642/8 "2019-08-08T02:26:02Z")

</div>

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