admin管理员组

文章数量:1129454

Now, RestHighLevelClient is deprecated. So i want rewrite code with ElasticSearch Java Api Client in my spring project.

Most of the functions are being modified properly, but I have not found an appropriate way to modify this function.

Below is my existing code.

//    import org.elasticsearch.index.reindex.DeleteByQueryRequest;
    private String deleteLarge(String indexName, Long categoryNumber, Optional<Long> age) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                .filter(QueryBuilders.matchQuery("category", categoryNumber));
        age.ifPresent(a -> boolQueryBuilder
                .filter(QueryBuilders.matchQuery("age", a)));

        DeleteByQueryRequest request = new DeleteByQueryRequest(indexName)
                .setQuery(boolQueryBuilder)
                .setSlices(10);

        try {
            TaskSubmissionResponse taskSubmissionResponse = restHighLevelClient.submitDeleteByQueryTask(request, RequestOptions.DEFAULT);
            logger.info("DeleteByQueryTask task id: {}", taskSubmissionResponse.getTask());
            return taskSubmissionResponse.getTask();
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return null;
    }

The purpose of this code is to delete a very large volume of documents (ranging from tens of thousands to millions). Since it was difficult to estimate the execution time, I submitted a task to be performed internally by ES.

However, it seems that the ElasticSearch Java API Client does not have such a feature. In the official documentation, there is a Class like ElasticsearchTasksClient, which provides the ability to get info or cancel task, but not to submit new tasks.

Here are my questions:

  1. Does ES no longer support tasks? Is it gradually being deprecated?
  2. If not, is there another way to submit new tasks?
  3. If it is indeed unavailable, is there another method to achieve similar functionality? Would using deleteByQuery in ElasticsearchAsyncClient be equivalent to submitting a task?

Now, RestHighLevelClient is deprecated. So i want rewrite code with ElasticSearch Java Api Client in my spring project.

Most of the functions are being modified properly, but I have not found an appropriate way to modify this function.

Below is my existing code.

//    import org.elasticsearch.index.reindex.DeleteByQueryRequest;
    private String deleteLarge(String indexName, Long categoryNumber, Optional<Long> age) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                .filter(QueryBuilders.matchQuery("category", categoryNumber));
        age.ifPresent(a -> boolQueryBuilder
                .filter(QueryBuilders.matchQuery("age", a)));

        DeleteByQueryRequest request = new DeleteByQueryRequest(indexName)
                .setQuery(boolQueryBuilder)
                .setSlices(10);

        try {
            TaskSubmissionResponse taskSubmissionResponse = restHighLevelClient.submitDeleteByQueryTask(request, RequestOptions.DEFAULT);
            logger.info("DeleteByQueryTask task id: {}", taskSubmissionResponse.getTask());
            return taskSubmissionResponse.getTask();
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return null;
    }

The purpose of this code is to delete a very large volume of documents (ranging from tens of thousands to millions). Since it was difficult to estimate the execution time, I submitted a task to be performed internally by ES.

However, it seems that the ElasticSearch Java API Client does not have such a feature. In the official documentation, there is a Class like ElasticsearchTasksClient, which provides the ability to get info or cancel task, but not to submit new tasks.

Here are my questions:

  1. Does ES no longer support tasks? Is it gradually being deprecated?
  2. If not, is there another way to submit new tasks?
  3. If it is indeed unavailable, is there another method to achieve similar functionality? Would using deleteByQuery in ElasticsearchAsyncClient be equivalent to submitting a task?
Share Improve this question asked Jan 8 at 9:24 RedwingsRedwings 5602 gold badges5 silver badges19 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I was a bit confused, but I solved the problem. When using deleteByQuery as shown below, the DeleteByQueryResponse returns with a task ID. Currently, it's difficult to test with really large-scale data, so further verification is needed, but it seems to behave similarly to the original code.

Below is an example of the modified version of the code.


    import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
    import co.elastic.clients.elasticsearch.core.DeleteByQueryRequest;
    import co.elastic.clients.elasticsearch.core.DeleteByQueryResponse;

    private String deleteLarge(String indexName, Long categoryNumber, Optional<Long> age) {
        BoolQuery.Builder queryBuilder = new BoolQuery.Builder().filter(
                fq -> fq.match(mq -> mq.field("categoryNumber").query(categoryNumber)));
        age.ifPresent(a -> queryBuilder.filter(
                fq -> fq.match(mq -> mq.field("age").query(a))));

        DeleteByQueryRequest newRequest = new DeleteByQueryRequest.Builder()
                .index(indexName)
                .query(queryBuilder.build()._toQuery())
                .waitForCompletion(false)
                .build();

        try {
            DeleteByQueryResponse deleteByQueryResponse = esClient.deleteByQuery(newRequest);
            logger.info("DeleteByQuery task id: {}", deleteByQueryResponse.task());
            return deleteByQueryResponse.task();

        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return null;
    }

Where did the word "task" disappear to? It took me quite a while to find it, thanks to that.

本文标签: How to submit task in ElasticSearch Java API ClientStack Overflow