0
votes

Trying to do 'exact' date match using range query but without success. I have agreement_start_date field with below mapping

"agreement_start_date" : {
     "type" : "date",
     "format" : "strict_date_optional_time||epoch_millis"
} 

I have indexed single document and it seems to be indexed fine.

curl "localhost:9200/cr/_search?pretty=true" -d ''

{
  "took" : 35,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "cr",
      "_type" : "agreement_index",
      "_id" : "570fe59b7f9e6f7f762129ed",
      "_score" : 1.0,
      "_source":{"agreement_start_date": "2016-04-14T18:46:51.268176+00:00"} 
  }
}

I want to get this record with simple query that match agreement_start_date with particular day.

{
  "query": {
    "bool": {
      "must": {
        "range": {
          "agreement_start_date": {
            "gte": "2016-04-14",
            "lte": "2016-04-14",
            "format": "yyyy-MM-dd"
          }
        }
      }
    }
  }
}

This query return 0 results. How to make query in elastic search which compare only provided parts of date?

2

2 Answers

1
votes

It seems that rounding date to nearest day do the job.

{
  "query": {
    "bool": {
      "must": {
        "range": {
          "agreement_start_date": {
            "gte": "2016-04-14||\d",
            "lte": "2016-04-14||\d",
            "format": "yyyy-MM-dd"
          }
        }
      }
    }
  }
}

This query return indexed document.

1
votes

I used this:

"range" : {
    "agreement_start_date" : {
        "gte": "2016-04-14",
        "lte": "2016-04-14",
        "format": "yyyy-MM-dd||yyyy-MM-dd",
        "time_zone": "Asia/Seoul"
    }
}