在 Elasticsearch 6 中,您可以使用 Java API 来删除文档。以下是删除文档的基本语法和步骤:

  1. 创建 DeleteRequest 对象: 指定要删除文档的索引和文档 ID。
DeleteRequest request = new DeleteRequest('index_name', 'document_id');

其中,'index_name' 是要删除文档的索引名称,'document_id' 是要删除文档的唯一标识。

  1. 设置可选参数(例如超时时间):
request.timeout(TimeValue.timeValueMinutes(2));

这里将超时时间设置为 2 分钟。

  1. 执行删除请求: 使用 Elasticsearch 的 Java 高级客户端执行删除请求。
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);

其中,'client' 是 Elasticsearch 客户端,可以使用 RestHighLevelClient 或 TransportClient 实例化。

  1. 获取删除操作的结果: 通过 DeleteResponse 对象获取删除操作的结果,包括索引名称、文档 ID 和版本号。
String index = response.getIndex();
String id = response.getId();
long version = response.getVersion();

完整示例代码:

DeleteRequest request = new DeleteRequest('index_name', 'document_id');
request.timeout(TimeValue.timeValueMinutes(2));

DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
String index = response.getIndex();
String id = response.getId();
long version = response.getVersion();

注意: 要使用 Java API 执行删除操作,您需要导入相应的 Elasticsearch 客户端库。


原文地址: https://www.cveoy.top/t/topic/qqkk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录