Return the most recent record from ElasticSearch index

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

    If you are using python elasticsearch5 module or curl:

    1. make sure each document that gets inserted has
      • a timestamp field that is type datetime
      • and you are monotonically increasing the timestamp value for each document
    2. from python you do

      es = elasticsearch5.Elasticsearch('my_host:my_port')
      es.search(
          index='my_index', 
          size=1,
          sort='my_timestamp:desc'
          )
      

    If your documents are not inserted with any field that is of type datetime, then I don't believe you can get the N "most recent".

提交回复
热议问题