• elasticsearch笔记(4) java操作es的查询_01----- term和terms查询


    一. 准备数据

    1.实体类

     1 package com.qf.secondspringboot.entity;
     2 
     3 import com.fasterxml.jackson.annotation.JsonFormat;
     4 import com.fasterxml.jackson.annotation.JsonIgnore;
     5 import lombok.AllArgsConstructor;
     6 import lombok.Data;
     7 import lombok.NoArgsConstructor;
     8 
     9 import java.util.Date;
    10 
    11 /**elasticsearch练习用的类
    12  * author wmq
    13  *
    14  * @date 2021/2/1419:41
    15  */
    16 @Data
    17 @AllArgsConstructor
    18 @NoArgsConstructor
    19 public class SmsLogs {
    20     @JsonIgnore
    21     private String id; // id
    22 
    23     @JsonFormat(pattern = "yyyy-MM-dd")
    24     private Date createDate; // 创建时间
    25 
    26     @JsonFormat(pattern = "yyyy-MM-dd")
    27     private Date sendDate; // 发送时间
    28 
    29     private String longCode; // 发送的长号码
    30     private String mobile; // 手机号
    31     private String corpName;  // 发送公司名称
    32     private String smsContent; // 短信内容
    33     private Integer start; // 短信发送状态,0成功,1失败
    34     private Integer operatorId; // 运营商编号 1移动 2联通 3电信
    35     private String province; // 省份
    36     private String ipAddr; // 服务器ip地址
    37     private Integer replyTotal; // 短信状态报告返回时长(秒)
    38     private Integer fee; // 费用
    39 }

    2 准备es的index 和document数据

      1 package com.qf.secondspringboot.mapper;
      2 
      3 import com.fasterxml.jackson.databind.ObjectMapper;
      4 import com.qf.secondspringboot.SecondSpringbootApplicationTests;
      5 import com.qf.secondspringboot.entity.SmsLogs;
      6 import org.apache.http.HttpHost;
      7 import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
      8 import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
      9 import org.elasticsearch.action.bulk.BulkRequest;
     10 import org.elasticsearch.action.bulk.BulkResponse;
     11 import org.elasticsearch.action.index.IndexRequest;
     12 import org.elasticsearch.client.RequestOptions;
     13 import org.elasticsearch.client.RestClient;
     14 import org.elasticsearch.client.RestClientBuilder;
     15 import org.elasticsearch.client.RestHighLevelClient;
     16 import org.elasticsearch.common.settings.Settings;
     17 import org.elasticsearch.common.xcontent.XContentBuilder;
     18 import org.elasticsearch.common.xcontent.XContentType;
     19 import org.elasticsearch.common.xcontent.json.JsonXContent;
     20 import org.junit.jupiter.api.Test;
     21 
     22 import java.io.IOException;
     23 import java.util.Date;
     24 
     25 /**
     26  * author wmq
     27  *
     28  * @date 2021/2/1419:44
     29  */
     30 public class ElasticSearch_Query_Text extends SecondSpringbootApplicationTests {
     31     String index = "sms-logs-index";
     32     String type = "sms-logs-type";
     33 
     34     //创建索引和文档
     35     @Test
     36     public void CreateIndexForSms() throws IOException, IOException {
     37         // 创建索引
     38         Settings.Builder settings = Settings.builder()
     39                 .put("number_of_shards", 5)
     40                 .put("number_of_replicas", 1);
     41         // 指定mappings
     42         XContentBuilder mappings = JsonXContent.contentBuilder()
     43                 .startObject()
     44                     .startObject("properties")
     45                         .startObject("createDate")
     46                             .field("type", "date")
     47                             .field("format", "yyyy-MM-dd")
     48                         .endObject()
     49                         .startObject("sendDate")
     50                             .field("type", "date")
     51                             .field("format", "yyyy-MM-dd")
     52                         .endObject()
     53                         .startObject("longCode")
     54                             .field("type", "keyword")
     55                         .endObject()
     56                         .startObject("mobile")
     57                             .field("type", "keyword")
     58                         .endObject()
     59                         .startObject("corpName")
     60                             .field("type", "keyword")
     61                         .endObject()
     62                         .startObject("smsContent")
     63                             .field("type", "text")
     64                             .field("analyzer", "ik_max_word")
     65                         .endObject()
     66                         .startObject("state")
     67                             .field("type", "integer")
     68                         .endObject()
     69                         .startObject("operatorId")
     70                             .field("type", "integer")
     71                         .endObject()
     72                         .startObject("province")
     73                             .field("type", "keyword")
     74                         .endObject()
     75                         .startObject("ipAddr")
     76                             .field("type", "ip")
     77                         .endObject()
     78                         .startObject("replyTotal")
     79                             .field("type", "integer")
     80                         .endObject()
     81                         .startObject("fee")
     82                             .field("type", "long")
     83                         .endObject()
     84                     .endObject()
     85                 .endObject();
     86 
     87 
     88 
     89         // 将settings和mappings封装为Request对象
     90         String index = "sms-logs-index";
     91         String type = "sms-logs-type";
     92 
     93         HttpHost httpHost = new HttpHost("192.168.43.30", 9200);
     94         RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
     95         RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
     96 
     97         CreateIndexRequest request = new CreateIndexRequest(index)
     98                 .settings(settings)
     99                 .mapping(type, mappings);
    100         // 通过Client连接
    101         CreateIndexResponse res = restHighLevelClient.indices()
    102                 .create(request, RequestOptions.DEFAULT);
    103         System.out.println(res.toString());
    104     }
    105 
    106     // 测试数据
    107     @Test
    108     public void CreateTestData() throws IOException {
    109         // 准备多个json数据
    110         SmsLogs s1 = new SmsLogs("1",new Date(),new Date(),"10690000988","1370000001","途虎养车","【途虎养车】亲爱的刘女士,您在途虎购买的货物单号(Th12345678)",0,1,"上海","10.126.2.9",10,3);
    111         SmsLogs s2 = new SmsLogs("2",new Date(),new Date(),"84690110988","1570880001","韵达快递","【韵达快递】您的订单已配送不要走开哦,很快就会到了,配送员:王五,电话:15300000001",0,1,"上海","10.126.2.8",13,5);
    112         SmsLogs s3 = new SmsLogs("3",new Date(),new Date(),"10698880988","1593570001","滴滴打车","【滴滴打车】指定的车辆现在距离您1000米,马上就要到了,请耐心等待哦,司机:李师傅,电话:13890024793",0,1,"河南","10.126.2.7",12,10);
    113         SmsLogs s4 = new SmsLogs("4",new Date(),new Date(),"20697000911","1586890005","中国移动","【中国移动】尊敬的客户,您充值的话费100元,现已经成功到账,您的当前余额为125元,2020年12月18日14:35",0,1,"北京","10.126.2.6",11,4);
    114         SmsLogs s5 = new SmsLogs("5",new Date(),new Date(),"18838880279","1562384869","网易","【网易】亲爱的玩家,您已经排队成功,请尽快登录到网易云游戏进行游玩,祝您游戏愉快---网易云游戏",0,1,"杭州","10.126.2.5",10,2);
    115         // 转为json
    116         ObjectMapper mapper = new ObjectMapper();
    117         String json1 = mapper.writeValueAsString(s1);
    118         String json2 = mapper.writeValueAsString(s2);
    119         String json3 = mapper.writeValueAsString(s3);
    120         String json4 = mapper.writeValueAsString(s4);
    121         String json5 = mapper.writeValueAsString(s5);
    122 
    123         // request,将数据封装进去
    124         BulkRequest request = new BulkRequest();
    125         request.add(new IndexRequest(index,type,s1.getId()).source(json1, XContentType.JSON));
    126         request.add(new IndexRequest(index,type,s2.getId()).source(json2,XContentType.JSON));
    127         request.add(new IndexRequest(index,type,s3.getId()).source(json3,XContentType.JSON));
    128         request.add(new IndexRequest(index,type,s4.getId()).source(json4,XContentType.JSON));
    129         request.add(new IndexRequest(index,type,s5.getId()).source(json5,XContentType.JSON));
    130 
    131         // client执行
    132         HttpHost httpHost = new HttpHost("192.168.43.30", 9200);
    133         RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
    134         RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
    135         BulkResponse response = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
    136         System.out.println(response);
    137     }
    138 
    139 
    140 
    141 }

    二. term查询(完全匹配)

       不会对搜索的关键字进行分词, 通常对应type是keyword而不是text的类型的查询, 如查询省名,黑龙江, 不需要分词黑龙  江.

       注意: 用到text类型中时候, 查询可能会因为分词得不到. 比如"奋斗的时代"作为关键词用term查询, 在text类型中, "奋斗的时代" 被分为 "奋斗,奋,斗,时代"三个词, 而没有整个的奋斗的时代. 因为term查询不分词, 所以查询不到.

    kibana中使用精准查询:

     

    java中的精准查询

     1 @Test
     2     public void termQueryText() 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 
     9 //        2.指定查询条件(先用searchSourceBuilder,再searchrequest指定searchSourceBuilder )
    10         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    11         searchSourceBuilder.from(0);
    12         searchSourceBuilder.size(20);
    13         searchSourceBuilder.query(QueryBuilders.termQuery("province","北京"));
    14 
    15         searchRequest.source(searchSourceBuilder);
    16 
    17 //        3.执行查询
    18         // client执行
    19         HttpHost httpHost = new HttpHost("192.168.43.30", 9200);
    20         RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
    21         RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
    22         SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    23 //        4.获取数据(source中的数据)
    24         SearchHit[] hits = search.getHits().getHits();//从kibana里的结果可以看出来有两个hits
    25         for (SearchHit searchHit : hits){
    26             Map<String, Object> map = searchHit.getSourceAsMap();
    27             System.out.println(map);
    28         }
    29 
    30     }

    三.terms查询

        3.1 terms查询与term查询都不会对分词,

        3.2 terms表示有多个精准查询条件, 相当于where xx == xx or xx ==xxx 或者是in条件

     在kibana中terms查询:

    在java中实现:

     1  @Test
     2     public void testTerms() 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.from(0);
    11         searchSourceBuilder.size(20);
    12         searchSourceBuilder.query(QueryBuilders
    13                 .termsQuery("province","北京", "上海","河南")); //用termsQuery仅仅是将参数变成多个
    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 //        4.获取数据(source中的数据)
    23         SearchHit[] hits = search.getHits().getHits();
    24         for (SearchHit searchHit : hits){
    25             Map<String, Object> map = searchHit.getSourceAsMap();
    26             System.out.println(map);
    27         }
    28 
    29     }
  • 相关阅读:
    软件工程个人作业01
    个人冲刺——(五)
    个人冲刺——(四)
    个人冲刺——(三)
    个人冲刺——(二)
    个人冲刺——(一)
    单词统计
    第十周学习总结
    第九周学习总结
    用户模板场景分析
  • 原文地址:https://www.cnblogs.com/dangdanghepingping/p/14403199.html
Copyright © 2020-2023  润新知