微服务day09

DSL查询

快速入门

GET /items/_search
{
  "query": {
    "match_all": {}
  }
}

叶子查询

GET /items/_search
{
  "query": {
    "match_all": {}
  }
}


GET /items/_search
{
  "query": {
    "multi_match": {
      "query": "脱脂牛奶",
      "fields": ["name"]
    }
  }
}

GET /items/_search
{
  "query": {
    "term": {
      "price": {
        "value": "80900"
      }
    }
  }
}

GET /items/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 100000,
        "lte": 1000000
      }
    }
  }
}

GET /items/_search
{
  "query": {
    "ids": {
      "values": ["688000","1861100"]
    }
  }
}

复合查询

案例

GET /items/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match":{
            "name":"智能手机"
          }
        }
      ],
      "filter": [
        {
          "term":{
            "brand":"华为"
            }
          },
          {
            "range": {
            "price": {
              "gte": 90000,
              "lte": 159900
            }
          
          }
        }
      ]
    }
  }
}

排序和分页

排序案例

GET /items/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "sold": {
        "order": "desc"
      }
    },
    {
      "price":{
      "order": "asc"
    }
    }
    
  ]
}

#排序简写
GET /items/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "sold":  "desc"
    },
    {
      "price": "asc"
    }
  ]
}

分页

分页案例

#排序分页
GET /items/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "sold":  "desc"
    },
    {
      "price": "asc"
    }
  ],
  "from": 0,
  "size": 10
}

高亮显示

默认情况下添加的标签就是 em。

返回值将高亮字段放在highlight中,不会修改源数据。

GET /items/_search
{
  "query": {
    "multi_match": {
      "query": "脱脂牛奶",
      "fields": ["name"]
    }
  },
  "highlight": {
    "fields": {
      "name":{
        "pre_tags": "<em>",
        "post_tags": "</em>"
      }
    }
  }
}

GET /items/_search
{
  "query": {
    "multi_match": {
      "query": "脱脂牛奶",
      "fields": ["name"]
    }
  },
  "highlight": {
    "fields": {
      "name":{}
    }
  }
}

查询结果相同。

结果:

{
        "_index" : "items",
        "_type" : "_doc",
        "_id" : "12179607155",
        "_score" : 22.290178,
        "_source" : {
          "id" : "12179607155",
          "name" : """【沃尔玛】艾思达/ASDA纯牛奶 灭菌乳 早餐奶 牛奶 英国 进口 全脂\脱脂\部分脱脂 部分脱脂牛奶 1L*6""",
          "price" : 96300,
          "image" : "https://m.360buyimg.com/mobilecms/s720x720_jfs/t5977/320/3555243042/134144/38730483/59546920Nbe3ddc70.jpg!q70.jpg.webp",
          "category" : "牛奶",
          "brand" : "ASDA",
          "sold" : 0,
          "commentCount" : 0,
          "isAD" : false,
          "updateTime" : 1556640000000
        },
        "highlight" : {
          "name" : [
            """【沃尔玛】艾思达/ASDA纯<em>牛</em><em>奶</em> 灭菌乳 早餐<em>奶</em> <em>牛</em><em>奶</em> 英国 进口 全<em>脂</em>\<em>脱</em><em>脂</em>\部分<em>脱</em><em>脂</em> 部分<em>脱</em><em>脂</em><em>牛</em><em>奶</em> 1L*6"""
          ]
        }
      },

查询总结

JavaRestClient

快速入门

    @org.junit.jupiter.api.Test
    public void testList() throws IOException {
        //创建对象
        SearchRequest searchRequest = new SearchRequest("items");
        
        //构建DSL
        searchRequest.source().query(QueryBuilders.matchAllQuery());
        
        //发送请求
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);

        //解析响应
        SearchHits hits = search.getHits();

        long value = hits.getTotalHits().value;
        System.out.println(value);

        SearchHit[] hits1 = hits.getHits();
        for (SearchHit hit : hits1) {
            String json = hit.getSourceAsString();
            ItemDoc bean = JSONUtil.toBean(json, ItemDoc.class);
            System.out.println(bean);
        }

    }

构造查询条件

案例

两种写法:

java 复制代码
    //复杂条件查询
    @org.junit.jupiter.api.Test
    public void testbool() throws IOException {
        //        //创建对象
        SearchRequest searchRequest = new SearchRequest("items");
        //创建bool对象
        //        //构建DSL
        BoolQueryBuilder query = new BoolQueryBuilder();
        //添加must查询
        query.must(QueryBuilders.matchQuery("name","脱脂牛奶"));
        //添加两个filter查询
        query.filter(QueryBuilders.termQuery("brand","德亚"));
        query.filter(QueryBuilders.rangeQuery("price").lt(30000));

        searchRequest.source().query(query);

        //发送请求
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);

        //解析响应
        SearchHits hits = search.getHits();

        long value = hits.getTotalHits().value;
        System.out.println(value);

        SearchHit[] hits1 = hits.getHits();
        for (SearchHit hit : hits1) {
            String json = hit.getSourceAsString();
            ItemDoc bean = JSONUtil.toBean(json, ItemDoc.class);
            System.out.println(bean);
        }

    }
java 复制代码
    @Test
    void testBool() throws IOException {
        // 1.创建Request
        SearchRequest request = new SearchRequest("items");
        // 2.组织请求参数
        // 2.1.准备bool查询
        BoolQueryBuilder bool = QueryBuilders.boolQuery();
        // 2.2.关键字搜索
        bool.must(QueryBuilders.matchQuery("name", "脱脂牛奶"));
        // 2.3.品牌过滤
        bool.filter(QueryBuilders.termQuery("brand", "德亚"));
        // 2.4.价格过滤
        bool.filter(QueryBuilders.rangeQuery("price").lte(30000));
        request.source().query(bool);
        // 3.发送请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        // 4.解析响应
//        handleResponse(response);
        //解析响应
        SearchHits hits = response.getHits();

        long value = hits.getTotalHits().value;
        System.out.println(value);

        SearchHit[] hits1 = hits.getHits();
        for (SearchHit hit : hits1) {
            String json = hit.getSourceAsString();
            ItemDoc bean = JSONUtil.toBean(json, ItemDoc.class);
            System.out.println(bean);
        }
    }

排序和分页

java 复制代码
    @org.junit.jupiter.api.Test
    public void testSoudAndPage() throws IOException {
        int pageNum = 2;
        int pageSize = 5;
        //创建对象
        SearchRequest searchRequest = new SearchRequest("items");

        //构建DSL
        //设置分页和排序
        searchRequest.source().query(QueryBuilders.matchAllQuery())
                .from((pageNum-1)*pageSize).size(pageSize)
                .sort("sold", SortOrder.DESC)
                .sort("price",SortOrder.ASC);

        //发送请求
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);

        //解析响应
        SearchHits hits = search.getHits();

        long value = hits.getTotalHits().value;
        System.out.println(value);

        SearchHit[] hits1 = hits.getHits();
        for (SearchHit hit : hits1) {
            String json = hit.getSourceAsString();
            ItemDoc bean = JSONUtil.toBean(json, ItemDoc.class);
            System.out.println(bean);
        }

    }

高亮显示

java 复制代码
    @org.junit.jupiter.api.Test
    public void testHighLight() throws IOException {
        //创建对象
        SearchRequest searchRequest = new SearchRequest("items");

        //构建DSL
        searchRequest.source().query(QueryBuilders.matchQuery("name","脱脂牛奶"));
        //设置高亮,两种都可以,第二种的底层就是第一种
//        searchRequest.source().highlighter(new HighlightBuilder().field("name"));
        searchRequest.source().highlighter(SearchSourceBuilder.highlight().field("name"));

        //发送请求
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);

        //解析响应
        SearchHits hits = search.getHits();

        long value = hits.getTotalHits().value;
        System.out.println(value);

        SearchHit[] hits1 = hits.getHits();
        for (SearchHit hit : hits1) {
            String json = hit.getSourceAsString();
            ItemDoc bean = JSONUtil.toBean(json, ItemDoc.class);
            //获取高亮显示的字段,并写入对象中
            Map<String, HighlightField> hgf = hit.getHighlightFields();
            //判断是否存在
            if (CollUtil.isNotEmpty(hgf)){
                //存在高亮字段
                //根距key获取数据
                HighlightField hf = hgf.get("name");
                if (hf != null){
                    //在添加高亮时如果字段过长就会断成几部分使用数组返回
                    Text[] fragments = hf.getFragments();
                    String string = fragments[0].string();
                    //将高亮字段进行替换
                    bean.setName(string);
                }
            }
            System.out.println(bean);
        }

    }

数据聚合

DSL聚合

由于出现问题详见:https://b11et3un53m.feishu.cn/wiki/WtCmwJQQDijsBikYLviceT9lnke

java 复制代码
@Test
void testAgg() throws IOException {
    // 1.创建Request
    SearchRequest request = new SearchRequest("items");
    // 2.准备请求参数
    BoolQueryBuilder bool = QueryBuilders.boolQuery()
            .filter(QueryBuilders.termQuery("category", "手机"))
            .filter(QueryBuilders.rangeQuery("price").gte(300000));
    request.source().query(bool).size(0);
    // 3.聚合参数
    request.source().aggregation(
            AggregationBuilders.terms("brand_agg").field("brand").size(5)
    );
    // 4.发送请求
    SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    // 5.解析聚合结果
    Aggregations aggregations = response.getAggregations();
    // 5.1.获取品牌聚合
    Terms brandTerms = aggregations.get("brand_agg");
    // 5.2.获取聚合中的桶
    List<? extends Terms.Bucket> buckets = brandTerms.getBuckets();
    // 5.3.遍历桶内数据
    for (Terms.Bucket bucket : buckets) {
        // 5.4.获取桶内key
        String brand = bucket.getKeyAsString();
        System.out.print("brand = " + brand);
        long count = bucket.getDocCount();
        System.out.println("; count = " + count);
    }
}
相关推荐
The_tuber_sadness4 分钟前
Kotlin深度面试题:协程、密封类和高阶函数
android·开发语言·kotlin
直裾4 分钟前
Scala的Set集合
开发语言·后端·scala
anqi277 分钟前
Scala 的Map集合
开发语言·后端·scala
喻师傅8 分钟前
Scala-字符串(拼接、printf格式化输出等)-用法详解
大数据·开发语言·scala
1momomo汉堡包8 分钟前
Scala的Array(1)
开发语言·后端·scala
Zoran_卓9 分钟前
在k8s上部署Crunchy Postgres for Kubernetes
数据库·容器·kubernetes
CaiGbro23 分钟前
IDEA自定义文件打开格式
java·git·intellij-idea
JavaEdge.28 分钟前
ShardingSphere 如何完美驾驭分布式事务与 XA 协议?
java
繁星璀璨G31 分钟前
C++11标准模板(STL)- 常用数学函数 - 宏常量 - 求值得到float类型的安静NaN(NAN)
开发语言·c++·算法·stl·常用数学函数·nan
不爱学习的YY酱31 分钟前
【操作系统不挂科】<线程概念(6)>选择题&简答题(带答案与解析)
linux·开发语言·操作系统