Search in sources :

Example 1 with UpdateByQueryRequest

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;
}
Also used : Index(org.elasticsearch.index.Index) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexRequest(org.elasticsearch.action.index.IndexRequest) ReplicationResponse(org.elasticsearch.action.support.replication.ReplicationResponse) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) ShardId(org.elasticsearch.index.shard.ShardId) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) ArrayList(java.util.ArrayList) List(java.util.List) BulkByScrollTask(org.elasticsearch.index.reindex.BulkByScrollTask) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) ActionResponse(org.elasticsearch.action.ActionResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) ReplicationResponse(org.elasticsearch.action.support.replication.ReplicationResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 2 with UpdateByQueryRequest

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;
}
Also used : UpdateByQueryRequestBuilder(org.elasticsearch.index.reindex.UpdateByQueryRequestBuilder) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest)

Example 3 with UpdateByQueryRequest

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;
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest)

Example 4 with UpdateByQueryRequest

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));
    }
}
Also used : Path(java.nio.file.Path) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) IndexingProxyStreamInput(org.codelibs.elasticsearch.idxproxy.stream.IndexingProxyStreamInput) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest)

Example 5 with UpdateByQueryRequest

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);
    }
}
Also used : Script(org.elasticsearch.script.Script) GoraException(org.apache.gora.util.GoraException) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IOException(java.io.IOException) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse)

Aggregations

UpdateByQueryRequest (org.elasticsearch.index.reindex.UpdateByQueryRequest)5 DeleteByQueryRequest (org.elasticsearch.index.reindex.DeleteByQueryRequest)4 ElasticsearchException (org.elasticsearch.ElasticsearchException)3 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)3 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)3 IndexRequest (org.elasticsearch.action.index.IndexRequest)3 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)3 IOException (java.io.IOException)2 BulkByScrollResponse (org.elasticsearch.index.reindex.BulkByScrollResponse)2 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 GoraException (org.apache.gora.util.GoraException)1 IndexingProxyStreamInput (org.codelibs.elasticsearch.idxproxy.stream.IndexingProxyStreamInput)1 ActionResponse (org.elasticsearch.action.ActionResponse)1 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)1 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)1 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)1 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)1 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)1