org.elasticsearch.index.reindex.UpdateByQueryRequest Java Examples

The following examples show how to use org.elasticsearch.index.reindex.UpdateByQueryRequest. 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: UpdateQueryParser.java    From elasticsearch-sql with MIT License 6 votes vote down vote up
private void parseUpdateByQuery(ElasticDslContext dslContext) {
    dslContext.getParseResult().setSqlOperation(SqlOperation.UPDATE_BY_QUERY);
    ElasticsearchParser.UpdateOperationContext updateOperationContext = dslContext.getSqlContext().updateOperation();
    String indexName = updateOperationContext.tableRef().indexName.getText();
    UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest(indexName);
    int size = updateOperationContext.ID().size();
    StringBuilder builder = new StringBuilder();
    Map<String, Object> params = new HashMap<>(0);
    for (int i = 0; i < size; i++) {
        builder.append(UPDATE_PREFIX).append(updateOperationContext.ID(i).getText()).append('=');
        if (updateOperationContext.identity(i).identityList() != null) {
            builder.append("params.").append(PARAM_PREFIX).append(params.size());
            params.put(PARAM_PREFIX + params.size(), parseIdentityList(updateOperationContext.identity(i).identityList().identity()));
        } else if (updateOperationContext.identity(i).STRING() != null) {
            builder.append('\'').append(StringManager.removeStringSymbol(updateOperationContext.identity(i).STRING().getText())).append('\'');
        } else {
            builder.append(updateOperationContext.identity(i).number.getText());
        }
        builder.append(';');
    }
    updateByQueryRequest.setScript(new Script(ScriptType.INLINE, "painless", builder.toString(), params));
    if (updateOperationContext.routingClause() != null) {
        updateByQueryRequest.setRouting(StringManager.removeStringSymbol(updateOperationContext.routingClause().STRING(0).getText()));
    }
    if (updateOperationContext.whereClause() != null) {
        BoolExpressionParser boolExpressionParser = new BoolExpressionParser();
        updateByQueryRequest.setQuery(boolExpressionParser.parseBoolQueryExpr(updateOperationContext.whereClause().expression()));
    } else {
        updateByQueryRequest.setQuery(QueryBuilders.matchAllQuery());
    }
    if (updateOperationContext.batchClause() != null) {
        updateByQueryRequest.setBatchSize(Integer.parseInt(updateOperationContext.batchClause().size.getText()));
    }
    if (updateOperationContext.limitClause() != null) {
        updateByQueryRequest.setMaxDocs(Integer.parseInt(updateOperationContext.limitClause().size.getText()));
    }
    dslContext.getParseResult().setUpdateByQueryRequest(updateByQueryRequest);
}
 
Example #2
Source File: EsUtil.java    From java-study with Apache License 2.0 5 votes vote down vote up
/**
 * @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;
}
 
Example #3
Source File: ElasticsearchIndexer.java    From datashare with GNU Affero General Public License v3.0 5 votes vote down vote up
private boolean groupTagUntag(Project prj, List<String> documentIds, Script untagScript) throws IOException {
    UpdateByQueryRequest updateByQuery = new UpdateByQueryRequest(prj.getId());
    updateByQuery.setQuery(termsQuery("_id", documentIds.toArray(new String[0])));
    updateByQuery.setConflicts("proceed");
    updateByQuery.setScript(untagScript);
    updateByQuery.setRefresh(esCfg.refreshPolicy.getValue().equals("true"));
    BulkByScrollResponse updateResponse = client.updateByQuery(updateByQuery, RequestOptions.DEFAULT);
    return updateResponse.getBulkFailures().size() == 0 && updateResponse.getUpdated() > 0 ;
}
 
Example #4
Source File: ElasticSqlParseResult.java    From elasticsearch-sql with MIT License 4 votes vote down vote up
public UpdateByQueryRequest getUpdateByQueryRequest() {
    return updateByQueryRequest;
}
 
Example #5
Source File: ElasticSqlParseResult.java    From elasticsearch-sql with MIT License 4 votes vote down vote up
public void setUpdateByQueryRequest(UpdateByQueryRequest updateByQueryRequest) {
    this.updateByQueryRequest = updateByQueryRequest;
}
 
Example #6
Source File: EsHighLevelRestTest1.java    From java-study with Apache License 2.0 3 votes vote down vote up
/**
	 * 根据查询条件更新
	 *
	 * @throws IOException
	 */
	private static void updateByQuery() throws IOException {
		String type = "_doc";
		String index = "test1";
		//
		UpdateByQueryRequest request = new UpdateByQueryRequest(index,type);
		// 设置查询条件
		request.setQuery(new TermQueryBuilder("user", "pancm"));
		BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

		// 设置复制文档的数量
		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);

	}