Return the most recent record from ElasticSearch index

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

    the _timestamp didn't work out for me,

    this query does work for me:

    (as in mconlin's answer)

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

    Could be trivial but the _timestamp answer didn't gave an error but not a good result either...

    Hope to help someone...

    (kibana/elastic 5.0.4)

    S.

    0 讨论(0)
  • 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".

    0 讨论(0)
  • 2020-12-13 23:49

    For information purpose, _timestamp is now deprecated since 2.0.0-beta2. Use date type in your mapping.

    A simple date mapping JSON from date datatype doc:

    {
      "mappings": {
         "my_type": {
            "properties": {
              "date": {
              "type": "date" 
            }
          }
        }
      }
    }
    

    You can also add a format field in date:

    {
      "mappings": {
        "my_type": {
          "properties": {
            "date": {
              "type":   "date",
              "format": "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
            }
          }
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-13 23:50

    Get the Last ID using by date (with out time stamp)

    Sample URL : http://localhost:9200/deal/dealsdetails/
    Method : POST

    Query :

    {
      "fields": ["_id"],
      "sort": [{
          "created_date": {
            "order": "desc"
          }
        },
        {
          "_score": {
            "order": "desc"
          }
        }
      ],
      "size": 1
    }
    

    result:

    {
      "took": 4,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 9,
        "max_score": null,
        "hits": [{
          "_index": "deal",
          "_type": "dealsdetails",
          "_id": "10",
          "_score": 1,
          "sort": [
            1478266145174,
            1
          ]
        }]
      }
    }
    
    0 讨论(0)
  • 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"
          }
        }
      ]
    }
    
    0 讨论(0)
  • 2020-12-13 23:52

    You can use sort on date field and size=1 parameter. Does it help?

    0 讨论(0)
提交回复
热议问题