ElasticSeach - Sorting on dates

后端 未结 1 1800
独厮守ぢ
独厮守ぢ 2021-01-05 03:57

I have an elastic search index which I cannot setup every field with a mapping so dates are going in as strings...

Does anyone know how I would go about sorting on t

相关标签:
1条回答
  • 2021-01-05 04:28

    If the format of the date is known, you can add that format to the dynamic_date_formats (Check out this link) setting. When you index a new string field it will be converted to the date type which can be sorted in the normal way.

    Example:

    Create an index without properties:

    curl -XPUT http://localhost:9200/dates -d '
    {
        "dates" : {
            "dynamic_date_formats" : ["yyyy-MM-dd", "dd-MM-yyyy"],
            "properties" : {
            }
        }
    }'
    

    Index 2 documents:

    curl -XPUT 'http://localhost:9200/dates/dates/1' -d '
    {
        "arbitraryDate": "2013-01-01"
    }'
    
    curl -XPUT 'http://localhost:9200/dates/dates/2' -d '
    {
        "arbitraryDate": "2012-01-01"
    }'
    

    If you check the mapping you will see that the field is not a string:

    curl -XGET 'http://localhost:9200/dates/_mapping'
    

    result:

    {
      "dates": {
        "dates": {
          "properties": {
            "arbitraryDate": {
              "type": "date",
              "format": "dateOptionalTime"
            }
          }
        }
      }
    }
    

    Now you can sort easily:

    curl -XGET 'http://localhost:9200/dates/_search' -d '
    {
      "query": {
        "match_all": {}
      },
      "sort": [
        {
          "arbitraryDate": {
            "order": "asc"
          }
        }
      ]
    }'
    
    0 讨论(0)
提交回复
热议问题