You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Elasticsearch 2.3.1 Java API:DeleteByQuery如何设置URL参数?

How to Set wait_for_completion and Other Params in Elasticsearch 2.3.1 Java API for DeleteByQuery

Hey there! I’ve worked with Elasticsearch 2.3.x quite a bit, so let’s walk through how to configure those parameters you mentioned for DeleteByQuery using the Java API.

In ES 2.3.1, the DeleteByQueryRequestBuilder class has dedicated methods for setting exactly the parameters you’re asking about—no need to hack around with raw JSON or extra headers. Here’s a complete, working example that incorporates both wait_for_completion and wait_for_active_shards:

import org.elasticsearch.action.deletebyquery.DeleteByQueryRequestBuilder;
import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilders;

// Assume you've already initialized your ES Client (via TransportClient or NodeClient)
Client esClient = ...;

// Build your DeleteByQuery request
DeleteByQueryRequestBuilder deleteRequest = esClient.prepareDeleteByQuery("your_target_index")
        // Replace this with your actual query (e.g., termQuery, rangeQuery)
        .setQuery(QueryBuilders.matchAllQuery())
        // Set wait_for_completion to false (returns task ID immediately for background execution)
        .setWaitForCompletion(false)
        // Set wait_for_active_shards: use an integer or "all" for all shards
        .setWaitForActiveShards(2); // Or .setWaitForActiveShards("all")

// Execute the request
DeleteByQueryResponse response = deleteRequest.get();

// If wait_for_completion=false, grab the task ID to monitor progress later
String backgroundTaskId = response.getTaskId();
System.out.println("Background delete task started with ID: " + backgroundTaskId);

Key Details:

  • setWaitForCompletion(boolean): Directly maps to the wait_for_completion curl parameter. Setting this to false is perfect for large delete operations, as it lets ES return immediately while processing the delete in the background. You can use the returned task ID to check status later via the Tasks API.
  • setWaitForActiveShards(): This method has two overloads—one that takes an integer (number of shards to wait for) and another that takes a string (like "all" to wait for every shard to be active). This ensures your delete operation doesn’t start until the required number of shards are available, reducing the risk of partial failures.

Just make sure your project includes the Elasticsearch 2.3.1 core dependency (since DeleteByQuery is part of the core module in this version) to access these classes.

内容的提问来源于stack exchange,提问作者thaveethu gce

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:44:48