Search in sources :

Example 6 with DeleteByQueryRequest

use of org.elasticsearch.index.reindex.DeleteByQueryRequest in project hazelcast by hazelcast.

the class BaseElasticTest method deleteDocuments.

/**
 * Deletes all documents in all indexes
 */
protected void deleteDocuments() throws IOException {
    DeleteByQueryRequest request = new DeleteByQueryRequest("*").setQuery(matchAllQuery()).setRefresh(true);
    elasticClient.deleteByQuery(request, DEFAULT);
}
Also used : DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest)

Example 7 with DeleteByQueryRequest

use of org.elasticsearch.index.reindex.DeleteByQueryRequest in project snow-owl by b2ihealthcare.

the class EsHttpClient method deleteByQuery.

@Override
public BulkByScrollResponse deleteByQuery(String index, int batchSize, QueryBuilder query) throws IOException {
    checkHealthy(index);
    DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(index).setBatchSize(batchSize).setQuery(query).setSlices(DeleteByQueryRequest.AUTO_SLICES).setAbortOnVersionConflict(false);
    return client.deleteByQuery(deleteByQueryRequest, EXTENDED_DEFAULT);
}
Also used : DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest)

Example 8 with DeleteByQueryRequest

use of org.elasticsearch.index.reindex.DeleteByQueryRequest in project ranger by apache.

the class RequestUtils method getIndexFromRequest.

// To support all kinds of request in elasticsearch
public static <Request extends ActionRequest> List<String> getIndexFromRequest(Request request) {
    List<String> indexs = new ArrayList<>();
    if (request instanceof SingleShardRequest) {
        indexs.add(((SingleShardRequest<?>) request).index());
        return indexs;
    }
    if (request instanceof ReplicationRequest) {
        indexs.add(((ReplicationRequest<?>) request).index());
        return indexs;
    }
    if (request instanceof InstanceShardOperationRequest) {
        indexs.add(((InstanceShardOperationRequest<?>) request).index());
        return indexs;
    }
    if (request instanceof CreateIndexRequest) {
        indexs.add(((CreateIndexRequest) request).index());
        return indexs;
    }
    if (request instanceof PutMappingRequest) {
        if (((PutMappingRequest) request).getConcreteIndex() != null) {
            indexs.add(((PutMappingRequest) request).getConcreteIndex().getName());
            return indexs;
        } else {
            return Arrays.asList(((PutMappingRequest) request).indices());
        }
    }
    if (request instanceof SearchRequest) {
        return Arrays.asList(((SearchRequest) request).indices());
    }
    if (request instanceof IndicesStatsRequest) {
        return Arrays.asList(((IndicesStatsRequest) request).indices());
    }
    if (request instanceof OpenIndexRequest) {
        return Arrays.asList(((OpenIndexRequest) request).indices());
    }
    if (request instanceof DeleteIndexRequest) {
        return Arrays.asList(((DeleteIndexRequest) request).indices());
    }
    if (request instanceof BulkRequest) {
        @SuppressWarnings("rawtypes") List<DocWriteRequest<?>> requests = ((BulkRequest) request).requests();
        if (CollectionUtils.isNotEmpty(requests)) {
            for (DocWriteRequest<?> docWriteRequest : requests) {
                indexs.add(docWriteRequest.index());
            }
            return indexs;
        }
    }
    if (request instanceof MultiGetRequest) {
        List<Item> items = ((MultiGetRequest) request).getItems();
        if (CollectionUtils.isNotEmpty(items)) {
            for (Item item : items) {
                indexs.add(item.index());
            }
            return indexs;
        }
    }
    if (request instanceof GetMappingsRequest) {
        return Arrays.asList(((GetMappingsRequest) request).indices());
    }
    if (request instanceof GetSettingsRequest) {
        return Arrays.asList(((GetSettingsRequest) request).indices());
    }
    if (request instanceof IndicesExistsRequest) {
        return Arrays.asList(((IndicesExistsRequest) request).indices());
    }
    if (request instanceof GetAliasesRequest) {
        return Arrays.asList(((GetAliasesRequest) request).indices());
    }
    if (request instanceof GetIndexRequest) {
        return Arrays.asList(((GetIndexRequest) request).indices());
    }
    if (request instanceof GetFieldMappingsRequest) {
        return Arrays.asList(((GetFieldMappingsRequest) request).indices());
    }
    if (request instanceof TypesExistsRequest) {
        return Arrays.asList(((TypesExistsRequest) request).indices());
    }
    if (request instanceof ValidateQueryRequest) {
        return Arrays.asList(((ValidateQueryRequest) request).indices());
    }
    if (request instanceof RecoveryRequest) {
        return Arrays.asList(((RecoveryRequest) request).indices());
    }
    if (request instanceof IndicesSegmentsRequest) {
        return Arrays.asList(((IndicesSegmentsRequest) request).indices());
    }
    if (request instanceof IndicesShardStoresRequest) {
        return Arrays.asList(((IndicesShardStoresRequest) request).indices());
    }
    if (request instanceof UpgradeStatusRequest) {
        return Arrays.asList(((UpgradeStatusRequest) request).indices());
    }
    if (request instanceof ClusterSearchShardsRequest) {
        return Arrays.asList(((ClusterSearchShardsRequest) request).indices());
    }
    if (request instanceof IndicesAliasesRequest) {
        List<IndicesAliasesRequest.AliasActions> aliasActions = ((IndicesAliasesRequest) request).getAliasActions();
        if (CollectionUtils.isNotEmpty(aliasActions)) {
            for (IndicesAliasesRequest.AliasActions action : aliasActions) {
                indexs.addAll(Arrays.asList(action.indices()));
            }
            return indexs;
        }
    }
    if (request instanceof ClearIndicesCacheRequest) {
        return Arrays.asList(((ClearIndicesCacheRequest) request).indices());
    }
    if (request instanceof CloseIndexRequest) {
        return Arrays.asList(((CloseIndexRequest) request).indices());
    }
    if (request instanceof FlushRequest) {
        return Arrays.asList(((FlushRequest) request).indices());
    }
    if (request instanceof SyncedFlushRequest) {
        return Arrays.asList(((SyncedFlushRequest) request).indices());
    }
    if (request instanceof ForceMergeRequest) {
        return Arrays.asList(((ForceMergeRequest) request).indices());
    }
    if (request instanceof RefreshRequest) {
        return Arrays.asList(((RefreshRequest) request).indices());
    }
    if (request instanceof RolloverRequest) {
        return Arrays.asList(((RolloverRequest) request).indices());
    }
    if (request instanceof UpdateSettingsRequest) {
        return Arrays.asList(((UpdateSettingsRequest) request).indices());
    }
    if (request instanceof ResizeRequest) {
        return Arrays.asList(((ResizeRequest) request).indices());
    }
    if (request instanceof DeleteIndexTemplateRequest) {
        indexs.add(((DeleteIndexTemplateRequest) request).name());
        return indexs;
    }
    if (request instanceof GetIndexTemplatesRequest) {
        return Arrays.asList(((GetIndexTemplatesRequest) request).names());
    }
    if (request instanceof PutIndexTemplateRequest) {
        indexs.add(((PutIndexTemplateRequest) request).name());
        return indexs;
    }
    if (request instanceof UpgradeRequest) {
        return Arrays.asList(((UpgradeRequest) request).indices());
    }
    if (request instanceof FieldCapabilitiesRequest) {
        return Arrays.asList(((FieldCapabilitiesRequest) request).indices());
    }
    if (request instanceof MultiSearchRequest) {
        List<SearchRequest> searchRequests = ((MultiSearchRequest) request).requests();
        if (CollectionUtils.isNotEmpty(searchRequests)) {
            for (SearchRequest singleRequest : searchRequests) {
                indexs.addAll(Arrays.asList(singleRequest.indices()));
            }
            return indexs;
        }
    }
    if (request instanceof MultiTermVectorsRequest) {
        List<TermVectorsRequest> termVectorsRequests = ((MultiTermVectorsRequest) request).getRequests();
        if (CollectionUtils.isNotEmpty(termVectorsRequests)) {
            for (TermVectorsRequest singleRequest : termVectorsRequests) {
                indexs.addAll(Arrays.asList(singleRequest.indices()));
            }
            return indexs;
        }
    }
    if (request instanceof UpdateByQueryRequest) {
        return Arrays.asList(((UpdateByQueryRequest) request).indices());
    }
    if (request instanceof DeleteByQueryRequest) {
        return Arrays.asList(((DeleteByQueryRequest) request).indices());
    }
    if (request instanceof ReindexRequest) {
        indexs.addAll(Arrays.asList(((ReindexRequest) request).getSearchRequest().indices()));
        indexs.addAll(Arrays.asList(((ReindexRequest) request).getDestination().indices()));
        return indexs;
    }
    // ClearScrollRequest does not carry any index, so return empty List
    if (request instanceof ClearScrollRequest) {
        return indexs;
    }
    // No matched request type to find specific index , set default value *
    indexs.add("*");
    return indexs;
}
Also used : ForceMergeRequest(org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest) IndicesShardStoresRequest(org.elasticsearch.action.admin.indices.shards.IndicesShardStoresRequest) TypesExistsRequest(org.elasticsearch.action.admin.indices.exists.types.TypesExistsRequest) ClearIndicesCacheRequest(org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) ArrayList(java.util.ArrayList) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) ReplicationRequest(org.elasticsearch.action.support.replication.ReplicationRequest) DeleteIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) SyncedFlushRequest(org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest) InstanceShardOperationRequest(org.elasticsearch.action.support.single.instance.InstanceShardOperationRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) Item(org.elasticsearch.action.get.MultiGetRequest.Item) ReindexRequest(org.elasticsearch.index.reindex.ReindexRequest) SingleShardRequest(org.elasticsearch.action.support.single.shard.SingleShardRequest) RolloverRequest(org.elasticsearch.action.admin.indices.rollover.RolloverRequest) GetFieldMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest) IndicesSegmentsRequest(org.elasticsearch.action.admin.indices.segments.IndicesSegmentsRequest) ClusterSearchShardsRequest(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsRequest) IndicesAliasesRequest(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) RefreshRequest(org.elasticsearch.action.admin.indices.refresh.RefreshRequest) TermVectorsRequest(org.elasticsearch.action.termvectors.TermVectorsRequest) MultiTermVectorsRequest(org.elasticsearch.action.termvectors.MultiTermVectorsRequest) SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) PutMappingRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest) UpdateSettingsRequest(org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest) UpgradeRequest(org.elasticsearch.action.admin.indices.upgrade.post.UpgradeRequest) GetIndexTemplatesRequest(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest) PutIndexTemplateRequest(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest) RecoveryRequest(org.elasticsearch.action.admin.indices.recovery.RecoveryRequest) SyncedFlushRequest(org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest) FlushRequest(org.elasticsearch.action.admin.indices.flush.FlushRequest) UpgradeStatusRequest(org.elasticsearch.action.admin.indices.upgrade.get.UpgradeStatusRequest) GetIndexRequest(org.elasticsearch.action.admin.indices.get.GetIndexRequest) GetAliasesRequest(org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) ValidateQueryRequest(org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest) GetMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) GetSettingsRequest(org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest) CloseIndexRequest(org.elasticsearch.action.admin.indices.close.CloseIndexRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) MultiTermVectorsRequest(org.elasticsearch.action.termvectors.MultiTermVectorsRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) FieldCapabilitiesRequest(org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest) ResizeRequest(org.elasticsearch.action.admin.indices.shrink.ResizeRequest) IndicesStatsRequest(org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest)

Example 9 with DeleteByQueryRequest

use of org.elasticsearch.index.reindex.DeleteByQueryRequest in project pancm_project by xuwujing.

the class EsHighLevelRestTest1 method deleteByQuery.

/**
 * 根据查询条件删除
 *
 * @throws IOException
 */
private static void deleteByQuery() throws IOException {
    String type = "_doc";
    String index = "test1";
    DeleteByQueryRequest request = new DeleteByQueryRequest(index, type);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.sort("sendtime", SortOrder.ASC);
    // 设置索引库表达式
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    boolQueryBuilder.must(QueryBuilders.termQuery("uid", 123456));
    sourceBuilder.query(boolQueryBuilder);
    // 设置查询条件
    request.setQuery(sourceBuilder.query());
    request.setSize(1);
    // 同步执行
    BulkByScrollResponse bulkResponse = client.deleteByQuery(request, RequestOptions.DEFAULT);
    // 异步执行
    // client.updateByQueryAsync(request, RequestOptions.DEFAULT, listener);
    // 返回结果
    TimeValue timeTaken = bulkResponse.getTook();
    boolean timedOut = bulkResponse.isTimedOut();
    long totalDocs = bulkResponse.getTotal();
    long updatedDocs = bulkResponse.getUpdated();
    long deletedDocs = bulkResponse.getDeleted();
    long batches = bulkResponse.getBatches();
    long noops = bulkResponse.getNoops();
    long versionConflicts = bulkResponse.getVersionConflicts();
    long bulkRetries = bulkResponse.getBulkRetries();
    long searchRetries = bulkResponse.getSearchRetries();
    TimeValue throttledMillis = bulkResponse.getStatus().getThrottled();
    TimeValue throttledUntilMillis = bulkResponse.getStatus().getThrottledUntil();
    List<ScrollableHitSource.SearchFailure> searchFailures = bulkResponse.getSearchFailures();
    List<BulkItemResponse.Failure> bulkFailures = bulkResponse.getBulkFailures();
    System.out.println("查询更新总共花费了:" + timeTaken.getMillis() + " 毫秒,总条数:" + totalDocs + ",更新数:" + updatedDocs);
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse)

Example 10 with DeleteByQueryRequest

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

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