From b46d017e5c20538bf8f753935a80a081ed93d1fb Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Mon, 30 Jun 2014 11:59:47 +0200 Subject: [PATCH] Bulk API: Fix return of wrong request type on failed updates In case an update request failed (for example when updating with a wrongly formatted date), the returned index operation type was index instead of update. Closes #6630 --- .../action/bulk/TransportShardBulkAction.java | 2 +- .../org/elasticsearch/document/BulkTests.java | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java b/src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java index fe2fd5fee33..349665c8988 100644 --- a/src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java +++ b/src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java @@ -315,7 +315,7 @@ public class TransportShardBulkAction extends TransportShardReplicationOperation } else { logger.debug("[{}][{}] failed to execute bulk item (index) {}", t, shardRequest.request.index(), shardRequest.shardId, indexRequest); } - responses[requestIndex] = new BulkItemResponse(item.id(), indexRequest.opType().lowercase(), + responses[requestIndex] = new BulkItemResponse(item.id(), "update", new BulkItemResponse.Failure(indexRequest.index(), indexRequest.type(), indexRequest.id(), t)); break; case DELETE: diff --git a/src/test/java/org/elasticsearch/document/BulkTests.java b/src/test/java/org/elasticsearch/document/BulkTests.java index 597a17b8386..a96f2c7a80e 100644 --- a/src/test/java/org/elasticsearch/document/BulkTests.java +++ b/src/test/java/org/elasticsearch/document/BulkTests.java @@ -23,9 +23,12 @@ import com.google.common.base.Charsets; import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.count.CountResponse; +import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.get.GetResponse; +import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateRequestBuilder; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.common.bytes.BytesArray; @@ -596,5 +599,35 @@ public class BulkTests extends ElasticsearchIntegrationTest { assertThat(bulkResponse.getItems()[i].isFailed(), is(expectedFailures[i])); } } + + @Test // issue 6630 + public void testThatFailedUpdateRequestReturnsCorrectType() throws Exception { + BulkResponse indexBulkItemResponse = client().prepareBulk() + .add(new IndexRequest("test", "type", "3").source("{ \"title\" : \"Great Title of doc 3\" }")) + .add(new IndexRequest("test", "type", "4").source("{ \"title\" : \"Great Title of doc 4\" }")) + .add(new IndexRequest("test", "type", "5").source("{ \"title\" : \"Great Title of doc 5\" }")) + .add(new IndexRequest("test", "type", "6").source("{ \"title\" : \"Great Title of doc 6\" }")) + .setRefresh(true) + .get(); + assertNoFailures(indexBulkItemResponse); + + BulkResponse bulkItemResponse = client().prepareBulk() + .add(new IndexRequest("test", "type", "1").source("{ \"title\" : \"Great Title of doc 1\" }")) + .add(new IndexRequest("test", "type", "2").source("{ \"title\" : \"Great Title of doc 2\" }")) + .add(new UpdateRequest("test", "type", "3").doc("{ \"date\" : \"2014-01-30T23:59:57\"}")) + .add(new UpdateRequest("test", "type", "4").doc("{ \"date\" : \"2014-13-30T23:59:57\"}")) + .add(new DeleteRequest("test", "type", "5")) + .add(new DeleteRequest("test", "type", "6")) + .get(); + + assertNoFailures(indexBulkItemResponse); + assertThat(bulkItemResponse.getItems().length, is(6)); + assertThat(bulkItemResponse.getItems()[0].getOpType(), is("index")); + assertThat(bulkItemResponse.getItems()[1].getOpType(), is("index")); + assertThat(bulkItemResponse.getItems()[2].getOpType(), is("update")); + assertThat(bulkItemResponse.getItems()[3].getOpType(), is("update")); + assertThat(bulkItemResponse.getItems()[4].getOpType(), is("delete")); + assertThat(bulkItemResponse.getItems()[5].getOpType(), is("delete")); + } }