当前位置: 首页 > article >正文

Springboot 整合 Elasticsearch(五):使用RestHighLevelClient操作ES ②

📁 前情提要:

Springboot 整合 Elasticsearch(三):使用RestHighLevelClient操作ES ①

目录

 一、Springboot 整合 Elasticsearch

1、RestHighLevelClient API介绍

1.1、全查询 & 分页 & 排序

1.2、单条件查询

1.2.1、termQuery

1.2.2、matchQuery

1.2.3、短语检索

1.3、组合查询

1.4、范围查询

1.5、模糊查询

1.6、分组查询


一、Springboot 整合 Elasticsearch

1、RestHighLevelClient API介绍

目前的测试数据有:

1.1、全查询 & 分页 & 排序

    @Test
    public void queryAllDoc() {
        try {
            String indexName = "forest";
            SearchRequest request = new SearchRequest();
            request.indices(indexName);

            // 构建查询的请求体
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            sourceBuilder.query(QueryBuilders.matchAllQuery());
            // 设置分页
            sourceBuilder.from(1);
            sourceBuilder.size(3);
            // 设置排序
            sourceBuilder.sort("price",  SortOrder.DESC);

            request.source(sourceBuilder);

            SearchResponse search = esConfig.restHighLevelClient().search(request, RequestOptions.DEFAULT);
            if (200 != search.status().getStatus()) {
                log.error("查询失败");
            }
            System.out.println("--------查询结果-----------");
            SearchHits hits = search.getHits();
            for (SearchHit hit : hits) {
                System.out.println(hit.getSourceAsString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

1.2、单条件查询

1.2.1、termQuery

term属于精确匹配,而且只能查单个词(一个汉字或者一个英文单词),不会对用户输入的内容进行分词;

    @Test
    public void queryDocByKey() {
        try {
            String indexName = "forest";
            SearchRequest request = new SearchRequest();
            request.indices(indexName);
            // 构建查询的请求体
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            sourceBuilder.query(QueryBuilders.termQuery("price", "300"));
            request.source(sourceBuilder);

            SearchResponse search = esConfig.restHighLevelClient().search(request, RequestOptions.DEFAULT);
            if (200 != search.status().getStatus()) {
                log.error("查询失败");
            }
            System.out.println("--------查询结果-----------");
            SearchHits hits = search.getHits();
            for (SearchHit hit : hits) {
                System.out.println(hit.getSourceAsString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

1.2.2、matchQuery

match进行搜索的时候,会先进行分词拆分,拆完后,再来匹配;例如输入“枫树”,match会拆分成“枫”,“树”两个字,只要包含其中一个字的都会被查出来。

    @Test
    public void queryDocByKey() {
        try {
            String indexName = "forest";
            SearchRequest request = new SearchRequest();
            request.indices(indexName);
            // 构建查询的请求体
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            sourceBuilder.query(QueryBuilders.matchQuery("title", "枫树"));
            request.source(sourceBuilder);

            SearchResponse search = esConfig.restHighLevelClient().search(request, RequestOptions.DEFAULT);
            if (200 != search.status().getStatus()) {
                log.error("查询失败");
            }
            System.out.println("--------查询结果-----------");
            SearchHits hits = search.getHits();
            for (SearchHit hit : hits) {
                System.out.println(hit.getSourceAsString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

1.2.3、短语检索

要求所有的分词必须同时出现在文档中,同时位置必须紧邻一致

    @Test
    public void queryDocByPhraseKey() {
        try {
            String indexName = "forest";
            SearchRequest request = new SearchRequest();
            request.indices(indexName);
            // 构建查询的请求体
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            sourceBuilder.query(QueryBuilders.matchPhraseQuery("title", "枫树"));
            request.source(sourceBuilder);

            SearchResponse search = esConfig.restHighLevelClient().search(request, RequestOptions.DEFAULT);
            if (200 != search.status().getStatus()) {
                log.error("查询失败");
            }
            System.out.println("--------查询结果-----------");
            SearchHits hits = search.getHits();
            for (SearchHit hit : hits) {
                System.out.println(hit.getSourceAsString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

1.3、组合查询

  • must:返回的文档必须满足must子句的条件,并且参与计算分值;
  • mustNot:返回的文档必须不满足定义的条件;
  • should:在一个bool查询中,如果没有must或者filter,有一个或者多个should子句,那么只要满足一个就可以返回;
  • filter:返回的文档必须满足filter子句的条件,但是不会像must一样,参与计算分值;
    @Test
    public void queryByCombinationKey() {
        try {
            // 声明搜索条件对象
            String indexName = "forest";
            SearchRequest request = new SearchRequest();
            request.indices(indexName);
            // 构建查询的请求体
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

            boolQueryBuilder.must(QueryBuilders.matchQuery("price", 1000));
            boolQueryBuilder.should(QueryBuilders.matchQuery("id", 3));
            sourceBuilder.query(boolQueryBuilder);
            request.source(sourceBuilder);
            SearchResponse search = esConfig.restHighLevelClient().search(request, RequestOptions.DEFAULT);

            if (200 != search.status().getStatus()) {
                log.error("查询失败");
            }
            System.out.println("--------查询结果-----------");
            SearchHits hits = search.getHits();
            for (SearchHit hit : hits) {
                System.out.println(hit.getSourceAsString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

[注]当使用should查询时,如果包含了must或者filter查询,那么should的查询语句就不是或者的意思了,而是有或者没有都行的含义。

1.4、范围查询

  • 闭区间查询:QueryBuilders.rangeQuery("${fieldName}").from(${fieldValue1}).to(${fieldValue2});
  • 开区间查询:QueryBuilders.rangeQuery("${fieldName}").from(${fieldValue1}, false).to(${fieldValue2}, false);
  • 大于:QueryBuilders.rangeQuery("${fieldName}").gt(${fieldValue});
  • 大于等于:QueryBuilders.rangeQuery("${fieldName}").gte(${fieldValue});
  • 小于:QueryBuilders.rangeQuery("${fieldName}").lt(${fieldValue});
  • 小于等于:QueryBuilders.rangeQuery("${fieldName}").lte(${fieldValue});
 @Test
    public void queryByCombinationKey() {
        try {
            // 声明搜索条件对象
            String indexName = "forest";
            SearchRequest request = new SearchRequest();
            request.indices(indexName);
            // 构建查询的请求体
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

            boolQueryBuilder.must(QueryBuilders.rangeQuery("price").from(300).to(500));
            boolQueryBuilder.must(QueryBuilders.rangeQuery("inventory").from(500).to(1000));

            sourceBuilder.query(boolQueryBuilder);
            request.source(sourceBuilder);
            SearchResponse search = esConfig.restHighLevelClient().search(request, RequestOptions.DEFAULT);

            if (200 != search.status().getStatus()) {
                log.error("查询失败");
            }
            System.out.println("--------查询结果-----------");
            SearchHits hits = search.getHits();
            for (SearchHit hit : hits) {
                System.out.println(hit.getSourceAsString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

1.5、模糊查询

    @Test
    public void queryFuzzyDocByKey() {
        try {
            String indexName = "forest";
            SearchRequest request = new SearchRequest();
            request.indices(indexName);
            // 构建查询的请求体
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            sourceBuilder.query(QueryBuilders.fuzzyQuery("title", "松").fuzziness(Fuzziness.AUTO));
            request.source(sourceBuilder);

            SearchResponse search = esConfig.restHighLevelClient().search(request, RequestOptions.DEFAULT);
            if (200 != search.status().getStatus()) {
                log.error("查询失败");
            }
            System.out.println("--------查询结果-----------");
            SearchHits hits = search.getHits();
            for (SearchHit hit : hits) {
                System.out.println(hit.getSourceAsString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

1.6、分组查询

按价格分组后求每组的条数

    @Test
    public void queryGroupByKey() {
        try {
            // 声明搜索条件对象
            String indexName = "forest";
            SearchRequest request = new SearchRequest();
            request.indices(indexName);
            // 构建查询的请求体
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

            sourceBuilder.aggregation(AggregationBuilders.terms("price_groupBy").field("price"));

            request.source(sourceBuilder);
            SearchResponse search = esConfig.restHighLevelClient().search(request, RequestOptions.DEFAULT);

            if (200 != search.status().getStatus()) {
                log.error("查询失败");
            }
            List<KeyAndCount> list = new ArrayList<>();
            System.out.println("--------查询结果-----------");
            Terms terms = search.getAggregations().get("price_groupBy");
            List<? extends Terms.Bucket> buckets = terms.getBuckets();
            for (Terms.Bucket bucket : buckets) {
                KeyAndCount keyAndCount = new KeyAndCount();
                keyAndCount.setKey(bucket.getKey().toString());
                keyAndCount.setCount(bucket.getDocCount());
                list.add(keyAndCount);
                System.out.println(keyAndCount);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



http://www.kler.cn/a/271964.html

相关文章:

  • 【Elasticsearch】腾讯云安装Elasticsearch
  • 基于JAVA的微信点餐小程序设计与实现(LW+源码+讲解)
  • Flutter:搜索页,搜索bar封装
  • 在elasticsearch中,document数据的写入流程如何?
  • Word常见问题:嵌入图片无法显示完整
  • Plotly 函数图像绘制
  • ClickHouse中的设置的分类
  • 【LeetCode热题100】24. 两两交换链表中的节点(链表)
  • 树与二叉树(数据结构)
  • 前端学习之css伪元素选择器
  • sqlplus设置提示符
  • 【CenterFusion】模型的创建、导入、保存CenterFusion/src/lib/model/model.py
  • ApplicationListener 注册监听器来监听应用程序中发布的事件
  • 【Web开发】CSS教学(超详细,满满的干货)
  • C#八皇后算法:回溯法 vs 列优先法 vs 行优先法 vs 对角线优先法
  • 如何在WordPress网站上设置多语言展示
  • 系列学习前端之第 5 章:学习 ES6 ~ ES11
  • C语言经典面试题目(七)
  • 【Java刷题篇】串联所有单词的子串
  • Java常见问题:编辑tomcat运行环境、部署若伊系统
  • springboot使用socket和端口启动gRPC服务器的比较
  • 【计算机网络】什么是http?
  • 2.3 性能度量
  • 柔性数组(变长数组)介绍
  • 【C语言】字符函数与字符串函数以及内存函数 { 超详细攻略,一篇学会 }
  • Windows10中配置并使用nvidia-smi