diff --git a/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java b/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java index e02dbbec8e7..344a292adaf 100644 --- a/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java +++ b/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java @@ -98,13 +98,7 @@ public class BulkRequest extends ActionRequest { */ public BulkRequest add(Iterable requests) { for (ActionRequest request : requests) { - if (request instanceof IndexRequest) { - add((IndexRequest) request); - } else if (request instanceof DeleteRequest) { - add((DeleteRequest) request); - } else { - throw new ElasticsearchIllegalArgumentException("No support for request [" + request + "]"); - } + add(request); } return this; } diff --git a/src/test/java/org/elasticsearch/action/bulk/BulkRequestTests.java b/src/test/java/org/elasticsearch/action/bulk/BulkRequestTests.java index 9171fc4e424..d92610ab3eb 100644 --- a/src/test/java/org/elasticsearch/action/bulk/BulkRequestTests.java +++ b/src/test/java/org/elasticsearch/action/bulk/BulkRequestTests.java @@ -21,14 +21,19 @@ package org.elasticsearch.action.bulk; import com.google.common.base.Charsets; import org.apache.lucene.util.Constants; +import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.update.UpdateRequest; +import org.elasticsearch.client.Requests; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.test.ElasticsearchTestCase; import org.junit.Test; +import java.util.ArrayList; +import java.util.List; + import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; @@ -98,4 +103,18 @@ public class BulkRequestTests extends ElasticsearchTestCase { bulkAction = copyToStringFromClasspath("/org/elasticsearch/action/bulk/simple-bulk5.json"); new BulkRequest().add(new BytesArray(bulkAction.getBytes(Charsets.UTF_8)), true, "test", null, false); } + + @Test + public void testBulkAddIterable() { + BulkRequest bulkRequest = Requests.bulkRequest(); + List requests = new ArrayList<>(); + requests.add(new IndexRequest("test", "test", "id").source("field", "value")); + requests.add(new UpdateRequest("test", "test", "id").doc("field", "value")); + requests.add(new DeleteRequest("test", "test", "id")); + bulkRequest.add(requests); + assertThat(bulkRequest.requests().size(), equalTo(3)); + assertThat(bulkRequest.requests().get(0), instanceOf(IndexRequest.class)); + assertThat(bulkRequest.requests().get(1), instanceOf(UpdateRequest.class)); + assertThat(bulkRequest.requests().get(2), instanceOf(DeleteRequest.class)); + } }