elasticsearch query dates by range

15,423

Your field is a string, the format property is ignored. You should change your mapping and use the date type. Have a look here to see the core types available in elasticsearch.

I would use a filter instead of a query. It will be cached, thus faster. The following is an example for the last 7 days:

{
    "filter" : {
        "range" : {
            "PublishTime" : {
                "from" : "20130505T000000",
                "to" : "20131105T235959"
            }
        }
    }
}

Note that if you use the filter like this it's going to be the same filter the whole day, thus you would make good use of the cache.

Share:
15,423
cdietschrun
Author by

cdietschrun

Updated on June 04, 2022

Comments

  • cdietschrun
    cdietschrun almost 2 years

    My elasticsearch has data, particularly something like this for dates:

    {
      "startTime": {
        "type": "string",
        "format": "yyyy/MM/dd",
        "index": "analyzed",
        "analyzer": "keyword"
      }
    }
    

    I am adding a date range picker and want to use the dates picked to go query elasticsearch for data with startTime inside this range chosen. I'm not sure how to structure this query to elasticsearch, or if it will even work with this being a string field (I can potentially change it, though).

    can anyone help me here?