ElasticSearch 2.3.1 DeleteByQuery Java API如何添加查询字符串参数?
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

