Search in sources :

Example 11 with IndexRequest

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

the class BulkRequestModifierTests method testNoFailures.

public void testNoFailures() {
    BulkRequest originalBulkRequest = new BulkRequest();
    for (int i = 0; i < 32; i++) {
        originalBulkRequest.add(new IndexRequest("index", "type", String.valueOf(i)));
    }
    TransportBulkAction.BulkRequestModifier modifier = new TransportBulkAction.BulkRequestModifier(originalBulkRequest);
    while (modifier.hasNext()) {
        modifier.next();
    }
    BulkRequest bulkRequest = modifier.getBulkRequest();
    assertThat(bulkRequest, Matchers.sameInstance(originalBulkRequest));
    @SuppressWarnings("unchecked") ActionListener<BulkResponse> actionListener = mock(ActionListener.class);
    assertThat(modifier.wrapActionListenerIfNeeded(1L, actionListener).getClass().isAnonymousClass(), is(true));
}
Also used : IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 12 with IndexRequest

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

the class BulkRequestTests method testSmileIsSupported.

public void testSmileIsSupported() throws IOException {
    XContentType xContentType = XContentType.SMILE;
    BytesReference data;
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType, out)) {
            builder.startObject();
            builder.startObject("index");
            builder.field("_index", "index");
            builder.field("_type", "type");
            builder.field("_id", "test");
            builder.endObject();
            builder.endObject();
        }
        out.write(xContentType.xContent().streamSeparator());
        try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType, out)) {
            builder.startObject();
            builder.field("field", "value");
            builder.endObject();
        }
        out.write(xContentType.xContent().streamSeparator());
        data = out.bytes();
    }
    BulkRequest bulkRequest = new BulkRequest();
    bulkRequest.add(data, null, null, xContentType);
    assertEquals(1, bulkRequest.requests().size());
    DocWriteRequest docWriteRequest = bulkRequest.requests().get(0);
    assertEquals(DocWriteRequest.OpType.INDEX, docWriteRequest.opType());
    assertEquals("index", docWriteRequest.index());
    assertEquals("type", docWriteRequest.type());
    assertEquals("test", docWriteRequest.id());
    assertThat(docWriteRequest, instanceOf(IndexRequest.class));
    IndexRequest request = (IndexRequest) docWriteRequest;
    assertEquals(1, request.sourceAsMap().size());
    assertEquals("value", request.sourceAsMap().get("field"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 13 with IndexRequest

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

the class BulkRequestTests method testSimpleBulkWithCarriageReturn.

public void testSimpleBulkWithCarriageReturn() throws Exception {
    String bulkAction = "{ \"index\":{\"_index\":\"test\",\"_type\":\"type1\",\"_id\":\"1\"} }\r\n{ \"field1\" : \"value1\" }\r\n";
    BulkRequest bulkRequest = new BulkRequest();
    bulkRequest.add(bulkAction.getBytes(StandardCharsets.UTF_8), 0, bulkAction.length(), null, null, XContentType.JSON);
    assertThat(bulkRequest.numberOfActions(), equalTo(1));
    assertThat(((IndexRequest) bulkRequest.requests().get(0)).source(), equalTo(new BytesArray("{ \"field1\" : \"value1\" }")));
    Map<String, Object> sourceMap = XContentHelper.convertToMap(((IndexRequest) bulkRequest.requests().get(0)).source(), false, XContentType.JSON).v2();
    assertEquals("value1", sourceMap.get("field1"));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 14 with IndexRequest

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

the class BulkRequestTests method testBulkNoSource.

// issue 15120
public void testBulkNoSource() throws Exception {
    BulkRequest bulkRequest = new BulkRequest();
    bulkRequest.add(new UpdateRequest("index", "type", "id"));
    bulkRequest.add(new IndexRequest("index", "type", "id"));
    ActionRequestValidationException validate = bulkRequest.validate();
    assertThat(validate, notNullValue());
    assertThat(validate.validationErrors(), not(empty()));
    assertThat(validate.validationErrors(), contains("script or doc is missing", "source is missing", "content type is missing"));
}
Also used : ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 15 with IndexRequest

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

the class TransportShardBulkActionTests method testExecuteBulkIndexRequestWithRejection.

public void testExecuteBulkIndexRequestWithRejection() throws Exception {
    IndexMetaData metaData = indexMetaData();
    IndexShard shard = newStartedShard(true);
    BulkItemRequest[] items = new BulkItemRequest[1];
    DocWriteRequest writeRequest = new IndexRequest("index", "type", "id").source(Requests.INDEX_CONTENT_TYPE, "foo", "bar");
    items[0] = new BulkItemRequest(0, writeRequest);
    BulkShardRequest bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
    Translog.Location location = new Translog.Location(0, 0, 0);
    UpdateHelper updateHelper = null;
    // Pretend the mappings haven't made it to the node yet, and throw  a rejection
    Exception err = new ReplicationOperation.RetryOnPrimaryException(shardId, "rejection");
    try {
        TransportShardBulkAction.executeBulkItemRequest(metaData, shard, bulkShardRequest, location, 0, updateHelper, threadPool::absoluteTimeInMillis, new ThrowingMappingUpdatePerformer(err));
        fail("should have thrown a retry exception");
    } catch (ReplicationOperation.RetryOnPrimaryException e) {
        assertThat(e, equalTo(err));
    }
    closeShards(shard);
}
Also used : IndexShard(org.elasticsearch.index.shard.IndexShard) IndexRequest(org.elasticsearch.action.index.IndexRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) IOException(java.io.IOException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Translog(org.elasticsearch.index.translog.Translog) UpdateHelper(org.elasticsearch.action.update.UpdateHelper) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) ReplicationOperation(org.elasticsearch.action.support.replication.ReplicationOperation)

Aggregations

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