从 ElasticSearch 中的一个索引中删除具有其中一项值的多个文档 - 7.8 版本
Delete multiple documents from an index in ElasticSearch with value of one of items - 7.8 version
我在 elsticsearch 中的文档如下所示
"hits" : [
{
"_index" : "MyIndex",
"_type" : "_doc",
"_id" : "Lxh7THMBycWRIeJFdwbZ",
"_score" : 1.0,
"_source" : {
"@timestamp" : "2020-07-14T13:10:26.087+0430",
"message" : "elk: customer inserted: id=5",
"traceId" : "ABC",
"severity" : "INFO",
"thread" : "http-nio-8095-exec-2"
}
}
]
我想删除带有 java 代码的“traceId”:“ABC”索引中的所有文档。
我使用 Elasticsearch 7.8,我的客户端是 RestHighLevelClient。
请指导我。
下面的示例代码应该有所帮助。您可以参考下面的链接来浏览 API 并了解需要按步骤完成的操作。
基本上每个通过 Java 发送的请求都会包含以下两个部分。
- 发送到 elasticsearch 的每个请求都类似于 HttpClient,即 High Level Rest Client。
- API 具体查询,在我们的例子中是 Delete by Query
关于您的示例,请注意我假设 traceId.keyword
是 keyword
类型的字段,因此我使用 TermBuilder
即 Term Query。如果它是 text
我在评论中提到的 Match Query
类型,则您必须使用它。
示例代码
import java.io.IOException;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
public class DeleteBasedOnQuery {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
deleteQueryUsingMatch(client);
client.close();
}
private static void deleteQueryUsingMatch(RestHighLevelClient client) throws IOException {
//Mention index name
DeleteByQueryRequest request = new DeleteByQueryRequest("my_delete_index");
request.setConflicts("proceed");
request.setBatchSize(100);
request.setRefresh(true);
//Term Query to Delete the document. Note the field name and value in below code
request.setQuery(new TermQueryBuilder("traceId.keyword", "ABC"));
//Match Query to Delete the Document
//request.setQuery(QueryBuilders.matchQuery("traceID", "abc"));
//Execute the request to delete based on above details
BulkByScrollResponse bulkResponse = client.deleteByQuery(request, RequestOptions.DEFAULT);
//By this time your delete query got executed and you have the response with you.
long totalDocs = bulkResponse.getTotal();
long deletedDocs = bulkResponse.getDeleted();
//Print the response details
System.out.println("Total Docs Processed :: " + totalDocs);
System.out.println("Total Docs Deleted :: " + deletedDocs);
}
}
我在 elsticsearch 中的文档如下所示
"hits" : [
{
"_index" : "MyIndex",
"_type" : "_doc",
"_id" : "Lxh7THMBycWRIeJFdwbZ",
"_score" : 1.0,
"_source" : {
"@timestamp" : "2020-07-14T13:10:26.087+0430",
"message" : "elk: customer inserted: id=5",
"traceId" : "ABC",
"severity" : "INFO",
"thread" : "http-nio-8095-exec-2"
}
}
]
我想删除带有 java 代码的“traceId”:“ABC”索引中的所有文档。 我使用 Elasticsearch 7.8,我的客户端是 RestHighLevelClient。 请指导我。
下面的示例代码应该有所帮助。您可以参考下面的链接来浏览 API 并了解需要按步骤完成的操作。
基本上每个通过 Java 发送的请求都会包含以下两个部分。
- 发送到 elasticsearch 的每个请求都类似于 HttpClient,即 High Level Rest Client。
- API 具体查询,在我们的例子中是 Delete by Query
关于您的示例,请注意我假设 traceId.keyword
是 keyword
类型的字段,因此我使用 TermBuilder
即 Term Query。如果它是 text
我在评论中提到的 Match Query
类型,则您必须使用它。
示例代码
import java.io.IOException;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
public class DeleteBasedOnQuery {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
deleteQueryUsingMatch(client);
client.close();
}
private static void deleteQueryUsingMatch(RestHighLevelClient client) throws IOException {
//Mention index name
DeleteByQueryRequest request = new DeleteByQueryRequest("my_delete_index");
request.setConflicts("proceed");
request.setBatchSize(100);
request.setRefresh(true);
//Term Query to Delete the document. Note the field name and value in below code
request.setQuery(new TermQueryBuilder("traceId.keyword", "ABC"));
//Match Query to Delete the Document
//request.setQuery(QueryBuilders.matchQuery("traceID", "abc"));
//Execute the request to delete based on above details
BulkByScrollResponse bulkResponse = client.deleteByQuery(request, RequestOptions.DEFAULT);
//By this time your delete query got executed and you have the response with you.
long totalDocs = bulkResponse.getTotal();
long deletedDocs = bulkResponse.getDeleted();
//Print the response details
System.out.println("Total Docs Processed :: " + totalDocs);
System.out.println("Total Docs Deleted :: " + deletedDocs);
}
}