具有弹性 java api 客户端 8.2.0 的 DeleteRequest 示例

DeleteRequest example with elastic java api client 8.2.0

我需要关于 ES 8.2.0 Java Api 客户端的 DeleteRequest 示例,我们没有类型。我们只有索引和文档。我正在寻找代码参考,我想通过传递索引名称和文档 ID 来删除一个特定文档。

你可以试试这个

语法:

DeleteRequest request = new DeleteRequest("your-index-name","doc-id"); 

示例:

DeleteRequest deleteRequest = new DeleteRequest("employeeindex","002");
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
         System.out.println("response id: "+deleteResponse.getId());

了解更多信息

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-delete.html

您可以使用以下代码从索引中删除文档。您需要提供 index_namedoc_id 才能删除文档。

RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
ElasticsearchClient esClient = new ElasticsearchClient(transport);

DeleteRequest request = DeleteRequest.of(d -> d.index("index_name").id("doc_id"));
DeleteResponse response = esClient.delete(request);