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);
}
}
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());
}
}
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));
}
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));
}
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");
}
Aggregations