Reputation: 59
I understand that Spring Data maps by default id field to the elastic _id. Is this some way to override this behavior? I have a document with id field that came from outside and I don't want to change its name because of backward compatibility. But I have to support the scenario when I have multiple docs with the same id (it's not unique field) So I want to let elastic to generate _id field to be unique and set my own id Any ideas on how to do that?
Upvotes: 0
Views: 1426
Reputation: 19431
Spring Data Elasticsearch identifies the id property by either the @Id
annotation or by the property name if it is id or document. So you get a conflict when having both the annotation and a field with one of these names.
In Spring Data Elasticsearch 4.0, you can change the name of the id property:
@Document(indexName = "sample-entities")
public class SampleEntity {
@Id
private String autogeneratedId; // (1)
@Field(name = "no-id") // (2)
private String id;
@Field(type = FieldType.Text)
private String message;
// getter/setter omitted for brevity
}
(1) this field will be used for the _id in Elasticsearch and will get an autogenerated value from Elasticsearch.
(2) this field will be named no-id in Elasticsearch and will not be identified as id field because of the renaming in the @Field
annotation.
If you are using Spring Data Elasticsearch 3.2, you need to ElasticsearchEntityMapper
instead of the default Jackson based one.
Upvotes: 4