Search in sources :

Example 26 with BulkByScrollResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.reindex.BulkByScrollResponse 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 27 with BulkByScrollResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.reindex.BulkByScrollResponse in project pancm_project by xuwujing.

the class IpHandler method reindexByQuery.

/**
 * @return Map
 * @Author pancm
 * @Description //重索引
 * @Date 2019/3/21
 * @Param []
 */
public static Map<String, Object> reindexByQuery(String index, String destIndex, QueryBuilder[] queryBuilders) throws IOException {
    if (index == null || destIndex == null) {
        return null;
    }
    Map<String, Object> map = new HashMap<>();
    try {
        // 创建索引复制请求并进行索引复制
        ReindexRequest request = new ReindexRequest();
        // 需要复制的索引
        request.setSourceIndices(index);
        /* 复制的目标索引 */
        request.setDestIndex(destIndex);
        if (queryBuilders != null) {
            for (QueryBuilder queryBuilder : queryBuilders) {
                request.setSourceQuery(queryBuilder);
            }
        }
        // 表示如果在复制索引的时候有缺失的文档的话会进行创建,默认是index
        request.setDestOpType("create");
        // 如果在复制的过程中发现版本冲突,那么会继续进行复制
        request.setConflicts("proceed");
        // 设置复制文档的数量
        // request.setSize(10);
        // 设置一次批量处理的条数,默认是1000
        // request.setSourceBatchSize(10000);
        // 设置超时时间
        request.setTimeout(TimeValue.timeValueMinutes(2));
        // 同步执行
        BulkByScrollResponse bulkResponse = client.reindex(request, RequestOptions.DEFAULT);
        // 响应结果处理
        map.put("time", bulkResponse.getTook().getMillis());
        map.put("total", bulkResponse.getTotal());
        map.put("createdDocs", bulkResponse.getCreated());
        map.put("updatedDocs", bulkResponse.getUpdated());
    } finally {
        if (isAutoClose) {
            close();
        }
    }
    return map;
}
Also used : ReindexRequest(org.elasticsearch.index.reindex.ReindexRequest) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse)

Example 28 with BulkByScrollResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.reindex.BulkByScrollResponse in project vertigo by KleeGroup.

the class ESStatement method remove.

/**
 * Supprime des documents.
 * @param query Requete de filtrage des documents à supprimer
 */
void remove(final ListFilter query) {
    Assertion.checkNotNull(query);
    // -----
    try {
        final QueryBuilder queryBuilder = ESSearchRequestBuilder.translateToQueryBuilder(query);
        final DeleteByQueryRequestBuilder deleteByQueryAction = DeleteByQueryAction.INSTANCE.newRequestBuilder(esClient).filter(queryBuilder);
        deleteByQueryAction.source().setIndices(indexName).setTypes(typeName);
        final BulkByScrollResponse response = deleteByQueryAction.get();
        final long deleted = response.getDeleted();
        LOGGER.debug("Removed {0} elements", deleted);
    } catch (final SearchPhaseExecutionException e) {
        final VUserException vue = new VUserException(SearchResource.DYNAMO_SEARCH_QUERY_SYNTAX_ERROR);
        vue.initCause(e);
        throw vue;
    }
}
Also used : SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) DeleteByQueryRequestBuilder(org.elasticsearch.index.reindex.DeleteByQueryRequestBuilder) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) VUserException(io.vertigo.lang.VUserException)

Example 29 with BulkByScrollResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.reindex.BulkByScrollResponse 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)

Example 30 with BulkByScrollResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.reindex.BulkByScrollResponse in project graylog2-server by Graylog2.

the class IndicesAdapterES7 method move.

@Override
public void move(String source, String target, Consumer<IndexMoveResult> resultCallback) {
    final ReindexRequest request = new ReindexRequest();
    request.setSourceIndices(source);
    request.setDestIndex(target);
    final BulkByScrollResponse result = client.execute((c, requestOptions) -> c.reindex(request, requestOptions));
    final IndexMoveResult indexMoveResult = IndexMoveResult.create(Math.toIntExact(result.getTotal()), result.getTook().millis(), !result.getBulkFailures().isEmpty());
    resultCallback.accept(indexMoveResult);
}
Also used : ReindexRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.index.reindex.ReindexRequest) IndexMoveResult(org.graylog2.indexer.indices.IndexMoveResult) BulkByScrollResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.index.reindex.BulkByScrollResponse)

Aggregations

BulkByScrollResponse (org.elasticsearch.index.reindex.BulkByScrollResponse)29 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)6 DeleteByQueryRequest (org.elasticsearch.index.reindex.DeleteByQueryRequest)6 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)5 UpdateByQueryRequest (org.elasticsearch.index.reindex.UpdateByQueryRequest)5 TimeValue (org.elasticsearch.common.unit.TimeValue)4 IOException (java.io.IOException)3 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)2 DeleteByQueryRequestBuilder (org.elasticsearch.index.reindex.DeleteByQueryRequestBuilder)2 ReindexRequest (org.elasticsearch.index.reindex.ReindexRequest)2 EsQueryBuilder (com.b2international.index.es.query.EsQueryBuilder)1 SearchException (core.framework.search.SearchException)1 StopWatch (core.framework.util.StopWatch)1 VUserException (io.vertigo.lang.VUserException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 GoraException (org.apache.gora.util.GoraException)1 ActionResponse (org.elasticsearch.action.ActionResponse)1 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)1