currently, we are forcing the _id of the Elasticsearch documents by setting the id field. This simplifies the design, as it allows to perform updates and/or creation of new documents without previously knowing whether a document is already present in the index.
For example, this is the class representing the document:
@Data
@Document(indexName = "person")
public class Person {
@Id
private String id;
private String name;
}
and this is the method for bulk create or update:
public void bulkCreateOrUpdate(final List<Person> personList) {
List<UpdateQuery> updateList = new ArrayList<>();
for (Person person : personList) {
UpdateQuery updateQuery = UpdateQuery.builder(person.getId())
.withDocument(this.operations.getElasticsearchConverter().mapObject(person))
.withDocAsUpsert(true)
.build();
updateList.add(updateQuery);
}
this.operations.bulkUpdate(updateList,
this.operations.getIndexCoordinatesFor(this.entityClass));
}
When saved, this is what it produces:
{
"_index": "person",
"_type": "_doc",
"_id": "10001",
"_score": 1.0,
"_source": {
"_class": "com.app.Person",
"id": "10001",
"name": "Miguelito",
}
How/what to modify the code so that the _id is autogenerated during saving? Is there an annotation that can be used on id field or is there a need to modify the bulkCreateOrUpdate implementation? The expected output is this:
{
"_index": "person",
"_type": "_doc",
"_id": "qwrqwr3242frw",
"_score": 1.0,
"_source": {
"_class": "com.app.Person",
"name": "Miguelito",
}
Please advice.
Thanks!