Skip to content

Commit be03278

Browse files
scoobyzhangsothawo
authored andcommitted
IndexQuery's id get ignored in bulkIndexOperation.
Original Pull Request #2407 Closdes #2405 (cherry picked from commit 4d7d095)
1 parent 4bf1435 commit be03278

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/main/java/org/springframework/data/elasticsearch/client/elc/RequestConverter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
* @author Peter-Josef Meisch
133133
* @author Sascha Woo
134134
* @author cdalxndr
135+
* @author scoobyzhang
135136
* @since 4.4
136137
*/
137138
class RequestConverter {
@@ -476,7 +477,7 @@ public IndexRequest<?> documentIndexRequest(IndexQuery query, IndexCoordinates i
476477
Object queryObject = query.getObject();
477478

478479
if (queryObject != null) {
479-
String id = !StringUtils.hasText(query.getId()) ? getPersistentEntityId(queryObject) : query.getId();
480+
String id = StringUtils.hasText(query.getId()) ? query.getId() : getPersistentEntityId(queryObject);
480481
builder //
481482
.id(id) //
482483
.document(elasticsearchConverter.mapObject(queryObject));
@@ -528,7 +529,7 @@ private IndexOperation<?> bulkIndexOperation(IndexQuery query, IndexCoordinates
528529
Object queryObject = query.getObject();
529530

530531
if (queryObject != null) {
531-
String id = StringUtils.hasText(query.getId()) ? getPersistentEntityId(queryObject) : query.getId();
532+
String id = StringUtils.hasText(query.getId()) ? query.getId() : getPersistentEntityId(queryObject);
532533
builder //
533534
.id(id) //
534535
.document(elasticsearchConverter.mapObject(queryObject));
@@ -569,7 +570,7 @@ private CreateOperation<?> bulkCreateOperation(IndexQuery query, IndexCoordinate
569570
Object queryObject = query.getObject();
570571

571572
if (queryObject != null) {
572-
String id = StringUtils.hasText(query.getId()) ? getPersistentEntityId(queryObject) : query.getId();
573+
String id = StringUtils.hasText(query.getId()) ? query.getId() : getPersistentEntityId(queryObject);
573574
builder //
574575
.id(id) //
575576
.document(elasticsearchConverter.mapObject(queryObject));

src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchIntegrationTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
* @author Peer Mueller
129129
* @author Sijia Liu
130130
* @author Haibo Liu
131+
* @author scoobyzhang
131132
*/
132133
@SpringIntegrationTest
133134
public abstract class ElasticsearchIntegrationTests {
@@ -215,6 +216,26 @@ public void shouldThrowDataAccessExceptionIfDocumentDoesNotExistWhileDoingPartia
215216
assertThatThrownBy(() -> operations.update(sampleEntity)).isInstanceOf(DataAccessException.class);
216217
}
217218

219+
@Test // #2405
220+
public void shouldNotIgnoreIdFromIndexQuery() {
221+
String indexName = indexNameProvider.indexName();
222+
IndexCoordinates indexCoordinates = IndexCoordinates.of(indexName);
223+
224+
SampleEntity object1 = SampleEntity.builder().id("objectId1").message("objectMessage1").build();
225+
SampleEntity object2 = SampleEntity.builder().id("objectId2").message("objectMessage2").build();
226+
List<IndexQuery> indexQueries = Arrays.asList(
227+
new IndexQueryBuilder().withIndex(indexName).withId("idFromQuery1").withObject(object1)
228+
.withOpType(IndexQuery.OpType.INDEX).build(),
229+
new IndexQueryBuilder().withIndex(indexName).withId("idFromQuery2").withObject(object2)
230+
.withOpType(IndexQuery.OpType.CREATE).build());
231+
operations.bulkIndex(indexQueries, indexCoordinates);
232+
233+
boolean foundObject1 = operations.exists("idFromQuery1", indexCoordinates);
234+
assertThat(foundObject1).isTrue();
235+
boolean foundObject2 = operations.exists("idFromQuery2", indexCoordinates);
236+
assertThat(foundObject2).isTrue();
237+
}
238+
218239
@Test
219240
public void shouldThrowDataAccessExceptionIfDocumentDoesNotExistWhileDoingPartialUpdate() {
220241

0 commit comments

Comments
 (0)