Search Date Field value in ElasticSearch without time

I have one date field in my data as

"type": "date", "format": "dateOptionalTime" 

Now My Date Field and Value -

 "INITIAL_EXTRACT_DATE" : "2015-04-02T06:47:57.78+05:30" 

During the search, I search based on only the date, which is "2015-04-02". but I get 0 result.

Can anyone suggest how to search for the exact date and any date.

Now I'm trying with this -

For the exact date -

 "term": { "IH_PT_DSC": { "value": "2015-04-02" } } 

For any date -

 "terms": { "IH_PT_DSC": [ "2015-04-02", "2015-04-03", "2015-04-03" ] } 
+5
source share
2 answers

You can use the range filter for this, using the same date in gte / lte and a format , where you specify only part of the date (i.e. do not consider the time part)

 { "constant_score": { "filter": { "range" : { "IH_PT_DSC" : { "gte": "2015-04-02", "lte": "2015-04-02", "format": "yyyy-MM-dd" } } } } } 

If you need to specify multiple dates, you can also do this easily.

 { "constant_score": { "filter": { "range" : { "IH_PT_DSC" : { "gte": "2015-04-01", "lte": "2015-04-03", "format": "yyyy-MM-dd" } } } } } 

Finally, if you need to query for intermediate date ranges, just use the bool/should filter:

 { "constant_score": { "filter": { "bool": { "should": [ { "range": { <--- interval 1 "IH_PT_DSC": { "gte": "2015-04-01", "lte": "2015-04-03", "format": "yyyy-MM-dd" } } }, { "range": { <--- interval 2 "IH_PT_DSC": { "gte": "2015-04-05", "lte": "2015-04-08", "format": "yyyy-MM-dd" } } }, { "range": { <--- interval 3 "IH_PT_DSC": { "gte": "2015-04-10", "lte": "2015-04-12", "format": "yyyy-MM-dd" } } } ] } } } } 
+3
source

You really should use a date range query and a format parameter (but much simpler, as explained in the previous answer)

For an exact date

 { "range": { "IH_PT_DSC": { "value": { "gte": "2015-04-02||/d", "lte": "2015-04-02||/d" } } } } 

For any date - use a Boolean filter and "must" different blocks together

 { "bool": { "should": [{ "range": { "IH_PT_DSC": { "value": { "gte": "2015-04-02||/d", "lte": "2015-04-02||/d" } } } }, { "range": { "IH_PT_DSC": { "value": { "gte": "2015-06-07||/d", "lte": "2015-06-07||/d" } } } } ] } } 
+1
source

All Articles