Search in sources :

Example 16 with UpdateRequest

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

the class RequestTests method testUpdateWithDifferentContentTypes.

public void testUpdateWithDifferentContentTypes() throws IOException {
    IllegalStateException exception = expectThrows(IllegalStateException.class, () -> {
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.doc(new IndexRequest().source(singletonMap("field", "doc"), XContentType.JSON));
        updateRequest.upsert(new IndexRequest().source(singletonMap("field", "upsert"), XContentType.YAML));
        Request.update(updateRequest);
    });
    assertEquals("Update request cannot have different content types for doc [JSON] and upsert [YAML] documents", exception.getMessage());
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 17 with UpdateRequest

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

the class RequestTests method testBulk.

public void testBulk() throws IOException {
    Map<String, String> expectedParams = new HashMap<>();
    BulkRequest bulkRequest = new BulkRequest();
    if (randomBoolean()) {
        String timeout = randomTimeValue();
        bulkRequest.timeout(timeout);
        expectedParams.put("timeout", timeout);
    } else {
        expectedParams.put("timeout", BulkShardRequest.DEFAULT_TIMEOUT.getStringRep());
    }
    if (randomBoolean()) {
        WriteRequest.RefreshPolicy refreshPolicy = randomFrom(WriteRequest.RefreshPolicy.values());
        bulkRequest.setRefreshPolicy(refreshPolicy);
        if (refreshPolicy != WriteRequest.RefreshPolicy.NONE) {
            expectedParams.put("refresh", refreshPolicy.getValue());
        }
    }
    XContentType xContentType = randomFrom(XContentType.JSON, XContentType.SMILE);
    int nbItems = randomIntBetween(10, 100);
    for (int i = 0; i < nbItems; i++) {
        String index = randomAsciiOfLength(5);
        String type = randomAsciiOfLength(5);
        String id = randomAsciiOfLength(5);
        BytesReference source = RandomObjects.randomSource(random(), xContentType);
        DocWriteRequest.OpType opType = randomFrom(DocWriteRequest.OpType.values());
        DocWriteRequest<?> docWriteRequest = null;
        if (opType == DocWriteRequest.OpType.INDEX) {
            IndexRequest indexRequest = new IndexRequest(index, type, id).source(source, xContentType);
            docWriteRequest = indexRequest;
            if (randomBoolean()) {
                indexRequest.setPipeline(randomAsciiOfLength(5));
            }
            if (randomBoolean()) {
                indexRequest.parent(randomAsciiOfLength(5));
            }
        } else if (opType == DocWriteRequest.OpType.CREATE) {
            IndexRequest createRequest = new IndexRequest(index, type, id).source(source, xContentType).create(true);
            docWriteRequest = createRequest;
            if (randomBoolean()) {
                createRequest.parent(randomAsciiOfLength(5));
            }
        } else if (opType == DocWriteRequest.OpType.UPDATE) {
            final UpdateRequest updateRequest = new UpdateRequest(index, type, id).doc(new IndexRequest().source(source, xContentType));
            docWriteRequest = updateRequest;
            if (randomBoolean()) {
                updateRequest.retryOnConflict(randomIntBetween(1, 5));
            }
            if (randomBoolean()) {
                randomizeFetchSourceContextParams(updateRequest::fetchSource, new HashMap<>());
            }
            if (randomBoolean()) {
                updateRequest.parent(randomAsciiOfLength(5));
            }
        } else if (opType == DocWriteRequest.OpType.DELETE) {
            docWriteRequest = new DeleteRequest(index, type, id);
        }
        if (randomBoolean()) {
            docWriteRequest.routing(randomAsciiOfLength(10));
        }
        if (randomBoolean()) {
            docWriteRequest.version(randomNonNegativeLong());
        }
        if (randomBoolean()) {
            docWriteRequest.versionType(randomFrom(VersionType.values()));
        }
        bulkRequest.add(docWriteRequest);
    }
    Request request = Request.bulk(bulkRequest);
    assertEquals("/_bulk", request.endpoint);
    assertEquals(expectedParams, request.params);
    assertEquals("POST", request.method);
    byte[] content = new byte[(int) request.entity.getContentLength()];
    try (InputStream inputStream = request.entity.getContent()) {
        Streams.readFully(inputStream, content);
    }
    BulkRequest parsedBulkRequest = new BulkRequest();
    parsedBulkRequest.add(content, 0, content.length, xContentType);
    assertEquals(bulkRequest.numberOfActions(), parsedBulkRequest.numberOfActions());
    for (int i = 0; i < bulkRequest.numberOfActions(); i++) {
        DocWriteRequest<?> originalRequest = bulkRequest.requests().get(i);
        DocWriteRequest<?> parsedRequest = parsedBulkRequest.requests().get(i);
        assertEquals(originalRequest.opType(), parsedRequest.opType());
        assertEquals(originalRequest.index(), parsedRequest.index());
        assertEquals(originalRequest.type(), parsedRequest.type());
        assertEquals(originalRequest.id(), parsedRequest.id());
        assertEquals(originalRequest.routing(), parsedRequest.routing());
        assertEquals(originalRequest.parent(), parsedRequest.parent());
        assertEquals(originalRequest.version(), parsedRequest.version());
        assertEquals(originalRequest.versionType(), parsedRequest.versionType());
        DocWriteRequest.OpType opType = originalRequest.opType();
        if (opType == DocWriteRequest.OpType.INDEX) {
            IndexRequest indexRequest = (IndexRequest) originalRequest;
            IndexRequest parsedIndexRequest = (IndexRequest) parsedRequest;
            assertEquals(indexRequest.getPipeline(), parsedIndexRequest.getPipeline());
            assertToXContentEquivalent(indexRequest.source(), parsedIndexRequest.source(), xContentType);
        } else if (opType == DocWriteRequest.OpType.UPDATE) {
            UpdateRequest updateRequest = (UpdateRequest) originalRequest;
            UpdateRequest parsedUpdateRequest = (UpdateRequest) parsedRequest;
            assertEquals(updateRequest.retryOnConflict(), parsedUpdateRequest.retryOnConflict());
            assertEquals(updateRequest.fetchSource(), parsedUpdateRequest.fetchSource());
            if (updateRequest.doc() != null) {
                assertToXContentEquivalent(updateRequest.doc().source(), parsedUpdateRequest.doc().source(), xContentType);
            } else {
                assertNull(parsedUpdateRequest.doc());
            }
        }
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) 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) InputStream(java.io.InputStream) 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) XContentType(org.elasticsearch.common.xcontent.XContentType) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 18 with UpdateRequest

use of org.elasticsearch.action.update.UpdateRequest in project sonarqube by SonarSource.

the class ProxyBulkRequestBuilder method toString.

@Override
public String toString() {
    StringBuilder message = new StringBuilder();
    message.append("Bulk[");
    Multiset<BulkRequestKey> groupedRequests = LinkedHashMultiset.create();
    for (int i = 0; i < request.requests().size(); i++) {
        ActionRequest<?> item = request.requests().get(i);
        String requestType;
        String index;
        String docType;
        if (item instanceof IndexRequest) {
            IndexRequest request = (IndexRequest) item;
            requestType = "index";
            index = request.index();
            docType = request.type();
        } else if (item instanceof UpdateRequest) {
            UpdateRequest request = (UpdateRequest) item;
            requestType = "update";
            index = request.index();
            docType = request.type();
        } else if (item instanceof DeleteRequest) {
            DeleteRequest request = (DeleteRequest) item;
            requestType = "delete";
            index = request.index();
            docType = request.type();
        } else {
            // Cannot happen, not allowed by BulkRequest's contract
            throw new IllegalStateException("Unsupported bulk request type: " + item.getClass());
        }
        groupedRequests.add(new BulkRequestKey(requestType, index, docType));
    }
    Set<Multiset.Entry<BulkRequestKey>> entrySet = groupedRequests.entrySet();
    int size = entrySet.size();
    int current = 0;
    for (Multiset.Entry<BulkRequestKey> requestEntry : entrySet) {
        message.append(requestEntry.getCount()).append(" ").append(requestEntry.getElement().toString());
        current++;
        if (current < size) {
            message.append(", ");
        }
    }
    message.append("]");
    return message.toString();
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) Multiset(com.google.common.collect.Multiset) LinkedHashMultiset(com.google.common.collect.LinkedHashMultiset) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 19 with UpdateRequest

use of org.elasticsearch.action.update.UpdateRequest in project sonarqube by SonarSource.

the class TestResultSetIterator method toRow.

/**
   * Convert protobuf message to tests required for Elasticsearch indexing
   */
public static Row toRow(String projectUuid, String fileUuid, Date updatedAt, List<DbFileSources.Test> tests) {
    Row result = new Row(projectUuid, fileUuid, updatedAt.getTime());
    for (DbFileSources.Test test : tests) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        // all the fields must be present, even if value is null
        try (JsonWriter writer = JsonWriter.of(new OutputStreamWriter(bytes, StandardCharsets.UTF_8)).setSerializeNulls(true)) {
            writer.beginObject();
            writer.prop(FIELD_PROJECT_UUID, projectUuid);
            writer.prop(FIELD_FILE_UUID, fileUuid);
            writer.prop(FIELD_TEST_UUID, test.getUuid());
            writer.prop(FIELD_NAME, test.getName());
            writer.prop(FIELD_STATUS, test.hasStatus() ? test.getStatus().toString() : null);
            writer.prop(FIELD_DURATION_IN_MS, test.hasExecutionTimeMs() ? test.getExecutionTimeMs() : null);
            writer.prop(FIELD_MESSAGE, test.hasMsg() ? test.getMsg() : null);
            writer.prop(FIELD_STACKTRACE, test.hasStacktrace() ? test.getStacktrace() : null);
            writer.prop(FIELD_UPDATED_AT, EsUtils.formatDateTime(updatedAt));
            writer.name(FIELD_COVERED_FILES);
            writer.beginArray();
            for (DbFileSources.Test.CoveredFile coveredFile : test.getCoveredFileList()) {
                writer.beginObject();
                writer.prop(FIELD_COVERED_FILE_UUID, coveredFile.getFileUuid());
                writer.name(FIELD_COVERED_FILE_LINES).valueObject(coveredFile.getCoveredLineList());
                writer.endObject();
            }
            writer.endArray();
            writer.endObject();
        }
        // This is an optimization to reduce memory consumption and multiple conversions from Map to JSON.
        // UpdateRequest#doc() and #upsert() take the same parameter values, so:
        // - passing the same Map would execute two JSON serializations
        // - Map is a useless temporarily structure: read JDBC result set -> convert to map -> convert to JSON. Generating
        // directly JSON from result set is more efficient.
        byte[] jsonDoc = bytes.toByteArray();
        UpdateRequest updateRequest = new UpdateRequest(INDEX_TYPE_TEST.getIndex(), INDEX_TYPE_TEST.getType(), test.getUuid()).routing(projectUuid).doc(jsonDoc).upsert(jsonDoc);
        result.getUpdateRequests().add(updateRequest);
    }
    return result;
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) OutputStreamWriter(java.io.OutputStreamWriter) Row(org.sonar.server.source.index.FileSourcesUpdaterHelper.Row) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DbFileSources(org.sonar.db.protobuf.DbFileSources) JsonWriter(org.sonar.api.utils.text.JsonWriter)

Example 20 with UpdateRequest

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

the class SimpleRoutingIT method testRequiredRoutingBulk.

public void testRequiredRoutingBulk() throws Exception {
    client().admin().indices().prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("_routing").field("required", true).endObject().endObject().endObject()).execute().actionGet();
    ensureGreen();
    {
        BulkResponse bulkResponse = client().prepareBulk().add(Requests.indexRequest(indexOrAlias()).type("type1").id("1").source(Requests.INDEX_CONTENT_TYPE, "field", "value")).execute().actionGet();
        assertThat(bulkResponse.getItems().length, equalTo(1));
        assertThat(bulkResponse.hasFailures(), equalTo(true));
        for (BulkItemResponse bulkItemResponse : bulkResponse) {
            assertThat(bulkItemResponse.isFailed(), equalTo(true));
            assertThat(bulkItemResponse.getOpType(), equalTo(DocWriteRequest.OpType.INDEX));
            assertThat(bulkItemResponse.getFailure().getStatus(), equalTo(RestStatus.BAD_REQUEST));
            assertThat(bulkItemResponse.getFailure().getCause(), instanceOf(RoutingMissingException.class));
            assertThat(bulkItemResponse.getFailureMessage(), containsString("routing is required for [test]/[type1]/[1]"));
        }
    }
    {
        BulkResponse bulkResponse = client().prepareBulk().add(Requests.indexRequest(indexOrAlias()).type("type1").id("1").routing("0").source(Requests.INDEX_CONTENT_TYPE, "field", "value")).execute().actionGet();
        assertThat(bulkResponse.hasFailures(), equalTo(false));
    }
    {
        BulkResponse bulkResponse = client().prepareBulk().add(new UpdateRequest(indexOrAlias(), "type1", "1").doc(Requests.INDEX_CONTENT_TYPE, "field", "value2")).execute().actionGet();
        assertThat(bulkResponse.getItems().length, equalTo(1));
        assertThat(bulkResponse.hasFailures(), equalTo(true));
        for (BulkItemResponse bulkItemResponse : bulkResponse) {
            assertThat(bulkItemResponse.isFailed(), equalTo(true));
            assertThat(bulkItemResponse.getOpType(), equalTo(DocWriteRequest.OpType.UPDATE));
            assertThat(bulkItemResponse.getFailure().getStatus(), equalTo(RestStatus.BAD_REQUEST));
            assertThat(bulkItemResponse.getFailure().getCause(), instanceOf(RoutingMissingException.class));
            assertThat(bulkItemResponse.getFailureMessage(), containsString("routing is required for [test]/[type1]/[1]"));
        }
    }
    {
        BulkResponse bulkResponse = client().prepareBulk().add(new UpdateRequest(indexOrAlias(), "type1", "1").doc(Requests.INDEX_CONTENT_TYPE, "field", "value2").routing("0")).execute().actionGet();
        assertThat(bulkResponse.hasFailures(), equalTo(false));
    }
    {
        BulkResponse bulkResponse = client().prepareBulk().add(Requests.deleteRequest(indexOrAlias()).type("type1").id("1")).execute().actionGet();
        assertThat(bulkResponse.getItems().length, equalTo(1));
        assertThat(bulkResponse.hasFailures(), equalTo(true));
        for (BulkItemResponse bulkItemResponse : bulkResponse) {
            assertThat(bulkItemResponse.isFailed(), equalTo(true));
            assertThat(bulkItemResponse.getOpType(), equalTo(DocWriteRequest.OpType.DELETE));
            assertThat(bulkItemResponse.getFailure().getStatus(), equalTo(RestStatus.BAD_REQUEST));
            assertThat(bulkItemResponse.getFailure().getCause(), instanceOf(RoutingMissingException.class));
            assertThat(bulkItemResponse.getFailureMessage(), containsString("routing is required for [test]/[type1]/[1]"));
        }
    }
    {
        BulkResponse bulkResponse = client().prepareBulk().add(Requests.deleteRequest(indexOrAlias()).type("type1").id("1").routing("0")).execute().actionGet();
        assertThat(bulkResponse.getItems().length, equalTo(1));
        assertThat(bulkResponse.hasFailures(), equalTo(false));
    }
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) Alias(org.elasticsearch.action.admin.indices.alias.Alias) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse)

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