How to get the maximum _id value in elasticsearch?

核能气质少年 提交于 2021-01-28 02:54:59

问题


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
}

But I get error back from ES 5.1 as-

"reason": {
              "type": "illegal_argument_exception",
              "reason": "Fielddata access on the _uid field is disallowed"
}

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.


回答1:


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
}


来源:https://stackoverflow.com/questions/45427887/how-to-get-the-maximum-id-value-in-elasticsearch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!