use of org.elasticsearch.action.delete.DeleteResponse in project elasticsearch-indexing-proxy by codelibs.
the class ProxyActionFilter method getExecutor.
@SuppressWarnings("unchecked")
private <Request extends ActionRequest, Response extends ActionResponse> Supplier<Response> getExecutor(final Task task, final String action, final Request request) {
if (BulkAction.NAME.equals(action)) {
final long startTime = System.nanoTime();
int count = 0;
final BulkRequest req = (BulkRequest) request;
for (final DocWriteRequest<?> subReq : req.requests()) {
if (indexingProxyService.isTargetIndex(subReq.index())) {
count++;
}
}
if (count == 0) {
return null;
} else if (count != req.requests().size()) {
throw new ElasticsearchException("Mixed target requests. ({} != {})", count, req.requests().size());
}
return () -> {
final List<BulkItemResponse> responseList = new ArrayList<>(req.requests().size());
for (int i = 0; i < req.requests().size(); i++) {
final DocWriteRequest<?> dwr = req.requests().get(i);
if (dwr instanceof IndexRequest) {
final IndexRequest r = (IndexRequest) dwr;
final String id = r.id() == null ? INDEX_UUID : r.id();
final IndexResponse response = new IndexResponse(new ShardId(new Index(r.index(), INDEX_UUID), 0), r.type(), id, r.version(), true);
responseList.add(new BulkItemResponse(i, r.opType(), response));
} else if (dwr instanceof UpdateRequest) {
final UpdateRequest r = (UpdateRequest) dwr;
final String id = r.id() == null ? INDEX_UUID : r.id();
final UpdateResponse response = new UpdateResponse(new ShardId(new Index(r.index(), INDEX_UUID), 0), r.type(), id, r.version(), Result.CREATED);
responseList.add(new BulkItemResponse(i, r.opType(), response));
} else if (dwr instanceof DeleteRequest) {
final DeleteRequest r = (DeleteRequest) dwr;
final String id = r.id() == null ? INDEX_UUID : r.id();
final DeleteResponse response = new DeleteResponse(new ShardId(new Index(r.index(), INDEX_UUID), 0), r.type(), id, r.version(), true);
response.setShardInfo(new ReplicationResponse.ShardInfo(1, 1, ReplicationResponse.EMPTY));
responseList.add(new BulkItemResponse(i, r.opType(), response));
} else {
responseList.add(new BulkItemResponse(i, dwr.opType(), new BulkItemResponse.Failure(dwr.index(), dwr.type(), dwr.id(), new ElasticsearchException("Unknown request: " + dwr))));
}
}
return (Response) new BulkResponse(responseList.toArray(new BulkItemResponse[responseList.size()]), (System.nanoTime() - startTime) / 1000000);
};
} else if (DeleteAction.NAME.equals(action)) {
final DeleteRequest req = (DeleteRequest) request;
if (!indexingProxyService.isTargetIndex(req.index())) {
return null;
}
return () -> {
final String id = req.id() == null ? INDEX_UUID : req.id();
final DeleteResponse res = new DeleteResponse(new ShardId(new Index(req.index(), INDEX_UUID), 0), req.type(), id, req.version(), true);
res.setShardInfo(new ReplicationResponse.ShardInfo(1, 1, ReplicationResponse.EMPTY));
return (Response) res;
};
} else if (DeleteByQueryAction.NAME.equals(action)) {
final long startTime = System.nanoTime();
int count = 0;
final DeleteByQueryRequest req = (DeleteByQueryRequest) request;
for (final String index : req.indices()) {
if (indexingProxyService.isTargetIndex(index)) {
count++;
}
}
if (count == 0) {
return null;
} else if (count != req.indices().length) {
throw new ElasticsearchException("Mixed target requests. ({} != {})", count, req.indices().length);
}
return () -> {
return (Response) new BulkByScrollResponse(TimeValue.timeValueNanos(System.nanoTime() - startTime), new BulkByScrollTask.Status(null, 0, 0, 0, 0, 0, 0, 0, 0, 0, TimeValue.ZERO, 0, null, TimeValue.ZERO), Collections.emptyList(), Collections.emptyList(), false);
};
} else if (IndexAction.NAME.equals(action)) {
final IndexRequest req = (IndexRequest) request;
if (!indexingProxyService.isTargetIndex(req.index())) {
return null;
}
return () -> {
final String id = req.id() == null ? INDEX_UUID : req.id();
return (Response) new IndexResponse(new ShardId(new Index(req.index(), INDEX_UUID), 0), req.type(), id, req.version(), true);
};
} else if (UpdateAction.NAME.equals(action)) {
final UpdateRequest req = (UpdateRequest) request;
if (!indexingProxyService.isTargetIndex(req.index())) {
return null;
}
return () -> {
final String id = req.id() == null ? INDEX_UUID : req.id();
return (Response) new UpdateResponse(new ShardId(new Index(req.index(), INDEX_UUID), 0), req.type(), id, req.version(), Result.CREATED);
};
} else if (UpdateByQueryAction.NAME.equals(action)) {
final long startTime = System.nanoTime();
int count = 0;
final UpdateByQueryRequest req = (UpdateByQueryRequest) request;
for (final String index : req.indices()) {
if (indexingProxyService.isTargetIndex(index)) {
count++;
}
}
if (count == 0) {
return null;
} else if (count != req.indices().length) {
throw new ElasticsearchException("Mixed target requests. ({} != {})", count, req.indices().length);
}
return () -> {
return (Response) new BulkByScrollResponse(TimeValue.timeValueNanos(System.nanoTime() - startTime), new BulkByScrollTask.Status(null, 0, 0, 0, 0, 0, 0, 0, 0, 0, TimeValue.ZERO, 0, null, TimeValue.ZERO), Collections.emptyList(), Collections.emptyList(), false);
};
}
return null;
}
use of org.elasticsearch.action.delete.DeleteResponse in project YCSB by brianfrankcooper.
the class ElasticsearchClient method delete.
@Override
public Status delete(final String table, final String key) {
try {
final SearchResponse searchResponse = search(table, key);
if (searchResponse.getHits().totalHits == 0) {
return Status.NOT_FOUND;
}
final String id = searchResponse.getHits().getAt(0).getId();
final DeleteResponse deleteResponse = client.prepareDelete(indexKey, table, id).get();
if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {
return Status.NOT_FOUND;
}
if (!isRefreshNeeded) {
synchronized (this) {
isRefreshNeeded = true;
}
}
return Status.OK;
} catch (final Exception e) {
e.printStackTrace();
return Status.ERROR;
}
}
use of org.elasticsearch.action.delete.DeleteResponse in project xmall by Exrick.
the class ItemESMessageListener method onMessage.
@Override
public void onMessage(Message message) {
try {
// 从消息中取商品id
TextMessage textMessage = (TextMessage) message;
log.info("得到消息:" + textMessage.getText());
String[] text = textMessage.getText().split(",");
Long itemId = new Long(text[1]);
// 等待事务提交
Thread.sleep(1000);
// 更新索引
Settings settings = Settings.builder().put("cluster.name", ES_CLUSTER_NAME).build();
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName(ES_CONNECT_IP), 9300));
if ("add".equals(text[0])) {
// 根据商品id查询商品信息
SearchItem searchItem = itemMapper.getItemById(itemId);
String image = searchItem.getProductImageBig();
if (image != null && !"".equals(image)) {
String[] strings = image.split(",");
image = strings[0];
} else {
image = "";
}
searchItem.setProductImageBig(image);
IndexResponse indexResponse = client.prepareIndex(ITEM_INDEX, ITEM_TYPE, String.valueOf(searchItem.getProductId())).setSource(jsonBuilder().startObject().field("productId", searchItem.getProductId()).field("salePrice", searchItem.getSalePrice()).field("productName", searchItem.getProductName()).field("subTitle", searchItem.getSubTitle()).field("productImageBig", searchItem.getProductImageBig()).field("categoryName", searchItem.getCategoryName()).field("cid", searchItem.getCid()).endObject()).get();
} else if ("delete".equals(text[0])) {
DeleteResponse deleteResponse = client.prepareDelete(ITEM_INDEX, ITEM_TYPE, String.valueOf(itemId)).get();
}
log.info("处理消息成功");
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.elasticsearch.action.delete.DeleteResponse in project elasticsearch by elastic.
the class TransportShardBulkAction method executeUpdateRequest.
/**
* Executes update request, delegating to a index or delete operation after translation,
* handles retries on version conflict and constructs update response
* NOTE: reassigns bulk item request at <code>requestIndex</code> for replicas to
* execute translated update request (NOOP update is an exception). NOOP updates are
* indicated by returning a <code>null</code> operation in {@link BulkItemResultHolder}
* */
private static BulkItemResultHolder executeUpdateRequest(UpdateRequest updateRequest, IndexShard primary, IndexMetaData metaData, BulkShardRequest request, int requestIndex, UpdateHelper updateHelper, LongSupplier nowInMillis, final MappingUpdatePerformer mappingUpdater) throws Exception {
Engine.Result updateOperationResult = null;
UpdateResponse updateResponse = null;
BulkItemRequest replicaRequest = request.items()[requestIndex];
int maxAttempts = updateRequest.retryOnConflict();
for (int attemptCount = 0; attemptCount <= maxAttempts; attemptCount++) {
final UpdateHelper.Result translate;
// translate update request
try {
translate = updateHelper.prepare(updateRequest, primary, nowInMillis);
} catch (Exception failure) {
// we may fail translating a update to index or delete operation
// we use index result to communicate failure while translating update request
updateOperationResult = new Engine.IndexResult(failure, updateRequest.version(), SequenceNumbersService.UNASSIGNED_SEQ_NO);
// out of retry loop
break;
}
// execute translated update request
switch(translate.getResponseResult()) {
case CREATED:
case UPDATED:
IndexRequest indexRequest = translate.action();
MappingMetaData mappingMd = metaData.mappingOrDefault(indexRequest.type());
indexRequest.process(mappingMd, request.index());
updateOperationResult = executeIndexRequestOnPrimary(indexRequest, primary, mappingUpdater);
break;
case DELETED:
DeleteRequest deleteRequest = translate.action();
updateOperationResult = executeDeleteRequestOnPrimary(deleteRequest, primary);
break;
case NOOP:
primary.noopUpdate(updateRequest.type());
break;
default:
throw new IllegalStateException("Illegal update operation " + translate.getResponseResult());
}
if (updateOperationResult == null) {
// this is a noop operation
updateResponse = translate.action();
// out of retry loop
break;
} else if (updateOperationResult.hasFailure() == false) {
// set translated update (index/delete) request for replica execution in bulk items
switch(updateOperationResult.getOperationType()) {
case INDEX:
IndexRequest updateIndexRequest = translate.action();
final IndexResponse indexResponse = new IndexResponse(primary.shardId(), updateIndexRequest.type(), updateIndexRequest.id(), updateOperationResult.getSeqNo(), updateOperationResult.getVersion(), ((Engine.IndexResult) updateOperationResult).isCreated());
BytesReference indexSourceAsBytes = updateIndexRequest.source();
updateResponse = new UpdateResponse(indexResponse.getShardInfo(), indexResponse.getShardId(), indexResponse.getType(), indexResponse.getId(), indexResponse.getSeqNo(), indexResponse.getVersion(), indexResponse.getResult());
if ((updateRequest.fetchSource() != null && updateRequest.fetchSource().fetchSource()) || (updateRequest.fields() != null && updateRequest.fields().length > 0)) {
Tuple<XContentType, Map<String, Object>> sourceAndContent = XContentHelper.convertToMap(indexSourceAsBytes, true, updateIndexRequest.getContentType());
updateResponse.setGetResult(updateHelper.extractGetResult(updateRequest, request.index(), indexResponse.getVersion(), sourceAndContent.v2(), sourceAndContent.v1(), indexSourceAsBytes));
}
// set translated request as replica request
replicaRequest = new BulkItemRequest(request.items()[requestIndex].id(), updateIndexRequest);
break;
case DELETE:
DeleteRequest updateDeleteRequest = translate.action();
DeleteResponse deleteResponse = new DeleteResponse(primary.shardId(), updateDeleteRequest.type(), updateDeleteRequest.id(), updateOperationResult.getSeqNo(), updateOperationResult.getVersion(), ((Engine.DeleteResult) updateOperationResult).isFound());
updateResponse = new UpdateResponse(deleteResponse.getShardInfo(), deleteResponse.getShardId(), deleteResponse.getType(), deleteResponse.getId(), deleteResponse.getSeqNo(), deleteResponse.getVersion(), deleteResponse.getResult());
updateResponse.setGetResult(updateHelper.extractGetResult(updateRequest, request.index(), deleteResponse.getVersion(), translate.updatedSourceAsMap(), translate.updateSourceContentType(), null));
// set translated request as replica request
replicaRequest = new BulkItemRequest(request.items()[requestIndex].id(), updateDeleteRequest);
break;
}
assert updateOperationResult.getSeqNo() != SequenceNumbersService.UNASSIGNED_SEQ_NO;
// out of retry loop
break;
} else if (updateOperationResult.getFailure() instanceof VersionConflictEngineException == false) {
// out of retry loop
break;
}
}
return new BulkItemResultHolder(updateResponse, updateOperationResult, replicaRequest);
}
use of org.elasticsearch.action.delete.DeleteResponse 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);
}
}
Aggregations