19

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?

Florian
  • 1,827
  • 4
  • 30
  • 62
wxkevin
  • 1,634
  • 2
  • 25
  • 44
  • 1
    Are you letting ES generate its own IDs or are you providing your own IDs? i.e. are you at all calling `MyDao.setId("abcdxyz")` ? – Val May 17 '16 at 13:05
  • 1
    I am letting ES generate its own IDs. Can I use auto-generation of IDs to get the ID to show up in both fields? – wxkevin May 17 '16 at 13:20
  • @wxkevin A different question, but can you share your pom. I am stuck with errors while trying to use spring-data-elasticsearch 2 : [question-link](http://stackoverflow.com/questions/37047845/cannot-use-custom-autoconfiguration-with-spring-data-elasticsearch/37107628) – Utkarsh Mishra May 18 '16 at 12:29
  • I can't copy and paste but I can tell you the dependencies and version numbers: spring-core (4.2.5.RELEASE), spring-webmvc (4.2.5.RELEASE), javax.servlet-api (3.1.0), elasticsearch (2.2.0), spring-data-elasticsearch (2.0.1.RELEASE), jcl-over-slf4j (1.7.16), logback-classic (1.1.5). If you need something else specific please feel free to ask. – wxkevin May 18 '16 at 12:45

1 Answers1

21

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.

Val
  • 207,596
  • 13
  • 358
  • 360
  • So you are saying there is no way to get Elasticsearch to generate the ID and use that ID in my id field? – wxkevin May 17 '16 at 14:13
  • 1
    Yes, ES IDs are stored in the `_id` field which is different from `_source.id`(or whatever name you want to give that field). The best approach is to generate your own IDs and call `setId()` – Val May 17 '16 at 14:15
  • 3
    I used @JsonIgnore in id attribute and the field was not created in ES! – Wendel Mar 28 '17 at 13:40