# Filter instead of query for improved performance?

**URL:** <https://discuss.elastic.co/t/filter-instead-of-query-for-improved-performance/106751>\
**Category:** Elasticsearch\
**Created:** [November 7, 2017, 8:03pm UTC](https://discuss.elastic.co/t/filter-instead-of-query-for-improved-performance/106751 "2017-11-07T20:03:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lemongrab](https://avatars.discourse-cdn.com/v4/letter/l/13edae/32.png) [@lemongrab](https://discuss.elastic.co/u/lemongrab)\
**Post date:** [November 7, 2017, 8:03pm UTC](https://discuss.elastic.co/t/filter-instead-of-query-for-improved-performance/106751/1 "2017-11-07T20:03:19Z")

</div>

Upon reading this thread here [https://stackoverflow.com/questions/14595988/queries-vs-filters](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](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.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [November 8, 2017, 6:00am UTC](https://discuss.elastic.co/t/filter-instead-of-query-for-improved-performance/106751/2 "2017-11-08T06:00:12Z")

</div>

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"
            }
          }
        }
      ]
    }
  }
}
```

---

<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:** [December 6, 2017, 6:00am UTC](https://discuss.elastic.co/t/filter-instead-of-query-for-improved-performance/106751/3 "2017-12-06T06:00:48Z")

</div>

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