我们可以使用 Spring-data-elasticsearch 获取现有文档的元数据“_version”吗?

Can we fetch the metadata `_version` of existing document using Spring-data-elasticsearch?

我正在实现一项功能,我需要在索引现有文档之前比较其元数据版本,并且我的对象中有一个版本属性,它不能与元数据版本相同。我可以在代码中显式获取元数据版本,以便进行比较吗?我正在使用 spring-data-elasticsearch。我将不胜感激任何帮助。

您只需要在您的实体中添加一个 属性 并用 @Version 注释,例如:

@Document(indexName = "person")
public class Person {

    @Id
    @Nullable
    private Long id;

    @Nullable @Version
    Long version;

    // other properties, getter and setter
}

当使用 Spring Data Elasticsearch 读取实体时,此 属性 - 必须是 Long - 将设置为从 Elasticsearch 返回的值。