Search code examples
javaspring-bootelasticsearchspring-data-elasticsearch

How to fetch version from search results using spring-data-elasticsearch


I am executing NativeSearchQuery in Springframework to execute search operation. I am trying to fetch "_version" value from response through Java API. The following is the sample request and response related to this use case

GET /my-index/_doc/foo-bar

{
  "_index" : "my-index",
  "_type" : "_doc",
  "_id" : "foo-bar",
  "_version" : 88,
  "_seq_no" : 169,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "_class" : "fooBar",
    "value" : 1250087,
    "creatdDateTime" : "20210203T124928.913Z",
    "ModifiedDateTime" : "20210203T124928.913Z"
  }
}

I am trying to fetch _version=88 from above response through JAVA API using NativeSearchQuery. How to get this value


Solution

  • When using Spring Data Elasticsearch, you need to have a property of type Long annotated with @Version. Spring Data Elasticsearch will then populate this property with the version value.