[英]How to aggregate ElasticSearch documents from same index but from multiple types?
[英]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 參考下面的 go 鏈接,並了解需要分步執行的操作。
基本上,通過 Java 發送的每個請求都將包含以下兩個部分。
至於您的示例,請注意,我假設traceId.keyword
是 type keyword
的字段,因此我使用的是TermBuilder
即Term Query 。 如果它是我在評論中提到的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.