fth
fth

Reputation: 2538

Elasticsearch date range query ignores month field

I've been using Elasticsearch as my data store and I got lots of documents in it. My problem is, I figured out that Elasticsearch ignores the month part of the mapping's date field.

Here is what I have in my index and my query, please tell me if I'm wrong:

curl -XPUT 'http://localhost:9200/tt6/' -d '{}'
curl -XPUT 'http://localhost:9200/tt6/tweet/_mapping' -d '{"tweet" : {"properties" : {"date" : {"type" : "date", "format": "YYYY-MM-DD HH:mm:ss" }}}}'
curl -XPUT 'http://localhost:9200/tt6/tweet/1' -d '{"date": "2014-02-14 04:00:45"}'

curl -XGET 'http://localhost:9200/tt6/_search' -d '
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "tweet.date": {
              "from": "2014-12-01 00:00:00",
              "to": "2014-12-30 00:00:00"
            }
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "from": 0,
  "size": 10,
  "sort": [],
  "facets": {}
}'

And my response is

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "tt6",
        "_type": "tweet",
        "_id": "1",
        "_score": 1,
        "_source": {
          "date": "2014-02-14 04:00:45",
          "name": "test"
        }
      }
    ]
  }
}

With the given date range there should be no response between the 1st of December 2014 and the 30th of December 2014, but Elasticsearch returns entries outside of the range. Why is that so?

Upvotes: 3

Views: 2451

Answers (1)

Paige Cook
Paige Cook

Reputation: 22555

Your date formatting is incorrect. Please change your mapping call to use yyyy-MM-dd HH:mm:ss and you will get the expected results.

curl -XPUT 'http://localhost:9200/tt6/tweet/_mapping' -d '{"tweet" : {"properties" : {"date" : {"type" : "date", "format": "yyyy-MM-dd HH:mm:ss" }}}}'

See Elasticsearch Date Range reference (specifically the date/format pattern section) for more details on the proper formatting values.

Upvotes: 2

Related Questions