zoukankan      html  css  js  c++  java
  • elasticsearch笔记(7) 复合查询_boolean查询 boolsting查询 filter查询

    6 复合查询

    6.1 bool查询

    将多个查询条件以一定的逻辑组合在一起

    • must:表示and的意思,所有的条件都符合才能找到
    • must_not:把满足条件的都去掉的结果
    • should:表示or的意思
    # 查询省份是上海或者河南
    # 运营商不是联通
    # smsContent中包含中国和移动
    # bool查询
     1 POST /sms-logs-index/sms-logs-type/_search
     2 {
     3   "query": {
     4     "bool": {
     5       "should": [
     6         {
     7           "term": {
     8             "province": {
     9               "value": "河南"
    10             }
    11           }
    12         },  
    13         {
    14           "term": {
    15             "province": {
    16               "value": "北京"
    17              }
    18             }
    19          }
    20       ],
    21       "must_not": [
    22         {
    23           "term": {
    24             "operatorId": {
    25               "value": "2"
    26             }
    27           }
    28         }
    29       ],
    30       "must": [
    31         {
    32           "match": {
    33             "smsContent": "中国"
    34           }
    35         },
    36         {
    37           "match": {
    38             "smsContent": "移动"
    39           }
    40         }
    41       ]
    42     }
    43   }
    44 }

    java中的聚合查询

     1 @Test
     2     public void bolleanTest() throws IOException {
     3         //        1. 创建查询对象
     4         String index = "sms-logs-index";
     5         String type = "sms-logs-type";
     6         SearchRequest searchRequest = new SearchRequest(index);//指定索引
     7         searchRequest.types(type);//指定类型
     8 //    2. 封装查询条件
     9         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    10         BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    11         boolQueryBuilder.should(QueryBuilders.termQuery("province","河南"));
    12         boolQueryBuilder.should(QueryBuilders.termQuery("province","北京"));
    13         boolQueryBuilder.mustNot(QueryBuilders.termQuery("operatorId","2"));
    14         boolQueryBuilder.must(QueryBuilders.matchQuery("smsContent","中国"));
    15         boolQueryBuilder.must(QueryBuilders.matchQuery("smsContent","移动"));
    16         searchSourceBuilder.query(boolQueryBuilder);
    17         searchRequest.source(searchSourceBuilder);
    18 
    19 
    20 
    21         //        3.执行查询
    22         // client执行
    23         HttpHost httpHost = new HttpHost("192.168.43.30", 9200);
    24         RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
    25         RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
    26         SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    27 
    28 //        4.获取数据(source中的数据)
    29         SearchHit[] hits = search.getHits().getHits();
    30         for(SearchHit searchHit : hits){
    31             System.out.println(searchHit);
    32         }
    33     }

    6.2 boolsting

    分数查询,比如math查询,结果有一个匹配度分数.  boolsting查询可以针对内容,让其分数大,或者小,达到排前,排后的效果

    • positive: 指定查询条件,  只有匹配到positive的内容,才会放到结果集,也就是放查询条件的地方
    • negative:如果匹配到的positive和negative,就会降低文档的分数
    • negative_boost:对匹配到的positive和negative的内容, 指定降低分数的系数. 这个系数必须小于1.0,比如:10分 这个系数为0.5就会变为5分

    关于分数的计算:

    • 关键字在文档出现的频次越高,分数越高
    • 文档的内容越短,分数越高
    • 搜索时候,指定的关键字会被分词,分词内容匹配分词库,匹配的个数越多,分数就越高
     1 # boosting查询
     2 POST /sms-logs-index/sms-logs-type/_search
     3 {
     4   "query": {
     5     "boosting": {
     6       "positive": { #这三个postitive. negative 和 negative_boost是在一级的.
     7         "match": {
     8           "smsContent": {
     9              "query": "电话玩家活物",
    10                "operator": "or"
    11           }
    12          }
    13       },
    14       "negative": { //匹配到这个的就按negative_boost在分数中减少
    15         "match": {
    16           "smsContent": "订单"
    17         }
    18       },
    19       "negative_boost": 0.1
    20     }
    21   }
    22 }

    java中用boosting查询

     1     @Test
     2     public void boostingTest() throws IOException {
     3         //        1. 创建查询对象
     4         String index = "sms-logs-index";
     5         String type = "sms-logs-type";
     6         SearchRequest searchRequest = new SearchRequest(index);//指定索引
     7         searchRequest.types(type);//指定类型
     8 //    2. 封装查询条件
     9         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    10 
    11         BoostingQueryBuilder boostingQueryBuilder = QueryBuilders.boostingQuery(
    12                 QueryBuilders.matchQuery("smsContent", "电话").operator(Operator.OR),//相当于positivequery
    13                 QueryBuilders.matchQuery("smsContent", "订单")
    14         ).negativeBoost(0.1f);
    15 
    16         searchSourceBuilder.query(boostingQueryBuilder);
    17         searchRequest.source(searchSourceBuilder);
    18 
    19 
    20 
    21         //        3.执行查询
    22         // client执行
    23         HttpHost httpHost = new HttpHost("192.168.43.30", 9200);
    24         RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
    25         RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
    26         SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    27 
    28 //        4.获取数据(source中的数据)
    29         SearchHit[] hits = search.getHits().getHits();
    30         for(SearchHit searchHit : hits){
    31             System.out.println(searchHit);
    32         }
    33     }

    二, filter查询:  如果结果不需要匹配度

         与query查询相比, 会计算分数, 并对结果排序

         filter不计算分数, 结果也不排序

     filter查询在java中应用

     1     @Test
     2     public void filterTest() throws IOException {
     3         //        1. 创建查询对象
     4         String index = "sms-logs-index";
     5         String type = "sms-logs-type";
     6         SearchRequest searchRequest = new SearchRequest(index);//指定索引
     7         searchRequest.types(type);//指定类型
     8 //    2. 封装查询条件
     9         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    10         BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();//先创建Boolean查询对象
    11         boolQueryBuilder.filter(QueryBuilders.termQuery("corpName", "网易"));
    12 
    13 
    14         searchSourceBuilder.query(boolQueryBuilder);
    15         searchRequest.source(searchSourceBuilder);
    16 
    17 
    18 
    19         //        3.执行查询
    20         // client执行
    21         HttpHost httpHost = new HttpHost("192.168.43.30", 9200);
    22         RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
    23         RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
    24         SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    25 
    26 //        4.获取数据(source中的数据)
    27         SearchHit[] hits = search.getHits().getHits();
    28         for(SearchHit searchHit : hits){
    29             System.out.println(searchHit);
    30         }
    31     }

    三. 高亮查询: 将查询到的内容自动加到高亮标签中.

    将用户输入的内容,以高亮的样式展示出来,查询的结果会附带在hits下面以单独的形式返回,不会影响查询的结果

    ES提供了一个hightlight的属性,和query同级别,其属性如下:

    • fragment_size:指定要展示多少内容,可以看到百度的内容后面有...还有很长,默认100个
    • pre_tags:指定前缀标签 比如:就是红色
    • post_tags:指定后缀标签:
    • fields:指定哪几个field以高亮形式返回

     java中用highlight查询

     1     @Test
     2     public void highlightQuery() throws IOException {
     3         //        1. 创建查询对象
     4         String index = "sms-logs-index";
     5         String type = "sms-logs-type";
     6         SearchRequest searchRequest = new SearchRequest(index);//指定索引
     7         searchRequest.types(type);//指定类型
     8 //    2. 封装查询条件
     9         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    10         searchSourceBuilder.query(QueryBuilders.matchQuery("smsContent", "网易"));
    11         HighlightBuilder highlightBuilder = new HighlightBuilder();
    12         highlightBuilder.field("smsContent") .preTags("<font color='red'>").postTags("</font>").fragmentSize(20);
    13         searchSourceBuilder.highlighter(highlightBuilder);
    14         searchRequest.source(searchSourceBuilder);
    15 
    16         //        3.执行查询
    17         // client执行
    18         HttpHost httpHost = new HttpHost("192.168.43.30", 9200);
    19         RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
    20         RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
    21         SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    22 
    23 //        4.获取数据(source中的数据)
    24         SearchHit[] hits = search.getHits().getHits();
    25         for(SearchHit searchHit : hits){
    26             System.out.println(searchHit);
    27         }
    28     }
  • 相关阅读:
    汇编语言LAHF和SAHF指令
    JSONHelper
    【CSS】利用宽高比例的媒体查询
    swiftmailer时没有设置https的选项,才可以发送成功。在linux下面
    DevExpress
    IntelliJ IDEA2017 激活方法 最新的激活注册方式方法,破解,密钥
    vue.js 列表追加项写法
    RedisCache 缓存
    时间通用类 datetime
    LogHelper 日志
  • 原文地址:https://www.cnblogs.com/dangdanghepingping/p/14425402.html
Copyright © 2011-2022 走看看