Summary
Recently we upgraded to Spring Data Elasticsearch 4.x. Part of this major release meant that Jackson is no longer used to convert our domain objects to json (using MappingElasticsearchConverter
instead) [1]. This means we are now forced to add a new id
field to all our documents.
Previously we had domain objects like this:
import org.springframework.data.annotation.Id;
public ESDocument {
@Id
private String id;
private String field1;
@JsonIgnore
public String getId() {
return id;
}
public String getField1() {
return field1;
}
Which resulted in documents like this in ES:
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "d5bf7b5c-7a44-42f9-94d6-d59fe3988482",
"_score" : 1.0,
"_source" : {
"field1" : "blabla"
}
}
Note that:
- The
@JsonIgnore
annotation used to ensure that we were not required to have aid
field in the_source
. - We are setting the document id ourselves and it ends up in
_id
.
Problem
With Spring Data Elastic 4.x the @JsonIgnore
annotation is no longer respected which means we are now forced to have an id
field in the _source
as shown below:
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "d5bf7b5c-7a44-42f9-94d6-d59fe3988482",
"_score" : 1.0,
"_source" : {
"id": "d5bf7b5c-7a44-42f9-94d6-d59fe3988482",
"field1" : "blabla"
}
}
Questions
- Is it no longer possible to omit the duplication of the identifier of the document (i.e. in the
_id
andid
fields)? If so how? (Note we already tried@org.springframework.data.annotation.Transient
which does not work because spring-data-elastic then thinks our document does not have an id). - Was our previous approach of suppressing the
id
field in_source
incorrect or problematic?
Versions
java: 1.8.0_252
elasticsearch: 7.6.2
spring-boot: 2.3.1.RELEASE
spring-data-elastic: 4.0.1.RELEASE
References
[1] - https://spring.io/blog/2020/05/27/what-s-new-in-spring-data-elasticsearch-4-0
id
field will always be present in the document. However, if you don't want it then remove it from your mappings instead? or you can use queries to fetch documents and can excludeid
field. Ref: elastic.co/guide/en/elasticsearch/reference/current/… – Krieger_source.id
field in our documents. Thanks for the link. – Chisel