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

ElasticSearch 2.3.1 DeleteByQuery Java API如何添加查询字符串参数?

How to Add Size, Routing & Other Query Parameters to DeleteByQuery Requests via Java API (Elasticsearch 2.3.1)

My Current Setup & Code

I'm using Elasticsearch 2.3.1 with the delete-by-query plugin installed to delete documents via the Java API. Here's my current code:

DeleteByQueryRequestBuilder deleteRequest = new DeleteByQueryRequestBuilder(esClient, DeleteByQueryAction.INSTANCE)
    .setIndices(index)
    .setTypes(type);
deleteRequest.setQuery(boolQuery);
DeleteByQueryResponse resp = deleteRequest.get();

My Question

How can I add query string parameters like size and routing to this DeleteByQuery request?


Answer

Since you're working with Elasticsearch 2.3.1's delete-by-query plugin, the DeleteByQueryRequestBuilder has built-in methods to handle these parameters, plus a flexible way to add custom ones. Here's how to implement each:

1. Add the Routing Parameter

Use the setRouting() method to target specific shards via routing keys. It supports both single and multiple values:

// Single routing key
deleteRequest.setRouting("user_123");

// Multiple routing keys
deleteRequest.setRouting("user_123", "user_456", "user_789");

2. Add the Size Parameter

In the context of DeleteByQuery, size controls how many documents are fetched per scroll batch (the plugin uses scroll under the hood to process deletions in chunks). Set it with setSize() to avoid overwhelming your cluster:

// Process 1000 documents per scroll batch
deleteRequest.setSize(1000);

3. Add Custom Query Parameters

For other parameters like timeout or refresh, use the setParam() method to manually attach them to the request:

// Set a 5-minute timeout for the deletion request
deleteRequest.setParam("timeout", "5m");
// Trigger an index refresh immediately after deletion
deleteRequest.setParam("refresh", "true");

Updated Full Code

Putting it all together, your code will look like this:

DeleteByQueryRequestBuilder deleteRequest = new DeleteByQueryRequestBuilder(esClient, DeleteByQueryAction.INSTANCE)
    .setIndices(index)
    .setTypes(type)
    .setQuery(boolQuery)
    .setRouting("user_123")
    .setSize(1000)
    .setParam("timeout", "5m");

DeleteByQueryResponse resp = deleteRequest.get();

A quick reminder: These parameters behave exactly like their REST API counterparts in Elasticsearch 2.3.1, so their behavior aligns with what you'd expect from direct HTTP requests.

内容的提问来源于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 10:13:42