Skip to content

IndexQuery's id is ignored in elasticsearchTemplate.bulkIndex #2405

Closed
@scoobyzhang

Description

@scoobyzhang

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions