Search in sources :

Example 11 with DeleteByQueryRequest

use of org.elasticsearch.index.reindex.DeleteByQueryRequest in project elasticsearch-indexing-proxy by codelibs.

the class RequestUtils method createDeleteByQueryRequest.

public static DeleteByQueryRequestBuilder createDeleteByQueryRequest(final Client client, final StreamInput streamInput, final String index) throws IOException {
    final DeleteByQueryRequestBuilder builder = client.prepareExecute(DeleteByQueryAction.INSTANCE);
    final DeleteByQueryRequest request = builder.request();
    request.readFrom(streamInput);
    if (index != null) {
        request.indices(index);
    }
    return builder;
}
Also used : DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) DeleteByQueryRequestBuilder(org.elasticsearch.index.reindex.DeleteByQueryRequestBuilder)

Example 12 with DeleteByQueryRequest

use of org.elasticsearch.index.reindex.DeleteByQueryRequest 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

DeleteByQueryRequest (org.elasticsearch.index.reindex.DeleteByQueryRequest)12 BulkByScrollResponse (org.elasticsearch.index.reindex.BulkByScrollResponse)6 UpdateByQueryRequest (org.elasticsearch.index.reindex.UpdateByQueryRequest)5 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)4 IOException (java.io.IOException)3 ElasticsearchException (org.elasticsearch.ElasticsearchException)3 ArrayList (java.util.ArrayList)2 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)2 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)2 IndexRequest (org.elasticsearch.action.index.IndexRequest)2 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)2 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)2 Path (java.nio.file.Path)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 ClusterSearchShardsRequest (org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsRequest)1 IndicesAliasesRequest (org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest)1 GetAliasesRequest (org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest)1