org.elasticsearch.index.reindex.ReindexRequest Java Examples

The following examples show how to use org.elasticsearch.index.reindex.ReindexRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReindexQueryParser.java    From elasticsearch-sql with MIT License 5 votes vote down vote up
private void buildScript(ElasticsearchParser.ReindexOperationContext reindexOperationContext, ReindexRequest reindexRequest) {
    StringBuilder script = new StringBuilder();
    for (ElasticsearchParser.NameOperandContext nameOperandContext : reindexOperationContext.fieldList().nameOperand()) {
        if (nameOperandContext.exclude != null) {
            script.append("ctx._source.remove('").append(nameOperandContext.fieldName.getText()).append("')';");
        }
    }
    if (StringUtils.isNotBlank(script.toString())) {
        reindexRequest.setScript(new Script(ScriptType.INLINE, "painless", script.toString(), Collections.emptyMap()));
    }
}
 
Example #2
Source File: ReindexQueryParser.java    From elasticsearch-sql with MIT License 4 votes vote down vote up
@Override
public void parse(ElasticDslContext dslContext) {
    if (dslContext.getSqlContext().reindexOperation() != null) {
        dslContext.getParseResult().setSqlOperation(SqlOperation.REINDEX);
        ElasticsearchParser.ReindexOperationContext reindexOperationContext = dslContext.getSqlContext().reindexOperation();
        String destIndex = reindexOperationContext.tableRef(0).indexName.getText();
        String[] sourceIndices = new String[reindexOperationContext.tableRef().size() - 1];
        for (int i = 1; i <= sourceIndices.length; i++) {
            sourceIndices[i - 1] = reindexOperationContext.tableRef(i).indexName.getText();
        }
        ReindexRequest reindexRequest = new ReindexRequest();
        if (reindexOperationContext.fieldList().nameOperand().size() > 0) {
            buildScript(reindexOperationContext, reindexRequest);
        }
        if (reindexOperationContext.whereClause() != null) {
            BoolExpressionParser boolExpressionParser = new BoolExpressionParser();
            QueryBuilder queryBuilder = boolExpressionParser.parseBoolQueryExpr(reindexOperationContext.whereClause().expression());
            if (reindexOperationContext.host != null) {
                reindexRequest.setRemoteInfo(buildRemoteInfo(reindexOperationContext, queryBuilder.toString()));
            } else {
                reindexRequest.setSourceQuery(queryBuilder);
            }
        }
        if (reindexOperationContext.batchClause() != null) {
            reindexRequest.setSourceBatchSize(Integer.parseInt(reindexOperationContext.batchClause().size.getText()));
        }
        if (reindexOperationContext.limitClause() != null) {
            reindexRequest.setMaxDocs(Integer.parseInt(reindexOperationContext.limitClause().size.getText()));
        }
        reindexRequest.setDestIndex(destIndex);
        reindexRequest.setSourceIndices(sourceIndices);
        reindexRequest.setDestOpType("create");
        reindexRequest.setConflicts("proceed");
        reindexRequest.setShouldStoreResult(true);
        reindexRequest.setScroll(TimeValue.timeValueMinutes(10));
        ActionRequestValidationException validationException = reindexRequest.validate();
        if (validationException != null) {
            throw validationException;
        }
        dslContext.getParseResult().setReindexRequest(reindexRequest);
    }
}
 
Example #3
Source File: ElasticSqlParseResult.java    From elasticsearch-sql with MIT License 4 votes vote down vote up
public ReindexRequest getReindexRequest() {
    return reindexRequest;
}
 
Example #4
Source File: ElasticSqlParseResult.java    From elasticsearch-sql with MIT License 4 votes vote down vote up
public void setReindexRequest(ReindexRequest reindexRequest) {
    this.reindexRequest = reindexRequest;
}
 
Example #5
Source File: EsHighLevelRestTest2.java    From java-study with Apache License 2.0 4 votes vote down vote up
/**
	 * 索引复制
	 * 
	 * @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);
	}
 
Example #6
Source File: EsUtil.java    From java-study with Apache License 2.0 4 votes vote down vote up
/**
 * @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;
}