Filtering by date range or by field value

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 and bool 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})

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
                    }
                }
            }   
            }
        }
    }
1 Like

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