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:
- Does ES no longer support tasks? Is it gradually being deprecated?
- If not, is there another way to submit new tasks?
- If it is indeed unavailable, is there another method to achieve similar functionality? Would using
deleteByQuery
inElasticsearchAsyncClient
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:
- Does ES no longer support tasks? Is it gradually being deprecated?
- If not, is there another way to submit new tasks?
- If it is indeed unavailable, is there another method to achieve similar functionality? Would using
deleteByQuery
inElasticsearchAsyncClient
be equivalent to submitting a task?
1 Answer
Reset to default 0I 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
版权声明:本文标题:How to submit task in ElasticSearch Java API Client? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736744951a1950721.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论