# Post filter on date range appears to improve performance

**URL:** https://discuss.elastic.co/t/post-filter-on-date-range-appears-to-improve-performance/68694
**Category:** Elasticsearch
**Created:** [December 12, 2016, 10:19am UTC](https://discuss.elastic.co/t/post-filter-on-date-range-appears-to-improve-performance/68694 "2016-12-12T10:19:42Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![manast](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manast/32/13705_2.png) [@manast](https://discuss.elastic.co/u/manast)
#### Post date: [December 12, 2016, 10:19am UTC](https://discuss.elastic.co/t/post-filter-on-date-range-appears-to-improve-performance/68694/1 "2016-12-12T10:19:42Z")

</div>

Hello,  
I am trying to improve the performance of date range queries and came up with the following alternatives. Field "updatedAt" is a date field in the following

Which alternative would you recommend?

Curiously query (3) that uses post\_filter appears much quicker.

Post\_filter is guaranteed to execute after the query. Hence the number of documents the expensive date range has to go through is much smaller. Can that be the explanation? And what about the recommendation in [https://www.elastic.co/guide/en/elasticsearch/guide/current/\_post\_filter.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/_post_filter.html) that says "Don't use post filter for searches" ?

(1)

```
{
"query": {
    "bool": {
        "filter": [
            {
                "term": {
                    "customerId": 1005
                }
            },
            {
                "term": {
                    "isChecked": false
                }
            }
        ],
        "must": [
            {
                "range": {
                    "updatedAt": {
                        "from": null,
                        "to": "now",
                        "include_lower": true,
                        "include_upper": false
                    }
                }
            }
        ]
    }
},
"sort": [
    {
        "updatedAt": {}
    }
]}

```

(2)

```
{
"query": {
    "bool": {
        "filter": [
            {
                "term": {
                    "customerId": 1005
                }
            },
            {
                "term": {
                    "isChecked": false
                }
            },
            {
                "range": {
                    "updatedAt": {
                        "from": null,
                        "to": "now",
                        "include_lower": true,
                        "include_upper": false
                    }
                }
            }
        ]
    }
},
"sort": [
    {
        "updatedAt": {}
    }
]}

```

(3)

```
{
"query": {
    "bool": {
        "filter": [
            {
                "term": {
                    "customerId": 1005
                }
            },
            {
                "term": {
                    "isChecked": false
                }
            }
        ]
    }
},
"post_filter": {
    "bool": {
        "must": [
            {
                "range": {
                    "updatedAt": {
                        "from": null,
                        "to": "now",
                        "include_lower": true,
                        "include_upper": false
                    }
                }
            }
        ]
    }
},
"sort": [
    {
        "updatedAt": {}
    }
]}
```

---

<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: [January 9, 2017, 10:20am UTC](https://discuss.elastic.co/t/post-filter-on-date-range-appears-to-improve-performance/68694/2 "2017-01-09T10:20:16Z")

</div>

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