Search in sources :

Example 56 with IndexResponse

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

the class BulkItemResponse method readFrom.

@Override
public void readFrom(StreamInput in) throws IOException {
    id = in.readVInt();
    if (in.getVersion().onOrAfter(Version.V_5_3_0_UNRELEASED)) {
        opType = OpType.fromId(in.readByte());
    } else {
        opType = OpType.fromString(in.readString());
    }
    byte type = in.readByte();
    if (type == 0) {
        response = new IndexResponse();
        response.readFrom(in);
    } else if (type == 1) {
        response = new DeleteResponse();
        response.readFrom(in);
    } else if (type == 3) {
        // make 3 instead of 2, because 2 is already in use for 'no responses'
        response = new UpdateResponse();
        response.readFrom(in);
    }
    if (in.readBoolean()) {
        failure = new Failure(in);
    }
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse)

Example 57 with IndexResponse

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

the class TransportUpdateAction method shardOperation.

protected void shardOperation(final UpdateRequest request, final ActionListener<UpdateResponse> listener, final int retryCount) {
    final ShardId shardId = request.getShardId();
    final IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex());
    final IndexShard indexShard = indexService.getShard(shardId.getId());
    final UpdateHelper.Result result = updateHelper.prepare(request, indexShard, threadPool::absoluteTimeInMillis);
    switch(result.getResponseResult()) {
        case CREATED:
            IndexRequest upsertRequest = result.action();
            // we fetch it from the index request so we don't generate the bytes twice, its already done in the index request
            final BytesReference upsertSourceBytes = upsertRequest.source();
            bulkAction.execute(toSingleItemBulkRequest(upsertRequest), wrapBulkResponse(ActionListener.<IndexResponse>wrap(response -> {
                UpdateResponse update = new UpdateResponse(response.getShardInfo(), response.getShardId(), response.getType(), response.getId(), response.getSeqNo(), response.getVersion(), response.getResult());
                if ((request.fetchSource() != null && request.fetchSource().fetchSource()) || (request.fields() != null && request.fields().length > 0)) {
                    Tuple<XContentType, Map<String, Object>> sourceAndContent = XContentHelper.convertToMap(upsertSourceBytes, true, upsertRequest.getContentType());
                    update.setGetResult(updateHelper.extractGetResult(request, request.concreteIndex(), response.getVersion(), sourceAndContent.v2(), sourceAndContent.v1(), upsertSourceBytes));
                } else {
                    update.setGetResult(null);
                }
                update.setForcedRefresh(response.forcedRefresh());
                listener.onResponse(update);
            }, exception -> handleUpdateFailureWithRetry(listener, request, exception, retryCount))));
            break;
        case UPDATED:
            IndexRequest indexRequest = result.action();
            // we fetch it from the index request so we don't generate the bytes twice, its already done in the index request
            final BytesReference indexSourceBytes = indexRequest.source();
            bulkAction.execute(toSingleItemBulkRequest(indexRequest), wrapBulkResponse(ActionListener.<IndexResponse>wrap(response -> {
                UpdateResponse update = new UpdateResponse(response.getShardInfo(), response.getShardId(), response.getType(), response.getId(), response.getSeqNo(), response.getVersion(), response.getResult());
                update.setGetResult(updateHelper.extractGetResult(request, request.concreteIndex(), response.getVersion(), result.updatedSourceAsMap(), result.updateSourceContentType(), indexSourceBytes));
                update.setForcedRefresh(response.forcedRefresh());
                listener.onResponse(update);
            }, exception -> handleUpdateFailureWithRetry(listener, request, exception, retryCount))));
            break;
        case DELETED:
            DeleteRequest deleteRequest = result.action();
            bulkAction.execute(toSingleItemBulkRequest(deleteRequest), wrapBulkResponse(ActionListener.<DeleteResponse>wrap(response -> {
                UpdateResponse update = new UpdateResponse(response.getShardInfo(), response.getShardId(), response.getType(), response.getId(), response.getSeqNo(), response.getVersion(), response.getResult());
                update.setGetResult(updateHelper.extractGetResult(request, request.concreteIndex(), response.getVersion(), result.updatedSourceAsMap(), result.updateSourceContentType(), null));
                update.setForcedRefresh(response.forcedRefresh());
                listener.onResponse(update);
            }, exception -> handleUpdateFailureWithRetry(listener, request, exception, retryCount))));
            break;
        case NOOP:
            UpdateResponse update = result.action();
            IndexService indexServiceOrNull = indicesService.indexService(shardId.getIndex());
            if (indexServiceOrNull != null) {
                IndexShard shard = indexService.getShardOrNull(shardId.getId());
                if (shard != null) {
                    shard.noopUpdate(request.type());
                }
            }
            listener.onResponse(update);
            break;
        default:
            throw new IllegalStateException("Illegal result " + result.getResponseResult());
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) IndexRequest(org.elasticsearch.action.index.IndexRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) ShardId(org.elasticsearch.index.shard.ShardId) XContentType(org.elasticsearch.common.xcontent.XContentType) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) Map(java.util.Map) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 58 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project crate by crate.

the class ArrayMapperTest method testInsertSearchArray.

@Test
public void testInsertSearchArray() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("array_field").field("type", ArrayMapper.CONTENT_TYPE).startObject(ArrayMapper.INNER).field("type", "double").field("index", "not_analyzed").endObject().endObject().endObject().endObject().endObject().string();
    mapper(INDEX, TYPE, mapping);
    IndexResponse response = client().prepareIndex(INDEX, "type").setId("123").setSource("{array_field:[0.0, 99.9, -100.5678]}").execute().actionGet();
    assertThat(response.getVersion(), is(1L));
    client().admin().indices().prepareRefresh(INDEX).execute().actionGet();
    SearchResponse searchResponse = client().prepareSearch(INDEX).setTypes("type").setFetchSource(true).addField("array_field").setQuery("{\"term\": {\"array_field\": 0.0}}").execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), is(1L));
    assertThat(Joiner.on(',').withKeyValueSeparator(":").join(searchResponse.getHits().getAt(0).getSource()), is("array_field:[0.0, 99.9, -100.5678]"));
    assertThat(searchResponse.getHits().getAt(0).field("array_field").getValues(), Matchers.<Object>hasItems(0.0D, 99.9D, -100.5678D));
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) SQLTransportIntegrationTest(io.crate.integrationtests.SQLTransportIntegrationTest) Test(org.junit.Test)

Example 59 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project crate by crate.

the class ArrayMapperTest method testInsertGetArray.

@Test
public void testInsertGetArray() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("array_field").field("type", ArrayMapper.CONTENT_TYPE).startObject(ArrayMapper.INNER).field("type", "double").field("index", "not_analyzed").endObject().endObject().endObject().endObject().endObject().string();
    DocumentMapper mapper = mapper(INDEX, TYPE, mapping);
    IndexResponse response = client().prepareIndex(INDEX, "type").setId("123").setSource("{array_field:[0.0, 99.9, -100.5678]}").execute().actionGet();
    assertThat(response.getVersion(), is(1L));
    client().admin().indices().prepareRefresh(INDEX).execute().actionGet();
    // realtime
    GetResponse rtGetResponse = client().prepareGet(INDEX, "type", "123").setFetchSource(true).setFields("array_field").setRealtime(true).execute().actionGet();
    assertThat(rtGetResponse.getId(), is("123"));
    assertThat(Joiner.on(',').withKeyValueSeparator(":").join(rtGetResponse.getSource()), is("array_field:[0.0, 99.9, -100.5678]"));
    assertThat(rtGetResponse.getField("array_field").getValues(), Matchers.<Object>hasItems(0.0D, 99.9D, -100.5678D));
    // non-realtime
    GetResponse getResponse = client().prepareGet(INDEX, "type", "123").setFetchSource(true).setFields("array_field").setRealtime(false).execute().actionGet();
    assertThat(getResponse.getId(), is("123"));
    assertThat(Joiner.on(',').withKeyValueSeparator(":").join(getResponse.getSource()), is("array_field:[0.0, 99.9, -100.5678]"));
    assertThat(getResponse.getField("array_field").getValues(), Matchers.<Object>hasItems(0.0D, 99.9D, -100.5678D));
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse) GetResponse(org.elasticsearch.action.get.GetResponse) SQLTransportIntegrationTest(io.crate.integrationtests.SQLTransportIntegrationTest) Test(org.junit.Test)

Example 60 with IndexResponse

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

the class WaitUntilRefreshIT method testIndex.

public void testIndex() {
    IndexResponse index = client().prepareIndex("test", "index", "1").setSource("foo", "bar").setRefreshPolicy(RefreshPolicy.WAIT_UNTIL).get();
    assertEquals(RestStatus.CREATED, index.status());
    assertFalse("request shouldn't have forced a refresh", index.forcedRefresh());
    assertSearchHits(client().prepareSearch("test").setQuery(matchQuery("foo", "bar")).get(), "1");
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse)

Aggregations

IndexResponse (org.elasticsearch.action.index.IndexResponse)103 Test (org.junit.Test)26 SearchResponse (org.elasticsearch.action.search.SearchResponse)18 IOException (java.io.IOException)15 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)15 HashMap (java.util.HashMap)13 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)13 IndexRequest (org.elasticsearch.action.index.IndexRequest)13 ElasticsearchException (org.elasticsearch.ElasticsearchException)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)8 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)7 Settings (org.elasticsearch.common.settings.Settings)7 ArrayList (java.util.ArrayList)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 DeleteIndexResponse (org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse)6 ExecutionException (java.util.concurrent.ExecutionException)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)5