Return the most recent record from ElasticSearch index

后端 未结 8 1607
执笔经年
执笔经年 2020-12-13 23:01

I would like to return the most recent record (top 1) from ElasticSearch index similar to the sql query below;

SELECT         


        
8条回答
  •  生来不讨喜
    2020-12-13 23:52

    Do you have _timestamp enabled in your doc mapping?

    {
        "doctype": {
            "_timestamp": {
                "enabled": "true",
                "store": "yes"
            },
            "properties": {
                ...
            }
        }
    }
    

    You can check your mapping here:

    http://localhost:9200/_all/_mapping
    

    If so I think this might work to get most recent:

    {
      "query": {
        "match_all": {}
      },
      "size": 1,
      "sort": [
        {
          "_timestamp": {
            "order": "desc"
          }
        }
      ]
    }
    

提交回复
热议问题