如何在elasticsearch中获得最大的_id值? [英] How to get the maximum _id value in elasticsearch?
问题描述
我们使用自定义的 _id
字段,该字段为 long
值.我想获得最大的_id值.我正在搜索的是-
We use a custom _id
field which is a long
value. I would like to get the max _id value. The search I am making is -
{
"stored_fields": [
"_id"
],
"query": {
"match_all": {}
},
"sort": {
"_id": "desc"
},
"size": 1
}
但是我从ES 5.1 as-那里得到了错误-
But I get error back from ES 5.1 as-
"reason": {
"type": "illegal_argument_exception",
"reason": "Fielddata access on the _uid field is disallowed"
}
所以,我该如何获取 _id
的最大值.我并不是真的想将 _id
的副本存储在文档中只是为了获得最大值.
So, how do I go about getting the max value of _id
. I don't really want to store copy of _id
inside the doc just to get the max value.
推荐答案
您应按 _uid
字段而不是 _id
字段进行排序. _id
字段不可用于排序( https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-id-field.html ).
You should sort by the _uid
field not by the _id
field. The _id
field is not accessible for sorting (https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-id-field.html).
{
"stored_fields": [
"_id"
],
"query": {
"match_all": {}
},
"sort": {
"_uid": "desc"
},
"size": 1
}
这篇关于如何在elasticsearch中获得最大的_id值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!