简体   繁体   English

从 ElasticSearch 中的索引中删除多个文档,其值为项目之一 - 7.8 版

[英]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的字段,因此我使用的是TermBuilderTerm Query If it is of type text I've mentioned Match Query in the comment, you would have to use that.如果它是我在评论中提到的Match Querytext类型,您将不得不使用它。

Sample Code示例代码

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.

相关问题 如何聚合来自相同索引但来自多种类型的ElasticSearch文档? - How to aggregate ElasticSearch documents from same index but from multiple types? 使用 RestClient 从 Scala 中的 elasticsearch 中删除文档 - Delete documents from elasticsearch in Scala using RestClient 如何从其他文档(ElasticSearch)中按字段值获取嵌套文档? - How to get nested documents by field value from other documents (ElasticSearch)? 在 ElasticSearch 中删除整个索引 - Delete entire index in ElasticSearch 删除arrayList中的项目:尝试从不存在的Index中删除? - Deleting items in arrayList: trying to delete from Index that is not there? 如何从Lucene索引中删除旧文档(按Date参数过滤) - How to delete old documents from lucene index (filter by Date parameter) ElasticSearch 创建的文档无法在索引中查看 - ElasticSearch created documents cannot be viewed in the index 如何在Elasticsearch 5.6.3中删除索引 - How to delete index in elasticsearch 5.6.3 java - 如何在java中基于一个相同的索引值同时从两个Arraylists中删除元素 - How to simulteniously delete elements from two Arraylists based one same index value in java Lucene:从索引中删除,基于多个字段 - Lucene: delete from index, based on multiple fields
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM