Home > OS >  Elasticsearch filter by epoch_millis not work
Elasticsearch filter by epoch_millis not work

Time:01-30

I have index with mapping for property "key.lastEvent"

{
  "mappings": {
    "_doc": {
      "properties": {
        "key": {
          "properties": {
            "lastEvent": {
              "type": "date"

My data looks like this:

"hits" : [
      {
        "_index" : "stat-index",
        "_type" : "_doc",
        "_id" : "07f8d7bc3c4846e359e3122c411619f4",
        "_score" : 0.0,
        "_source" : {
          "id" : "07f8d7bc3c4846e359e3122c411619f4",
          "timestamp" : "2021-12-08T00:00:00 03:00",
          "key" : {
            "lastEvent" : "2021-12-08T00:00:00 03:00",
            "id" : "07f8d7bc3c4846e359e3122c411619f4"
          },
          "count" : 20
        }
      }
]

And I want to filter it like this (actually it's filter from grafana, so I can't adjust it):

GET stat-index/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "key.lastEvent": {
              "gte": 1607288400000,
              "lte": 1607461199000,
              "format": "epoch_millis"
            }
          }
        }
      ]
    }
  }
}

And it returns 0 hits. But if I use filter with another date format

GET stat-index/_search
{
  "query": {
    "bool": {
      "filter": [
        {
           "range": {
            "key.lastEvent": {
              "gte": "2021-12-06T00:00:00.000Z",
              "lte": "2021-12-08T00:00:00.000Z",
              "format": "date_time"
            }
          }
        }
      ]
    }
  }
}

It works as expected. So... It's a problem with my mapping? How can I force first variant to work?

CodePudding user response:

There is no problem with your query. I think you wrote the wrong epoch value.

  • The epoch value of 1607288400000 in your query
    • is not 2021-12-06T00:00:00.000Z
    • but 2020-12-06T00:00:00.000Z.
  • The epoch value of 1607461199000 in your query
    • is not 2021-12-08T00:00:00.000Z
    • but 2020-12-08T00:00:00.000Z.
  •  Tags:  
  • Related