[英]Delete multiple documents from an index in ElasticSearch with value of one of items - 7.8 version
my doc in elsticsearch is like below我在 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"
}
}
]
I want to delete All docs in one index that "traceId": "ABC" with java code.我想用 java 代码删除一个索引中“traceId”:“ABC”的所有文档。 I use Elasticsearch 7.8 and my client is RestHighLevelClient.
我使用 Elasticsearch 7.8,我的客户端是 RestHighLevelClient。 please guide me.
请指导我。
The below sample code should help.下面的示例代码应该会有所帮助。 You can refer to the links below to go through the API and understand what needs to be done in steps.
您可以通过 API 参考下面的 go 链接,并了解需要分步执行的操作。
Basically every request sent via Java would have below two parts.基本上,通过 Java 发送的每个请求都将包含以下两个部分。
As for your example, note that I'm assuming that traceId.keyword
is the field of type keyword
and hence I'm using TermBuilder
ie Term Query .至于您的示例,请注意,我假设
traceId.keyword
是 type keyword
的字段,因此我使用的是TermBuilder
即Term Query 。 If it is of type text
I've mentioned Match Query
in the comment, you would have to use that.如果它是我在评论中提到的
Match Query
的text
类型,您将不得不使用它。
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.