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


(Bertrand Renuart) #1

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

So the following query:

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

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


(system) #2

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