如何在 ElasticSearch 中按 _version 对数据进行排序

How to sort data by _version in ElasticSearch

因为我可以使用

这样的分数对数据进行排序
    {
  "version":true,
  "_source":false,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ], 
  "query": {
    "match_all": {}

  }
}

请告诉我如何对 _version 执行相同的操作。默认情况下,字段 _version 不支持 Fielddata。所以可能是我遗漏了一些东西。 是否有任何特定的设置可以查询版本? 请帮忙!

你不能这样做,通常你也不必这样做。 看到这个线程: https://discuss.elastic.co/t/filter-by--version-and-show--version-in-elasticsearch-query/22024/2

While using the _version might seem to work in certain cases, I would recommend to never use it for anything else than optimistic locking of updates. In particular, versions do not carry any meaning: they might look like the number of times a document has been modified but it is not always the case (for instance if you create a new document which has the same ID as a document that you just deleted, the version number of the new document will not be 1), and more importantly it is an implementation detail, this behaviour might change in the future.

_version 字段未编入索引,因此您不能在查询中使用它。
您可以创建自定义版本字段并手动处理它。