Search in sources :

Example 16 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest in project elasticsearch by elastic.

the class BulkRequestTests method testBulkAddIterable.

public void testBulkAddIterable() {
    BulkRequest bulkRequest = Requests.bulkRequest();
    List<DocWriteRequest> requests = new ArrayList<>();
    requests.add(new IndexRequest("test", "test", "id").source(Requests.INDEX_CONTENT_TYPE, "field", "value"));
    requests.add(new UpdateRequest("test", "test", "id").doc(Requests.INDEX_CONTENT_TYPE, "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));
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) ArrayList(java.util.ArrayList) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 17 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest in project elasticsearch by elastic.

the class BulkWithUpdatesIT method testThatFailedUpdateRequestReturnsCorrectType.

// issue 6630
public void testThatFailedUpdateRequestReturnsCorrectType() throws Exception {
    BulkResponse indexBulkItemResponse = client().prepareBulk().add(new IndexRequest("test", "type", "3").source("{ \"title\" : \"Great Title of doc 3\" }", XContentType.JSON)).add(new IndexRequest("test", "type", "4").source("{ \"title\" : \"Great Title of doc 4\" }", XContentType.JSON)).add(new IndexRequest("test", "type", "5").source("{ \"title\" : \"Great Title of doc 5\" }", XContentType.JSON)).add(new IndexRequest("test", "type", "6").source("{ \"title\" : \"Great Title of doc 6\" }", XContentType.JSON)).setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
    assertNoFailures(indexBulkItemResponse);
    BulkResponse bulkItemResponse = client().prepareBulk().add(new IndexRequest("test", "type", "1").source("{ \"title\" : \"Great Title of doc 1\" }", XContentType.JSON)).add(new IndexRequest("test", "type", "2").source("{ \"title\" : \"Great Title of doc 2\" }", XContentType.JSON)).add(new UpdateRequest("test", "type", "3").doc("{ \"date\" : \"2014-01-30T23:59:57\"}", XContentType.JSON)).add(new UpdateRequest("test", "type", "4").doc("{ \"date\" : \"2014-13-30T23:59:57\"}", XContentType.JSON)).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(OpType.INDEX));
    assertThat(bulkItemResponse.getItems()[1].getOpType(), is(OpType.INDEX));
    assertThat(bulkItemResponse.getItems()[2].getOpType(), is(OpType.UPDATE));
    assertThat(bulkItemResponse.getItems()[3].getOpType(), is(OpType.UPDATE));
    assertThat(bulkItemResponse.getItems()[4].getOpType(), is(OpType.DELETE));
    assertThat(bulkItemResponse.getItems()[5].getOpType(), is(OpType.DELETE));
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 18 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest in project elasticsearch by elastic.

the class BulkWithUpdatesIT method testThatMissingIndexDoesNotAbortFullBulkRequest.

// issue 6410
public void testThatMissingIndexDoesNotAbortFullBulkRequest() throws Exception {
    createIndex("bulkindex1", "bulkindex2");
    BulkRequest bulkRequest = new BulkRequest();
    bulkRequest.add(new IndexRequest("bulkindex1", "index1_type", "1").source(Requests.INDEX_CONTENT_TYPE, "text", "hallo1")).add(new IndexRequest("bulkindex2", "index2_type", "1").source(Requests.INDEX_CONTENT_TYPE, "text", "hallo2")).add(new IndexRequest("bulkindex2", "index2_type").source(Requests.INDEX_CONTENT_TYPE, "text", "hallo2")).add(new UpdateRequest("bulkindex2", "index2_type", "2").doc(Requests.INDEX_CONTENT_TYPE, "foo", "bar")).add(new DeleteRequest("bulkindex2", "index2_type", "3")).setRefreshPolicy(RefreshPolicy.IMMEDIATE);
    client().bulk(bulkRequest).get();
    SearchResponse searchResponse = client().prepareSearch("bulkindex*").get();
    assertHitCount(searchResponse, 3);
    assertAcked(client().admin().indices().prepareClose("bulkindex2"));
    BulkResponse bulkResponse = client().bulk(bulkRequest).get();
    assertThat(bulkResponse.hasFailures(), is(true));
    assertThat(bulkResponse.getItems().length, is(5));
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 19 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest in project elasticsearch by elastic.

the class AbstractAsyncBulkByScrollActionScriptTestCase method testChangeSource.

public void testChangeSource() {
    IndexRequest index = applyScript((Map<String, Object> ctx) -> {
        @SuppressWarnings("unchecked") Map<String, Object> source = (Map<String, Object>) ctx.get("_source");
        source.put("bar", "cat");
    });
    assertEquals("cat", index.sourceAsMap().get("bar"));
}
Also used : IndexRequest(org.elasticsearch.action.index.IndexRequest) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap)

Example 20 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest in project elasticsearch by elastic.

the class AbstractAsyncBulkByScrollActionScriptTestCase method applyScript.

@SuppressWarnings("unchecked")
protected <T extends ActionRequest> T applyScript(Consumer<Map<String, Object>> scriptBody) {
    IndexRequest index = new IndexRequest("index", "type", "1").source(singletonMap("foo", "bar"));
    ScrollableHitSource.Hit doc = new ScrollableHitSource.BasicHit("test", "type", "id", 0);
    ExecutableScript executableScript = new SimpleExecutableScript(scriptBody);
    when(scriptService.executable(any(CompiledScript.class), Matchers.<Map<String, Object>>any())).thenReturn(executableScript);
    AbstractAsyncBulkByScrollAction<Request> action = action(scriptService, request().setScript(EMPTY_SCRIPT));
    RequestWrapper<?> result = action.buildScriptApplier().apply(AbstractAsyncBulkByScrollAction.wrap(index), doc);
    return (result != null) ? (T) result.self() : null;
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) ActionRequest(org.elasticsearch.action.ActionRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) ScrollableHitSource(org.elasticsearch.action.bulk.byscroll.ScrollableHitSource) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Aggregations

IndexRequest (org.elasticsearch.action.index.IndexRequest)175 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)34 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)32 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)32 IOException (java.io.IOException)28 Test (org.junit.Test)27 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)25 ElasticsearchException (org.elasticsearch.ElasticsearchException)20 IndexResponse (org.elasticsearch.action.index.IndexResponse)17 HashMap (java.util.HashMap)16 Map (java.util.Map)14 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)14 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)14 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)13 GetRequest (org.elasticsearch.action.get.GetRequest)13 BytesReference (org.elasticsearch.common.bytes.BytesReference)11 ArrayList (java.util.ArrayList)10 Matchers.anyBoolean (org.mockito.Matchers.anyBoolean)9 CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)8 Settings (org.elasticsearch.common.settings.Settings)8