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

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

基于id的单条删除:DeleteRequest

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

自定义条件删除:

  通过QueryBuilders中的termQuery(等值匹配)、rangeQuery(范围匹配)、wildcardQuery(模糊匹配)指定搜索条件

  通过QueryBuilders中的boolQuery中的should、must来设置and、or逻辑

  通过DeleteByQueryRequest来构建删除请求,setQuery来装载条件,indices来指定索引

  通过deleteByQuery来发起删除请求(es也是先查询后删除)

 

单条删除:

    //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;
    }

 

自定义条件删除:

    //自定义删除条件
    @RequestMapping("/customDeleteCondition")
    public String customDeleteCondition(){

        //设置删除条件名字 == 张三
        TermQueryBuilder userNameWhere = QueryBuilders.termQuery("userName.keyword", "张三");

        //设置删除条件体重》=10 《=100
        RangeQueryBuilder weightRangeWhere = QueryBuilders.rangeQuery("weight");
        weightRangeWhere.gte(10);
        weightRangeWhere.lte(100);

        //设置删除条件模糊匹配名字中带t的记录
        WildcardQueryBuilder userNameWildCardWhere = QueryBuilders.wildcardQuery("userName.keyword",  "*r*");

        //装载条件并设置must == and  should == or  and、or根据业务需要自行调整
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        queryBuilder.should(userNameWhere);
        queryBuilder.should(weightRangeWhere);
        queryBuilder.should(userNameWildCardWhere);

        DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest();//构建删除请求
        deleteByQueryRequest.setQuery(queryBuilder);//装载删除条件
        deleteByQueryRequest.indices("user");//指定删除索引

        try {
            this.client.deleteByQuery(deleteByQueryRequest , RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "complete";
    }

 

特殊情况:

  针对map数据类型的自定义条件删除

  map数据类型在做删除操作时比较特别须写全标识,示例中scoure为字段归属标识,“数学”为字段名,keyword为分词,99为值

  个人猜想此处须单独写出scoure字段归属标识有两个原因:1.此处删除操作的条件是完全脱离实体类进行设置的。2.考虑到字段同名问题须特别指出要修改哪个“数学”

    public void mapTestDelete(){

        //map数据类型在做删除操作时比较特别须写全标识,示例中scoure为字段标识,“数学”为字段名,keyword为分词,99为值
        //设置删除条件并装载数据
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        queryBuilder.must(QueryBuilders.termQuery("scoure.数学.keyword", "99"));

        DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest();//构建删除请求
        deleteByQueryRequest.setQuery(queryBuilder);//装载删除条件
        deleteByQueryRequest.indices("map_test");//指定删除索引

        try {
            this.client.deleteByQuery(deleteByQueryRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

posted @ 2021-04-26 18:21  茴香饺子、  阅读(2036)  评论(0编辑  收藏  举报