elasticsearch RestHighLevelClient 关于document的常用操作 ---------- 删除篇

es删除操作大致分为:基于id的单条删除、基于id的批量删除

基于id的单条删除:DeleteRequest

基于id的批量删除:BulkRequest中通过多个DeleteRequest来装载要删除的数据

 

单条删除:

    //es单条操作--删除文档(记录)
    public String deleteEsDocument(){

        //创建删除文档请求
        DeleteRequest request = new DeleteRequest();

        //设置属性:指定要删除的索引及id值
        DeleteRequest delete = request.index("user").id("1001");

        try {
            //像es服务发送请求
            DeleteResponse deleteResult = this.client.delete(delete, RequestOptions.DEFAULT);

            System.out.println("delde es index result is : " + delete.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "delete es index is complete";
    }

 

批量删除:

    //批量删除(单条件)public String bulkDeleteEsDocument(){

        //获取数据源
        List<String> list = setTestData();

        //创建批量操作请求
        BulkRequest request = new BulkRequest();

        int size = list.size();
        for(int i=0; i<size; i++){
            request.add(new DeleteRequest().index("user").id(list.get(i)));
        }

        BulkResponse delete = null;
        try {
            delete = this.client.bulk(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(delete.status());
        return "bulk delete es document complete";
    }

    //批量删除的数据源
    private static List<String> setTestData(){

        List<String> objects = new ArrayList<>();
        objects.add("sAMC7XgBrgVVHTXvmYL_");
        objects.add("Pi6t6ngBdcbzHcXaH_iM");

        return objects;
    }

 

上一篇:【编译原理·总复习】第三章||词法分析||有穷自动机||DFA转换NFA||DFA化简||例题+知识点


下一篇:线程池 threadpool