use of org.elasticsearch.index.reindex.UpdateByQueryRequest 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.index.reindex.UpdateByQueryRequest in project elasticsearch-indexing-proxy by codelibs.
the class RequestUtils method createUpdateByQueryRequest.
public static UpdateByQueryRequestBuilder createUpdateByQueryRequest(final Client client, final StreamInput streamInput, final String index) throws IOException {
final UpdateByQueryRequestBuilder builder = client.prepareExecute(UpdateByQueryAction.INSTANCE);
final UpdateByQueryRequest request = builder.request();
request.readFrom(streamInput);
if (index != null) {
request.indices(index);
}
return builder;
}
use of org.elasticsearch.index.reindex.UpdateByQueryRequest in project elasticsearch-indexing-proxy by codelibs.
the class RequestUtils method createBulkRequest.
public static BulkRequestBuilder createBulkRequest(final Client client, final StreamInput streamInput, final String index) throws IOException {
final BulkRequestBuilder builder = client.prepareBulk();
final BulkRequest request = builder.request();
request.readFrom(streamInput);
if (index != null) {
request.requests().stream().forEach(req -> {
if (req instanceof DeleteRequest) {
((DeleteRequest) req).index(index);
} else if (req instanceof DeleteByQueryRequest) {
((DeleteByQueryRequest) req).indices(index);
} else if (req instanceof IndexRequest) {
((IndexRequest) req).index(index);
} else if (req instanceof UpdateRequest) {
((UpdateRequest) req).index(index);
} else if (req instanceof UpdateByQueryRequest) {
((UpdateByQueryRequest) req).indices(index);
} else {
throw new ElasticsearchException("Unsupported request in bulk: " + req);
}
});
}
return builder;
}
use of org.elasticsearch.index.reindex.UpdateByQueryRequest in project elasticsearch-indexing-proxy by codelibs.
the class IndexingProxyService method dumpRequests.
public void dumpRequests(final int filePosition, ActionListener<String> listener) {
final Path path = dataPath.resolve(String.format(dataFileFormat, filePosition) + IndexingProxyPlugin.DATA_EXTENTION);
if (FileAccessUtils.existsFile(path)) {
try (IndexingProxyStreamInput streamInput = AccessController.doPrivileged((PrivilegedAction<IndexingProxyStreamInput>) () -> {
try {
return new IndexingProxyStreamInput(Files.newInputStream(path), namedWriteableRegistry);
} catch (final IOException e) {
throw new ElasticsearchException("Failed to read " + path.toAbsolutePath(), e);
}
})) {
final StringBuilder buf = new StringBuilder(10000);
while (streamInput.available() > 0) {
final short classType = streamInput.readShort();
switch(classType) {
case RequestUtils.TYPE_DELETE:
DeleteRequest deleteRequest = RequestUtils.createDeleteRequest(client, streamInput, null).request();
buf.append(deleteRequest.toString());
break;
case RequestUtils.TYPE_DELETE_BY_QUERY:
DeleteByQueryRequest deleteByQueryRequest = RequestUtils.createDeleteByQueryRequest(client, streamInput, null).request();
buf.append(deleteByQueryRequest.toString());
buf.append(' ');
buf.append(deleteByQueryRequest.getSearchRequest().toString().replace("\n", ""));
break;
case RequestUtils.TYPE_INDEX:
IndexRequest indexRequest = RequestUtils.createIndexRequest(client, streamInput, null).request();
buf.append(indexRequest.toString());
break;
case RequestUtils.TYPE_UPDATE:
UpdateRequest updateRequest = RequestUtils.createUpdateRequest(client, streamInput, null).request();
buf.append("update {[").append(updateRequest.index()).append("][").append(updateRequest.type()).append("][").append(updateRequest.id()).append("] source[").append(updateRequest.toXContent(JsonXContent.contentBuilder(), ToXContent.EMPTY_PARAMS).string()).append("]}");
break;
case RequestUtils.TYPE_UPDATE_BY_QUERY:
UpdateByQueryRequest updateByQueryRequest = RequestUtils.createUpdateByQueryRequest(client, streamInput, null).request();
buf.append(updateByQueryRequest.toString());
buf.append(' ');
buf.append(updateByQueryRequest.getSearchRequest().toString().replace("\n", ""));
break;
case RequestUtils.TYPE_BULK:
BulkRequest bulkRequest = RequestUtils.createBulkRequest(client, streamInput, null).request();
buf.append("bulk [");
buf.append(bulkRequest.requests().stream().map(req -> {
if (req instanceof UpdateRequest) {
UpdateRequest upreq = (UpdateRequest) req;
try {
return "update {[" + upreq.index() + "][" + upreq.type() + "][" + upreq.id() + "] source[" + upreq.toXContent(JsonXContent.contentBuilder(), ToXContent.EMPTY_PARAMS).string() + "]}";
} catch (IOException e) {
return e.getMessage();
}
} else {
return req.toString();
}
}).collect(Collectors.joining(",")));
buf.append("]");
break;
default:
listener.onFailure(new ElasticsearchException("Unknown request type: " + classType));
}
buf.append('\n');
}
listener.onResponse(buf.toString());
} catch (IOException e) {
listener.onFailure(e);
}
} else {
listener.onFailure(new ElasticsearchException("The data file does not exist: " + dataPath));
}
}
use of org.elasticsearch.index.reindex.UpdateByQueryRequest in project gora by apache.
the class ElasticsearchStore method deleteByQuery.
@Override
public long deleteByQuery(Query<K, T> query) throws GoraException {
try {
BulkByScrollResponse bulkResponse;
if (query.getFields() != null && query.getFields().length < elasticsearchMapping.getFields().size()) {
UpdateByQueryRequest updateRequest = new UpdateByQueryRequest(elasticsearchMapping.getIndexName());
QueryBuilder matchDocumentsWithinRange = QueryBuilders.rangeQuery("gora_id").from(query.getStartKey()).to(query.getEndKey());
updateRequest.setQuery(matchDocumentsWithinRange);
// Create a script for deleting fields
StringBuilder toDelete = new StringBuilder();
String[] fieldsToDelete = query.getFields();
for (String field : fieldsToDelete) {
String elasticsearchField = elasticsearchMapping.getFields().get(field).getName();
toDelete.append(String.format(Locale.getDefault(), "ctx._source.remove('%s');", elasticsearchField));
}
// toDelete.deleteCharAt(toDelete.length() - 1);
updateRequest.setScript(new Script(ScriptType.INLINE, "painless", toDelete.toString(), Collections.emptyMap()));
bulkResponse = client.updateByQuery(updateRequest, RequestOptions.DEFAULT);
return bulkResponse.getUpdated();
} else {
DeleteByQueryRequest deleteRequest = new DeleteByQueryRequest(elasticsearchMapping.getIndexName());
QueryBuilder matchDocumentsWithinRange = QueryBuilders.rangeQuery("gora_id").from(query.getStartKey()).to(query.getEndKey());
deleteRequest.setQuery(matchDocumentsWithinRange);
bulkResponse = client.deleteByQuery(deleteRequest, RequestOptions.DEFAULT);
return bulkResponse.getDeleted();
}
} catch (IOException ex) {
throw new GoraException(ex);
}
}
Aggregations