Elasticsearch如何检索最大ID

pro*_*014 8 elasticsearch

如何有效地检索一个类型索引的最大ID?

tor*_*nge 10

您还可以使用Max Aggregation(自1.3开始)获取文档中给定字段的最大值.

curl localhost:9200/myindex/mytype/_search -d
  '{
    "aggs" : {
      "max_id" : {
        "max" : { 
          "field" : "id"
        }
      }
    },
    "size":0
  }'
Run Code Online (Sandbox Code Playgroud)

将返回:

{
  "took":7,
  "timed_out":false,
  "_shards": {
    "total":6,
    "successful":6,
    "failed":0
  },
  "hits": {
    "total":22,
    "max_score":0.0,
    "hits": []
  },
  "aggregations": {
    "max_id": {
      "value": 27.0
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在我的情况下,最大ID为27.


Rot*_*mon 5

您想获取某个类型的顶部 _id 吗?

{
  "fields": [
    "_id"
  ],
  "query": {
    "match_all": {}
  },
  "sort": {
    "_id": "desc"
  },
  "size": 1
}
Run Code Online (Sandbox Code Playgroud)