Search in sources :

Example 1 with BulkByScrollResponse

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

the class EsHighLevelRestTest2 method reindex.

/**
 * 索引复制
 *
 * @throws IOException
 */
private static void reindex() throws IOException {
    // 创建索引复制请求并进行索引复制
    ReindexRequest request = new ReindexRequest();
    // 需要复制的索引
    request.setSourceIndices("user");
    // 复制的目标索引
    request.setDestIndex("dest_test");
    // 表示如果在复制索引的时候有缺失的文档的话会进行创建,默认是index
    request.setDestOpType("create");
    // 如果在复制的过程中发现版本冲突,那么会继续进行复制
    request.setConflicts("proceed");
    // 只复制文档类型为 userindex 的数据
    request.setSourceDocTypes("userindex");
    // 只复制 pancm 用户的数据
    request.setSourceQuery(new TermQueryBuilder("user", "pancm"));
    // 设置复制文档的数量
    request.setSize(10);
    // 设置一次批量处理的条数,默认是1000
    request.setSourceBatchSize(100);
    // 进行排序
    // request.addSortField("postDate", SortOrder.DESC);
    // 指定切片大小
    request.setSlices(2);
    // 设置超时时间
    request.setTimeout(TimeValue.timeValueMinutes(2));
    // 允许刷新
    request.setRefresh(true);
    // 同步执行
    BulkByScrollResponse bulkResponse = client.reindex(request, RequestOptions.DEFAULT);
    // 异步执行
    // client.reindexAsync(request, RequestOptions.DEFAULT, listener);
    // 响应结果处理
    TimeValue timeTaken = bulkResponse.getTook();
    boolean timedOut = bulkResponse.isTimedOut();
    long totalDocs = bulkResponse.getTotal();
    long updatedDocs = bulkResponse.getUpdated();
    long createdDocs = bulkResponse.getCreated();
    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 + ",创建数:" + createdDocs + ",更新数:" + updatedDocs);
}
Also used : ReindexRequest(org.elasticsearch.index.reindex.ReindexRequest) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) TimeValue(org.elasticsearch.common.unit.TimeValue) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse)

Example 2 with BulkByScrollResponse

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

the class EsHighLevelRestTest2 method deleteByQuery.

/**
 * 根据查询条件删除
 * @throws IOException
 */
private static void deleteByQuery() throws IOException {
    // 
    DeleteByQueryRequest request = new DeleteByQueryRequest("user");
    // 设置查询条件
    request.setQuery(new TermQueryBuilder("user", "pancm"));
    // 设置复制文档的数量
    request.setSize(10);
    // 设置一次批量处理的条数,默认是1000
    request.setBatchSize(100);
    // 设置路由
    request.setRouting("=cat");
    // 设置超时时间
    request.setTimeout(TimeValue.timeValueMinutes(2));
    // 允许刷新
    request.setRefresh(true);
    // 索引选项
    request.setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN);
    // 同步执行
    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 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 + ",删除数:" + deletedDocs);
}
Also used : DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) TimeValue(org.elasticsearch.common.unit.TimeValue) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse)

Example 3 with BulkByScrollResponse

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

the class IpHandler method updateByQuery.

/**
 * @return boolean
 * @Author pancm
 * @Description 根据条件更新
 * @Date 2019/3/21
 * @Param []
 */
public static Map<String, Object> updateByQuery(String index, String type, QueryBuilder... queryBuilders) throws IOException {
    if (index == null || type == null) {
        return null;
    }
    Map<String, Object> map = new HashMap<>();
    try {
        UpdateByQueryRequest request = new UpdateByQueryRequest();
        request.indices(index);
        request.setDocTypes(type);
        if (queryBuilders != null) {
            for (QueryBuilder queryBuilder : queryBuilders) {
                request.setQuery(queryBuilder);
            }
        }
        // 同步执行
        BulkByScrollResponse bulkResponse = client.updateByQuery(request, RequestOptions.DEFAULT);
        // 响应结果处理
        map.put("time", bulkResponse.getTook().getMillis());
        map.put("total", bulkResponse.getTotal());
    } finally {
        if (isAutoClose) {
            close();
        }
    }
    return map;
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse)

Example 4 with BulkByScrollResponse

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

the class IpHandler method deleteByQuery.

/**
 * @return Map
 * @Author pancm
 * @Description //根据条件删除数据
 * @Date 2019/3/21
 * @Param []
 */
public static Map<String, Object> deleteByQuery(String index, String type, QueryBuilder[] queryBuilders) throws IOException {
    if (index == null || type == null || queryBuilders == null) {
        return null;
    }
    Map<String, Object> map = new HashMap<>();
    try {
        DeleteByQueryRequest request = new DeleteByQueryRequest(index, type);
        if (queryBuilders != null) {
            for (QueryBuilder queryBuilder : queryBuilders) {
                request.setQuery(queryBuilder);
            }
        }
        // 同步执行
        BulkByScrollResponse bulkResponse = client.deleteByQuery(request, RequestOptions.DEFAULT);
        // 响应结果处理
        map.put("time", bulkResponse.getTook().getMillis());
        map.put("total", bulkResponse.getTotal());
    } finally {
        if (isAutoClose) {
            close();
        }
    }
    return map;
}
Also used : DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse)

Example 5 with BulkByScrollResponse

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

the class EsHighLevelRestTest1 method updateByQuery.

/**
 * 根据查询条件更新
 *
 * @throws IOException
 */
private static void updateByQuery() throws IOException {
    String type = "_doc";
    String index = "test1";
    // 
    UpdateByQueryRequest request = new UpdateByQueryRequest(index, type);
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    boolQueryBuilder.must(QueryBuilders.termQuery("user", "pancm"));
    // Script script = new Script(ScriptType.INLINE, SCRIPT_NAME, id, map);
    // boolQueryBuilder.must(QueryBuilders.scriptQuery(script));
    // boolQueryBuilder.must(QueryBuilders.scriptQuery(script));
    // // 设置查询条件
    // request.setQuery(new TermQueryBuilder("user", "pancm"));
    // 设置复制文档的数量
    request.setSize(10);
    // 设置一次批量处理的条数,默认是1000
    request.setBatchSize(100);
    // 设置超时时间
    request.setTimeout(TimeValue.timeValueMinutes(2));
    // 索引选项
    request.setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN);
    // 同步执行
    BulkByScrollResponse bulkResponse = client.updateByQuery(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) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) TimeValue(org.elasticsearch.common.unit.TimeValue) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse)

Aggregations

BulkByScrollResponse (org.elasticsearch.index.reindex.BulkByScrollResponse)28 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)6 DeleteByQueryRequest (org.elasticsearch.index.reindex.DeleteByQueryRequest)6 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)5 TimeValue (org.elasticsearch.common.unit.TimeValue)4 UpdateByQueryRequest (org.elasticsearch.index.reindex.UpdateByQueryRequest)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