Return the most recent record from ElasticSearch index

后端 未结 8 1577
执笔经年
执笔经年 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:57

    I used @timestamp instead of _timestamp

    {
        'size' : 1,
        'query': {
            'match_all' : {}
                },
        "sort" : [{"@timestamp":{"order": "desc"}}]
    }
    
    0 讨论(0)
  • 2020-12-14 00:04

    Since this question was originally asked and answered, some of the inner-workings of Elasticsearch have changed, particularly around timestamps. Here is a full example showing how to query for single latest record. Tested on ES 6/7.

    1) Tell Elasticsearch to treat timestamp field as the timestamp

    curl -XPUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d '{"mappings":{"message":{"properties":{"timestamp":{"type":"date"}}}}}'
    

    2) Put some test data into the index

    curl -XPOST "localhost:9200/my_index/message/1" -H 'Content-Type: application/json' -d '{ "timestamp" : "2019-08-02T03:00:00Z", "message" : "hello world" }'
    curl -XPOST "localhost:9200/my_index/message/2" -H 'Content-Type: application/json' -d '{ "timestamp" : "2019-08-02T04:00:00Z", "message" : "bye world" }'
    

    3) Query for the latest record

    curl -X POST "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d '{"query": {"match_all": {}},"size": 1,"sort": [{"timestamp": {"order": "desc"}}]}'
    

    4) Expected results

    {
       "took":0,
       "timed_out":false,
       "_shards":{
          "total":5,
          "successful":5,
          "skipped":0,
          "failed":0
       },
       "hits":{
          "total":2,
          "max_score":null,
          "hits":[
             {
                "_index":"my_index",
                "_type":"message",
                "_id":"2",
                "_score":null,
                "_source":{
                   "timestamp":"2019-08-02T04:00:00Z",
                   "message":"bye world"
                },
                "sort":[
                   1564718400000
                ]
             }
          ]
       }
    }
    
    0 讨论(0)
提交回复
热议问题