一. 准備數據
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 }