Milad.N
Milad.N

Reputation: 2329

Valid value range(not query range) for date format in Elasticsearch

What is the valid value range? For example, is it like Epoch Timestamp or we could add values like 1536-22-05?

What have I tried so far?

Well it's not a matter of trying. I've just started to work with elasticsearch, and I'm working on my schema. For the date part, I was wondering if it would compare dates which were for hundreds of years ago, and no matter how much I searched I didn't find a straight answer. So I felt like there should be an answer to this question somewhere, thus I asked it here, just for the sake of having it somewhere.

To answer the above question I added the following values:

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 1,
      "hits": [
         {
            "_index": "pezeshkyab",
            "_type": "test",
            "_id": "1",
            "_score": 1,
            "_source": {
               "mydate": "1369-05-09"
            }
         },
         {
            "_index": "pezeshkyab",
            "_type": "test",
            "_id": "2",
            "_score": 1,
            "_source": {
               "mydate": "1379-05-09"
            }
         },
         {
            "_index": "pezeshkyab",
            "_type": "test",
            "_id": "3",
            "_score": 1,
            "_source": {
               "mydate": "1990-05-09"
            }
         }
      ]
   }
}

and then executed this query:

POST /pezeshkyab/test/_search
{
    "query": {
        "range": {
            "mydate": {
                "gt": "1359-01-01",
                "lt": "1399-12-12"
            }
        }
    }
}

and got this:

{
   "took": 74,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 1,
      "hits": [
         {
            "_index": "pezeshkyab",
            "_type": "test",
            "_id": "1",
            "_score": 1,
            "_source": {
               "mydate": "1369-05-09"
            }
         },
         {
            "_index": "pezeshkyab",
            "_type": "test",
            "_id": "2",
            "_score": 1,
            "_source": {
               "mydate": "1379-05-09"
            }
         }
      ]
   }
}

So the answer is pretty obvious, I just wanted to put the answer somewhere so that future searches by other people would yield some result.

Upvotes: 0

Views: 58

Answers (1)

jhilden
jhilden

Reputation: 12439

The problem is most likely that your "mydate" field is not being stored in ES as a date.

Here is a sample that explicitly sets "f2" as a date in ES, this will allow for proper querying/filtering.

PUT test

PUT /test/_mapping/type1
{
    "type1" : {
        "properties" : {
            "f1" : {"type" : "string"},
            "f2" : {"type" : "date"}
        }
    }
}

--verify the mapping
GET test/type1/_mapping

POST test/type1
{
  "f1": "record 1",
  "f2": "2000-01-01"
}

POST test/type1
{
  "f1": "record 2",
  "f2": "1500-01-01"
}

GET test/type1/_search
{
  "query": {
    "filtered": {
      "filter": {
        "range": {
          "f2": {
            "gte": "1500-01-01",
            "lte": "1999-01-01"
          }
        }
      }
    }
  }
}

With this sample code "record 2" is correctly returned while "record 1" is excluded.

Upvotes: 1

Related Questions