Spring Elasticsearch vs. Data ID _id

I am using Spring Data Elasticsearch 2.0.1 with Elastic version 2.2.0.

My DAO is like:

import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName = "myIndex") public class MyDao { @Id private String id; public String getId() { return id; } public void setId(String id) { this.id = id; } <other fields, setters, getters omitted> } 

Saving an object in ES using the repository in the _id metadata _id filled correctly. The getter and setter methods for the id field correctly return the value of the _id metadata _id . But the id field in the _source field is null.

2 questions: 1) Why is the id null field? 2) Does it matter that the id field is null?

+7
java spring-data-elasticsearch elasticsearch
source share
1 answer

Since you are letting ES generate its own identifiers, i.e. you never call MyDao.setId("abcdxyz") , then _source cannot have a value in the id field.

What happens if you create your own identifiers and call setId("yourid") , then Spring Data ES will use it as the value for the _id your document, and also store this value in the _source.id field. This means that _source.id will not be empty.

If you do not call setId() , then _source.id will be null, and the ES will generate its own identifier. When you call getId() , Spring Data ES will definitely return the value of the _id field, not _source.id , since it is annotated with @Id

To answer your second question, it doesn't matter that the _source.id field is null ... until you need to reference it. Spring Data ES will always populate it when matching JSON documents with your Java entities, even if the base id field in ES is null.

+9
source share

All Articles