Closed
Description
Brief
IndexQuery's id
is ignored in elasticsearchTemplate.bulkIndex
Environment
Spring Data Elasticsearch Version: 5.0.0
JVM Version: corretto-17.0.5
Code to reproduce the problem
/*
theObject {
id: "objectId",
name: "objectName"
}
*/
List<IndexQuery> indexQueries = Collections.singletonList(
new IndexQueryBuilder()
.withIndex(indexName)
.withId("my-id")
.withObject(theObject)
.build()
);
elasticsearchTemplate.bulkIndex(indexQueries, BulkOptions.defaultOptions(), IndexCoordinates.of(indexName));
Problem
Expected behavior
After the execution of above codes, a document with _id: my-id
is created
{
"_id" : "my-id",
"_source" : {
"id" : "objectId",
"name" : "objectName"
}
}
Actual behavior
After the execution of above codes, a document with _id: randomValue
is created
{
"_id" : "THiFSoUBGkc0hG1MdJYU",
"_source" : {
"id" : "objectId",
"name" : "objectName"
}
}
Problem found in code
The code I've posted will reach RequestConverter.java#L531
String id = StringUtils.hasText(query.getId()) ? getPersistentEntityId(queryObject) : query.getId();
It seems that this logic is wrong, we should changed it to
String id = !StringUtils.hasText(query.getId()) ? getPersistentEntityId(queryObject) : query.getId();
I found this logic repeated 3 times in RequestConverter.java
, only L479
is correct
- RequestConverter.java#L479
String id = !StringUtils.hasText(query.getId()) ? getPersistentEntityId(queryObject) : query.getId();
- RequestConverter.java#L531
String id = StringUtils.hasText(query.getId()) ? getPersistentEntityId(queryObject) : query.getId();
- RequestConverter.java#L572
String id = StringUtils.hasText(query.getId()) ? getPersistentEntityId(queryObject) : query.getId();