Search in sources :

Example 11 with UpdateRequest

use of org.elasticsearch.action.update.UpdateRequest 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 12 with UpdateRequest

use of org.elasticsearch.action.update.UpdateRequest 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 13 with UpdateRequest

use of org.elasticsearch.action.update.UpdateRequest in project elasticsearch by elastic.

the class PipelineExecutionServiceTests method testBulkRequestExecutionWithFailures.

public void testBulkRequestExecutionWithFailures() throws Exception {
    BulkRequest bulkRequest = new BulkRequest();
    String pipelineId = "_id";
    int numRequest = scaledRandomIntBetween(8, 64);
    int numIndexRequests = 0;
    for (int i = 0; i < numRequest; i++) {
        DocWriteRequest request;
        if (randomBoolean()) {
            if (randomBoolean()) {
                request = new DeleteRequest("_index", "_type", "_id");
            } else {
                request = new UpdateRequest("_index", "_type", "_id");
            }
        } else {
            IndexRequest indexRequest = new IndexRequest("_index", "_type", "_id").setPipeline(pipelineId);
            indexRequest.source(Requests.INDEX_CONTENT_TYPE, "field1", "value1");
            request = indexRequest;
            numIndexRequests++;
        }
        bulkRequest.add(request);
    }
    CompoundProcessor processor = mock(CompoundProcessor.class);
    when(processor.getProcessors()).thenReturn(Collections.singletonList(mock(Processor.class)));
    Exception error = new RuntimeException();
    doThrow(error).when(processor).execute(any());
    when(store.get(pipelineId)).thenReturn(new Pipeline(pipelineId, null, version, processor));
    @SuppressWarnings("unchecked") BiConsumer<IndexRequest, Exception> requestItemErrorHandler = mock(BiConsumer.class);
    @SuppressWarnings("unchecked") Consumer<Exception> completionHandler = mock(Consumer.class);
    executionService.executeBulkRequest(bulkRequest.requests(), requestItemErrorHandler, completionHandler);
    verify(requestItemErrorHandler, times(numIndexRequests)).accept(any(IndexRequest.class), eq(error));
    verify(completionHandler, times(1)).accept(null);
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) Matchers.anyString(org.mockito.Matchers.anyString) IndexRequest(org.elasticsearch.action.index.IndexRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 14 with UpdateRequest

use of org.elasticsearch.action.update.UpdateRequest in project elasticsearch by elastic.

the class CrudIT method testBulk.

public void testBulk() throws IOException {
    int nbItems = randomIntBetween(10, 100);
    boolean[] errors = new boolean[nbItems];
    XContentType xContentType = randomFrom(XContentType.JSON, XContentType.SMILE);
    BulkRequest bulkRequest = new BulkRequest();
    for (int i = 0; i < nbItems; i++) {
        String id = String.valueOf(i);
        boolean erroneous = randomBoolean();
        errors[i] = erroneous;
        DocWriteRequest.OpType opType = randomFrom(DocWriteRequest.OpType.values());
        if (opType == DocWriteRequest.OpType.DELETE) {
            if (erroneous == false) {
                assertEquals(RestStatus.CREATED, highLevelClient().index(new IndexRequest("index", "test", id).source("field", -1)).status());
            }
            DeleteRequest deleteRequest = new DeleteRequest("index", "test", id);
            bulkRequest.add(deleteRequest);
        } else {
            BytesReference source = XContentBuilder.builder(xContentType.xContent()).startObject().field("id", i).endObject().bytes();
            if (opType == DocWriteRequest.OpType.INDEX) {
                IndexRequest indexRequest = new IndexRequest("index", "test", id).source(source, xContentType);
                if (erroneous) {
                    indexRequest.version(12L);
                }
                bulkRequest.add(indexRequest);
            } else if (opType == DocWriteRequest.OpType.CREATE) {
                IndexRequest createRequest = new IndexRequest("index", "test", id).source(source, xContentType).create(true);
                if (erroneous) {
                    assertEquals(RestStatus.CREATED, highLevelClient().index(createRequest).status());
                }
                bulkRequest.add(createRequest);
            } else if (opType == DocWriteRequest.OpType.UPDATE) {
                UpdateRequest updateRequest = new UpdateRequest("index", "test", id).doc(new IndexRequest().source(source, xContentType));
                if (erroneous == false) {
                    assertEquals(RestStatus.CREATED, highLevelClient().index(new IndexRequest("index", "test", id).source("field", -1)).status());
                }
                bulkRequest.add(updateRequest);
            }
        }
    }
    BulkResponse bulkResponse = execute(bulkRequest, highLevelClient()::bulk, highLevelClient()::bulkAsync);
    assertEquals(RestStatus.OK, bulkResponse.status());
    assertTrue(bulkResponse.getTookInMillis() > 0);
    assertEquals(nbItems, bulkResponse.getItems().length);
    for (int i = 0; i < nbItems; i++) {
        BulkItemResponse bulkItemResponse = bulkResponse.getItems()[i];
        assertEquals(i, bulkItemResponse.getItemId());
        assertEquals("index", bulkItemResponse.getIndex());
        assertEquals("test", bulkItemResponse.getType());
        assertEquals(String.valueOf(i), bulkItemResponse.getId());
        DocWriteRequest.OpType requestOpType = bulkRequest.requests().get(i).opType();
        if (requestOpType == DocWriteRequest.OpType.INDEX || requestOpType == DocWriteRequest.OpType.CREATE) {
            assertEquals(errors[i], bulkItemResponse.isFailed());
            assertEquals(errors[i] ? RestStatus.CONFLICT : RestStatus.CREATED, bulkItemResponse.status());
        } else if (requestOpType == DocWriteRequest.OpType.UPDATE) {
            assertEquals(errors[i], bulkItemResponse.isFailed());
            assertEquals(errors[i] ? RestStatus.NOT_FOUND : RestStatus.OK, bulkItemResponse.status());
        } else if (requestOpType == DocWriteRequest.OpType.DELETE) {
            assertFalse(bulkItemResponse.isFailed());
            assertEquals(errors[i] ? RestStatus.NOT_FOUND : RestStatus.OK, bulkItemResponse.status());
        }
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) XContentType(org.elasticsearch.common.xcontent.XContentType) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 15 with UpdateRequest

use of org.elasticsearch.action.update.UpdateRequest in project elasticsearch by elastic.

the class RequestTests method testUpdate.

public void testUpdate() throws IOException {
    XContentType xContentType = randomFrom(XContentType.values());
    Map<String, String> expectedParams = new HashMap<>();
    String index = randomAsciiOfLengthBetween(3, 10);
    String type = randomAsciiOfLengthBetween(3, 10);
    String id = randomAsciiOfLengthBetween(3, 10);
    UpdateRequest updateRequest = new UpdateRequest(index, type, id);
    updateRequest.detectNoop(randomBoolean());
    if (randomBoolean()) {
        BytesReference source = RandomObjects.randomSource(random(), xContentType);
        updateRequest.doc(new IndexRequest().source(source, xContentType));
        boolean docAsUpsert = randomBoolean();
        updateRequest.docAsUpsert(docAsUpsert);
        if (docAsUpsert) {
            expectedParams.put("doc_as_upsert", "true");
        }
    } else {
        updateRequest.script(new Script("_value + 1"));
        updateRequest.scriptedUpsert(randomBoolean());
    }
    if (randomBoolean()) {
        BytesReference source = RandomObjects.randomSource(random(), xContentType);
        updateRequest.upsert(new IndexRequest().source(source, xContentType));
    }
    if (randomBoolean()) {
        String routing = randomAsciiOfLengthBetween(3, 10);
        updateRequest.routing(routing);
        expectedParams.put("routing", routing);
    }
    if (randomBoolean()) {
        String parent = randomAsciiOfLengthBetween(3, 10);
        updateRequest.parent(parent);
        expectedParams.put("parent", parent);
    }
    if (randomBoolean()) {
        String timeout = randomTimeValue();
        updateRequest.timeout(timeout);
        expectedParams.put("timeout", timeout);
    } else {
        expectedParams.put("timeout", ReplicationRequest.DEFAULT_TIMEOUT.getStringRep());
    }
    if (randomBoolean()) {
        WriteRequest.RefreshPolicy refreshPolicy = randomFrom(WriteRequest.RefreshPolicy.values());
        updateRequest.setRefreshPolicy(refreshPolicy);
        if (refreshPolicy != WriteRequest.RefreshPolicy.NONE) {
            expectedParams.put("refresh", refreshPolicy.getValue());
        }
    }
    if (randomBoolean()) {
        int waitForActiveShards = randomIntBetween(0, 10);
        updateRequest.waitForActiveShards(waitForActiveShards);
        expectedParams.put("wait_for_active_shards", String.valueOf(waitForActiveShards));
    }
    if (randomBoolean()) {
        long version = randomLong();
        updateRequest.version(version);
        if (version != Versions.MATCH_ANY) {
            expectedParams.put("version", Long.toString(version));
        }
    }
    if (randomBoolean()) {
        VersionType versionType = randomFrom(VersionType.values());
        updateRequest.versionType(versionType);
        if (versionType != VersionType.INTERNAL) {
            expectedParams.put("version_type", versionType.name().toLowerCase(Locale.ROOT));
        }
    }
    if (randomBoolean()) {
        int retryOnConflict = randomIntBetween(0, 5);
        updateRequest.retryOnConflict(retryOnConflict);
        if (retryOnConflict > 0) {
            expectedParams.put("retry_on_conflict", String.valueOf(retryOnConflict));
        }
    }
    if (randomBoolean()) {
        randomizeFetchSourceContextParams(updateRequest::fetchSource, expectedParams);
    }
    Request request = Request.update(updateRequest);
    assertEquals("/" + index + "/" + type + "/" + id + "/_update", request.endpoint);
    assertEquals(expectedParams, request.params);
    assertEquals("POST", request.method);
    HttpEntity entity = request.entity;
    assertNotNull(entity);
    assertTrue(entity instanceof ByteArrayEntity);
    UpdateRequest parsedUpdateRequest = new UpdateRequest();
    XContentType entityContentType = XContentType.fromMediaTypeOrFormat(entity.getContentType().getValue());
    try (XContentParser parser = createParser(entityContentType.xContent(), entity.getContent())) {
        parsedUpdateRequest.fromXContent(parser);
    }
    assertEquals(updateRequest.scriptedUpsert(), parsedUpdateRequest.scriptedUpsert());
    assertEquals(updateRequest.docAsUpsert(), parsedUpdateRequest.docAsUpsert());
    assertEquals(updateRequest.detectNoop(), parsedUpdateRequest.detectNoop());
    assertEquals(updateRequest.fetchSource(), parsedUpdateRequest.fetchSource());
    assertEquals(updateRequest.script(), parsedUpdateRequest.script());
    if (updateRequest.doc() != null) {
        assertToXContentEquivalent(updateRequest.doc().source(), parsedUpdateRequest.doc().source(), xContentType);
    } else {
        assertNull(parsedUpdateRequest.doc());
    }
    if (updateRequest.upsertRequest() != null) {
        assertToXContentEquivalent(updateRequest.upsertRequest().source(), parsedUpdateRequest.upsertRequest().source(), xContentType);
    } else {
        assertNull(parsedUpdateRequest.upsertRequest());
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) Script(org.elasticsearch.script.Script) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) WriteRequest(org.elasticsearch.action.support.WriteRequest) ReplicatedWriteRequest(org.elasticsearch.action.support.replication.ReplicatedWriteRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) WriteRequest(org.elasticsearch.action.support.WriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) BulkShardRequest(org.elasticsearch.action.bulk.BulkShardRequest) GetRequest(org.elasticsearch.action.get.GetRequest) ReplicatedWriteRequest(org.elasticsearch.action.support.replication.ReplicatedWriteRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) ReplicationRequest(org.elasticsearch.action.support.replication.ReplicationRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) VersionType(org.elasticsearch.index.VersionType) XContentType(org.elasticsearch.common.xcontent.XContentType) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

UpdateRequest (org.elasticsearch.action.update.UpdateRequest)56 IndexRequest (org.elasticsearch.action.index.IndexRequest)32 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)25 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)14 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)11 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)8 GetRequest (org.elasticsearch.action.get.GetRequest)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 Script (org.elasticsearch.script.Script)6 HashMap (java.util.HashMap)5 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)5 WriteRequest (org.elasticsearch.action.support.WriteRequest)5 BytesReference (org.elasticsearch.common.bytes.BytesReference)5 XContentType (org.elasticsearch.common.xcontent.XContentType)5 VersionType (org.elasticsearch.index.VersionType)5 HttpEntity (org.apache.http.HttpEntity)4 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)4 ElasticsearchException (org.elasticsearch.ElasticsearchException)4 TimeValue (org.elasticsearch.common.unit.TimeValue)4