# Filtering by date range or by field value

**URL:** <https://discuss.elastic.co/t/filtering-by-date-range-or-by-field-value/85477>\
**Category:** Elasticsearch\
**Created:** [May 11, 2017, 8:35pm UTC](https://discuss.elastic.co/t/filtering-by-date-range-or-by-field-value/85477 "2017-05-11T20:35:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cpatdowling](https://avatars.discourse-cdn.com/v4/letter/c/5daacb/32.png) [@cpatdowling](https://discuss.elastic.co/u/cpatdowling)\
**Post date:** [May 11, 2017, 8:35pm UTC](https://discuss.elastic.co/t/filtering-by-date-range-or-by-field-value/85477/1 "2017-05-11T20:35:32Z")

</div>

I'm having some difficulty constructing a query that matches a field value, and then filters that query by a date range. Conversely, I could query the date range and then filter by field value but I expect that the latter will return more results on the initial query pre-filter. Fundamentally my question rests on how to incorporate a filter with a date range query; I haven't found any luck in the documentation and the few examples that exist on the web are from deprecated versions of ES.

If anyone can point me to the relevant portion of the documentation, that would be greatly appreciated---having read through [range](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html) and [bool](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html) syntax.

I'm using version 5.4.0, using the python API. My latest query format attempt

```
query = {"query": { 
            "bool": {
                "must": {
                    "term": {
                        "elementkey": integer_value
                    }
                }
            },
            "filter": {
                "range": {
                    "transactiondatetime": {
                        "gt": start_date
                        "lt": end_date
                    }
                }
            }
        }
    }

```

and the resulting error message:

> TransportError(400, {u'line': 1, u'root\_cause': [{u'reason': u'no [query] registered for [filter]', u'type': u'parsing\_exception', u'line': 1, u'col': 22}], u'type': u'parsing\_exception', u'reason': u'no [query] registered for [filter]', u'col': 22})

---

<div class="post-metadata">

**Author:** ![cpatdowling](https://avatars.discourse-cdn.com/v4/letter/c/5daacb/32.png) [@cpatdowling](https://discuss.elastic.co/u/cpatdowling)\
**Post date:** [May 12, 2017, 12:54am UTC](https://discuss.elastic.co/t/filtering-by-date-range-or-by-field-value/85477/2 "2017-05-12T00:54:43Z")

</div>

After some continued trial and error and staring hopelessness in the mouth, I believe I found the correct query syntax. Posting for rookie posterity.

```
query = {"query": { 
            "bool": {
                "must": {
                    "term": {
                        "elementkey": integer_value
                        }
                    },
            "filter": {
                "range": {
                    "transactiondatetime": {
                        "gt": start_date,
                        "lt": end_date
                    }
                }
            }   
            }
        }
    }
```

---

<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:** [June 9, 2017, 1:07am UTC](https://discuss.elastic.co/t/filtering-by-date-range-or-by-field-value/85477/3 "2017-06-09T01:07:23Z")

</div>

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