# 5.1: "must" or "filter" query with aggregation?

**URL:** https://discuss.elastic.co/t/5-1-must-or-filter-query-with-aggregation/71780
**Category:** Elasticsearch
**Created:** [January 16, 2017, 7:56pm UTC](https://discuss.elastic.co/t/5-1-must-or-filter-query-with-aggregation/71780 "2017-01-16T19:56:21Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Bertrand](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bertrand/32/45679_2.png) [@Bertrand](https://discuss.elastic.co/u/Bertrand)
#### Post date: [January 16, 2017, 7:56pm UTC](https://discuss.elastic.co/t/5-1-must-or-filter-query-with-aggregation/71780/1 "2017-01-16T19:56:21Z")

</div>

From what I understand from the doc, a `filter` clause should be used in a _Bool Query_ when document scoring doesn't matter (cfr. [https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-bool-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-bool-query.html)).

So the following query:

```auto
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "@timestamp": {
              "gte": "2017-01-07T00:00:00.000Z",
              "lte": "2017-01-08T00:00:00.000Z"
            }
          }
        },
        {
          "query_string": {
            "analyze_wildcard": true,
            "query": "instanceId: xyz AND metric: harvester.input.tcp.lines"
          }
        }
]
    }
  },
  "aggs": {
    "2": {
      "date_histogram": {
        "interval": "1m",
        "field": "@timestamp"
      },
      "aggs": {
        "1": {
          "max": {
            "field": "total"
          }
        }
      }
    }
  }
}

```

should ideally be rewritten by replacing the `must` clause with a `filter` clause as follows:

```auto
{
  "size": 0,
  "query": {
    "bool": {
      "filter": [ {
          "range": {
            "@timestamp": {
              "gte": "2017-01-07T00:00:00.000Z",
              "lte": "2017-01-08T00:00:00.000Z"
            }
          }
        }, {
          "query_string": {
            "analyze_wildcard": true,
            "query": "instanceId: xyz AND metric: harvester.input.tcp.lines"
          }
        }]
    }
  },
  "aggs": {
    "2": {
      "date_histogram": {
        "interval": "1m",
        "field": "@timestamp"
      },
      "aggs": {
        "1": {
          "max": {
            "field": "total"
          }
        }
      }
    }
  }
}

```

Correct?  
Is it _required_ or is ES intelligent enough to discover by himself scoring is not important ?

---

<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: [February 13, 2017, 7:56pm UTC](https://discuss.elastic.co/t/5-1-must-or-filter-query-with-aggregation/71780/2 "2017-02-13T19:56:31Z")

</div>

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