从ElasticSearch索引返回最新记录

时间:2013-12-21 20:48:14

标签: elasticsearch

我想从ElasticSearch索引返回最近的记录(前1),类似于下面的sql查询;

SELECT TOP 1 Id, name, title 
FROM MyTable 
ORDER BY Date DESC;

可以这样做吗?

8 个答案:

答案 0 :(得分:56)

您的文档映射中是否启用了_timestamp

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

您可以在此处查看您的地图:

http://localhost:9200/_all/_mapping

如果是这样,我认为这可能会起到最新的作用:

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

答案 1 :(得分:9)

出于提供信息的目的,自2.0.0-beta2以来,_timestamp现已弃用。 在映射中使用date类型。

来自date数据类型doc的简单日期映射JSON:

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

您还可以在format中添加date字段:

{
  "mappings": {
    "my_type": {
      "properties": {
        "date": {
          "type":   "date",
          "format": "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        }
      }
    }
  }
}

答案 2 :(得分:5)

您可以在日期字段和sort参数上使用size=1。 这有帮助吗?

答案 3 :(得分:3)

按日期使用最后一个ID(带时间戳)

  

示例网址http://localhost:9200/deal/dealsdetails/
  方法:POST

查询:

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

结果:

{
  "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
      ]
    }]
  }
}

答案 4 :(得分:1)

如果您使用的是python elasticsearch5模块或curl:

  1. 确保要插入的每个文档都有
    • 日期时间类型的时间戳字段
    • 您正在单调增加每个文档的时间戳值
  2. 从python开始

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

如果您的文档未插入日期时间类型的任何字段,那么我认为您不会获得N“最新”。

答案 5 :(得分:1)

我用@timestamp代替了_timestamp

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

答案 6 :(得分:1)

自从最初提出并回答了这个问题以来,Elasticsearch的某些内部工作方式已经发生了变化,尤其是在时间戳周围。这是一个完整的示例,显示了如何查询单个最新记录。在ES 6/7上进行了测试。

1)告诉Elasticsearch将timestamp字段视为时间戳记

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

2)将一些测试数据放入索引

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)查询最新记录

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

4)预期结果

{
   "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
            ]
         }
      ]
   }
}

答案 7 :(得分:0)

_timestamp没有为我效劳,

此查询对我有用:

(如在mconlin的回答中)

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

可能是微不足道的但_timestamp的回答并没有给出错误,但也不是一个好的结果......

希望能帮助别人......

(kibana / elastic 5.0.4)

S上。