Java Code Examples for org.elasticsearch.index.reindex.DeleteByQueryRequest#setQuery()

The following examples show how to use org.elasticsearch.index.reindex.DeleteByQueryRequest#setQuery() . 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: DeleteQueryParser.java    From elasticsearch-sql with MIT License 6 votes vote down vote up
private void parseDeleteByQuery(ElasticDslContext dslContext) {
    dslContext.getParseResult().setSqlOperation(SqlOperation.DELETE_BY_QUERY);
    ElasticsearchParser.DeleteOperationContext deleteOperationContext = dslContext.getSqlContext().deleteOperation();
    List<String> indices = new ArrayList<>(0);
    for (ElasticsearchParser.TableRefContext tableRefContext : deleteOperationContext.tableRef()) {
        indices.add(tableRefContext.indexName.getText());
    }
    DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(indices.toArray(new String[0]));
    BoolExpressionParser boolExpressionParser = new BoolExpressionParser();
    if (deleteOperationContext.whereClause() != null) {
        deleteByQueryRequest.setQuery(boolExpressionParser.parseBoolQueryExpr(deleteOperationContext.whereClause().expression()));
    } else {
        deleteByQueryRequest.setQuery(QueryBuilders.matchAllQuery());
    }
    if (deleteOperationContext.routingClause() != null) {
        deleteByQueryRequest.setRouting(StringManager.removeStringSymbol(deleteOperationContext.routingClause().STRING(0).getText()));
    }
    if (deleteOperationContext.batchClause() != null) {
        deleteByQueryRequest.setBatchSize(Integer.parseInt(deleteOperationContext.batchClause().size.getText()));
    }
    if (deleteOperationContext.limitClause() != null) {
        deleteByQueryRequest.setMaxDocs(Integer.parseInt(deleteOperationContext.limitClause().size.getText()));
    }
    dslContext.getParseResult().setDeleteByQueryRequest(deleteByQueryRequest);
}
 
Example 2
Source File: EsUtil.java    From java-study with Apache License 2.0 6 votes vote down vote up
/**
 * @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;
}
 
Example 3
Source File: EsUtil.java    From bookmark with MIT License 6 votes vote down vote up
/**
 * Description: delete by query
 *
 * @param index   index
 * @param builder builder
 * @author fanxb
 * @date 2019/7/26 15:16
 */
public void deleteByQuery(String index, QueryBuilder builder) {
    if (!status) {
        return;
    }
    DeleteByQueryRequest request = new DeleteByQueryRequest(index);
    request.setQuery(builder);
    //设置批量操作数量,最大为10000
    request.setBatchSize(10000);
    request.setConflicts("proceed");
    try {
        client.deleteByQuery(request, RequestOptions.DEFAULT);
    } catch (Exception e) {
        throw new EsException(e);
    }
}
 
Example 4
Source File: EsHighLevelRestTest1.java    From java-study with Apache License 2.0 5 votes vote down vote up
/**
	 * 根据查询条件删除
	 *
	 * @throws IOException
	 */
	private static void deleteByQuery() throws IOException {
		String type = "_doc";
		String index = "test1";
		DeleteByQueryRequest request = new DeleteByQueryRequest(index,type);
		// 设置查询条件
		request.setQuery(QueryBuilders.termQuery("uid",1234));
		// 同步执行
		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);

	}
 
Example 5
Source File: EsUtil.java    From demo-project with MIT License 5 votes vote down vote up
/**
 * Description: delete by query
 *
 * @param index   index
 * @param builder builder
 * @author fanxb
 * @date 2019/7/26 15:16
 */
public void deleteByQuery(String index, QueryBuilder builder) {
    DeleteByQueryRequest request = new DeleteByQueryRequest(index);
    request.setQuery(builder);
    //设置批量操作数量,最大为10000
    request.setBatchSize(10000);
    request.setConflicts("proceed");
    try {
        client.deleteByQuery(request, RequestOptions.DEFAULT);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 6
Source File: ElasticRequestUtils.java    From vind with Apache License 2.0 5 votes vote down vote up
public static DeleteByQueryRequest getDeleteByQueryRequest(String index, QueryBuilder query) {

        final DeleteByQueryRequest request =
                new DeleteByQueryRequest(index);
        request.setQuery(query);
        request.setRefresh(true);
        return request;
    }
 
Example 7
Source File: ElasticSearch7Client.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public int delete(String indexName, String timeBucketColumnName, long endTimeBucket) throws IOException {
    indexName = formatIndexName(indexName);

    DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(indexName);
    deleteByQueryRequest.setAbortOnVersionConflict(false);
    deleteByQueryRequest.setQuery(QueryBuilders.rangeQuery(timeBucketColumnName).lte(endTimeBucket));
    BulkByScrollResponse bulkByScrollResponse = client.deleteByQuery(deleteByQueryRequest, RequestOptions.DEFAULT);
    log.debug(
        "delete indexName: {}, by query request: {}, response: {}", indexName, deleteByQueryRequest,
        bulkByScrollResponse
    );
    return HttpStatus.SC_OK;
}
 
Example 8
Source File: EsHighLevelRestTest2.java    From java-study with Apache License 2.0 4 votes vote down vote up
/**
	 * 根据查询条件删除
	 * @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);

	}