Es批量删除DeleteByQueryRequestBuilder
在Elasticsearch中,使用DeleteByQueryRequestBuilder
可以根据指定的查询条件来批量删除文档。以下是一个使用Java High Level REST Client的例子,演示如何使用DeleteByQueryRequestBuilder
来批量删除满足特定查询条件的文档。
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.index.reindex.DeleteByQueryResponse;
import java.io.IOException;
public class DeleteByQueryExample {
public static void main(String[] args) throws IOException {
// 初始化Elasticsearch客户端
try (RestHighLevelClient client = new RestHighLevelClient(...)) {
// 创建DeleteByQueryRequestBuilder
DeleteByQueryRequest request = new DeleteByQueryRequest("index_name"); // 替换为你的索引名
// 设置查询条件
request.setQuery(QueryBuilders.matchQuery("field_name", "value")); // 替换为你的字段名和值
// 执行批量删除
DeleteByQueryRequestBuilder deleteRequestBuilder = new DeleteByQueryRequestBuilder(client, DeleteByQueryAction.INSTANCE, request);
DeleteByQueryResponse response = deleteRequestBuilder.get();
// 打印结果
long deleted = response.getDeleted();
System.out.println("Batch delete operation completed. " + deleted + " documents deleted.");
}
}
}
确保替换index_name
为你的目标索引名,以及根据需要替换查询条件。这段代码使用了DeleteByQueryRequestBuilder
来构建和执行批量删除操作,并打印了删除的文档数量。
评论已关闭