ElasticSearch date range

Crista23 picture Crista23 · Nov 7, 2014 · Viewed 77.5k times · Source

I have the following query:

{
   "query": {
     "query_string": {
       "query": "searchTerm",
        "default_operator": "AND"
       }
     },
    "facets": {
      "counts": {
        "date_histogram": {
         "field": "firstdate",
         "interval": "hour"
         }
    }
}

and I would like to add a date range to it, so as to retrieve values for the field firstdate which are within a specific from/to interval. Any suggestions on how to do it? Many thanks!

Answer

Olly Cruickshank picture Olly Cruickshank · Nov 7, 2014

you just need to add a range filter to your query:

{
"query":{
  "filtered": {
     "query": {
       "query_string": {"query": "searchTerm", "default_operator": "AND" }
     },
      "filter" : {
         "range": {"firstdate": {"gte": "2014-10-21T20:03:12.963","lte": "2014-11-24T20:03:12.963"}}
    }
  }
},
"facets": {
 "counts": {
    "date_histogram": {
       "field": "firstdate",
       "interval": "hour"
      }
    }
  }
}