Filter instead of query for improved performance?

Upon reading this thread here https://stackoverflow.com/questions/14595988/queries-vs-filters , I took away from it that it's possible to submit a filter 'query' instead of a query 'query' for improved performance. My use case is that I simply want to find all the documents that have been created 5 minutes prior to 'now' within a certain index and type. So i was thinking that my query would look something like this:

curl -XGET "http://localhost:9200/someIndex/someType/_search" -H 'Content-Type: application/json' -d'
{
"filter": [
{
"range": {
"createdAt": {
"gte": "now-5m",
"lte": "now"
}
}
}]
}'

Unfortunately, this issues an error as follows:
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "Unknown key for a START_ARRAY in [filter].",
"line": 2,
"col": 15
}
],
"type": "parsing_exception",
"reason": "Unknown key for a START_ARRAY in [filter].",
"line": 2,
"col": 15
},
"status": 400
}

any help on forming my query would be much appreciated.

Filters are part of the bool query. You need to rewrite your query like this:

{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "createdAt": {
              "gte": "now-5m",
              "lte": "now"
            }
          }
        }
      ]
    }
  }
}
1 Like

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