From c1eec752b6bf0bc864d91237eac1db9db8293b1c Mon Sep 17 00:00:00 2001 From: Andriy Redko Date: Wed, 4 Dec 2024 14:37:49 -0500 Subject: [PATCH] Inconsistency between java client and OpenSearch update by query responses Signed-off-by: Andriy Redko --- .../data/client/osc/ResponseConverter.java | 4 +++ .../core/ElasticsearchIntegrationTests.java | 30 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/spring-data-opensearch/src/main/java/org/opensearch/data/client/osc/ResponseConverter.java b/spring-data-opensearch/src/main/java/org/opensearch/data/client/osc/ResponseConverter.java index c747fff..0e12f5f 100644 --- a/spring-data-opensearch/src/main/java/org/opensearch/data/client/osc/ResponseConverter.java +++ b/spring-data-opensearch/src/main/java/org/opensearch/data/client/osc/ResponseConverter.java @@ -489,6 +489,10 @@ public ByQueryResponse byQueryResponse(UpdateByQueryResponse response) { builder.withDeleted(response.deleted()); } + if (response.updated() != null) { + builder.withUpdated(response.updated()); + } + if (response.batches() != null) { builder.withBatches(Math.toIntExact(response.batches())); } diff --git a/spring-data-opensearch/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchIntegrationTests.java b/spring-data-opensearch/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchIntegrationTests.java index 6b28e1e..f745298 100755 --- a/spring-data-opensearch/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchIntegrationTests.java +++ b/spring-data-opensearch/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchIntegrationTests.java @@ -3669,6 +3669,36 @@ public void shouldDeleteDocumentForGivenQueryUsingParameters() { IndexCoordinates.of(indexName)); assertThat(searchHits.getTotalHits()).isEqualTo(0); } + + @Test // OS-GH-387 + public void shouldUpdateDocumentForGivenQueryUsingParameters() { + // Given + String documentId = nextIdAsString(); + SampleEntity sampleEntity = SampleEntity.builder().id(documentId).message("some message") + .version(System.currentTimeMillis()).build(); + + IndexQuery indexQuery = getIndexQuery(sampleEntity); + String indexName = indexNameProvider.indexName(); + + operations.index(indexQuery, IndexCoordinates.of(indexName)); + + // When + final Query query = getTermQuery("id", documentId); + org.springframework.data.elasticsearch.core.document.Document document = org.springframework.data.elasticsearch.core.document.Document + .create(); + document.put("message", "another message"); + + final UpdateQuery updateQuery = UpdateQuery.builder(query).withSlices(2).withDocument(document).build(); + ByQueryResponse result = operations.updateByQuery(updateQuery, IndexCoordinates.of(indexName)); + + // Then + assertThat(result.getDeleted()).isEqualTo(0); + assertThat(result.getUpdated()).isEqualTo(1); + SearchHits searchHits = operations.search(query, SampleEntity.class, + IndexCoordinates.of(indexName)); + assertThat(searchHits.getTotalHits()).isEqualTo(1); + } + @Test public void shouldDeleteDocumentForGivenQueryAndUnavailableIndex() {