Search code examples
elasticsearchdate-range

ElasticSearch date range


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!


Solution

  • 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"
          }
        }
      }
    }