Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 502
Crista23 Avatar asked Nov 06 '14 23:11

Crista23


2 Answers

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"
      }
    }
  }
}
like image 90
Olly Cruickshank Avatar answered Sep 19 '22 11:09

Olly Cruickshank


Boolean query will work too,

 {
   "query" :{
      "bool" : {             
          "must" : {
              "range": {"firstdate": {"gte": "2014-10-21T20:03:12.963","lte": "2014-11-24T20:03:12.963"}}
          },

          "must" : {
            "query_string": {
              "query": "searchTerm",
              "default_operator": "AND"
            }
          }

      }
    },

   "facets": {
       "counts": {
          "date_histogram": {
             "field": "firstdate",
             "interval": "hour"
           }
        }
    }

 }
like image 37
maximus ツ Avatar answered Sep 18 '22 11:09

maximus ツ