Spring Data Elasticsearch id vs. _id

wxkevin picture wxkevin · May 17, 2016 · Viewed 9.8k times · Source

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

My DAO is similar to:

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 the object to ES using a repository the _id metadata field gets populated correctly. The getter and setter methods for the id field correctly return the value of the _id metadata field. But the id field within the _source field is null.

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

Answer

Val picture Val · May 17, 2016

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

What is happening is that if you generate your own IDs and call setId("yourid"), then Spring Data ES will use it as the value for the _id of your document and also persist that value into the _source.id field. Which means that _source.id will not be null.

If you don't call setId(), then _source.id will be null and ES will generate its own ID. When you then call getId(), Spring Data ES will make sure to return you the value of the _id field and not _source.id since it's annotated with @Id

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