Nowis
Nowis

Reputation: 590

Elasticsearch: store & query dates before Epoch

I'm looking for a solution to be able to store and query dates before Epoch (1st January 1970).

For example, I'm trying to store into Elasticsearch's index the date December, 25 1969 00:00:00 +0100. I suppose it possible to store into into string or integer, but is there any solution to keep the field type date into Elasticsearch's mapping ?

Upvotes: 2

Views: 861

Answers (1)

Val
Val

Reputation: 217344

You can definitely store dates before the epoch, simply by storing the date string or negative numbers.

If your index mapping looks like this:

PUT epoch
{
  "mappings": {
    "properties": {
      "my_date": {
        "type": "date"
      }
    }
  }
}

Then you can index documents before the epoch, like this:

PUT epoch/_doc/1
{
  "my_date": -608400
}

PUT epoch/_doc/2
{
  "my_date": "1969-12-25T00:00:00"
}

When searching for document with a date before the epoch, both would be returned:

POST epoch/_search 
{
  "query": {
    "range": {
      "before": {
        "lt": "1970-01-01"
      }
    }
  }
}

Upvotes: 3

Related Questions