邏輯結構部分是一個倒排索引表:
一、將要搜索的文檔內容分詞,全部不重複的詞組成分詞列表。
二、將搜索的文檔最終以Document方式存儲起來。
三、每一個詞和docment都有關聯。java
下面是Elasticsearch與MySQL數據庫邏輯結構概念的對比數據庫
索引(index) =數據庫(databases)
類型(type) =表(table)
文檔(document)= 行(row)緩存
字符串類型網絡
text
當一個字段是要被全文搜索的,好比Email內容、產品描述,應該使用text類型。設置text類型之後,字段內容會被分析,在生成倒排索引之前,字符串會被分析器分紅一個一個詞項。text類型的字段不用於排序,不多用於聚合。
keyword
keyword類型適用於索引結構化的字段,好比email地址、主機名、狀態碼和標籤。若是字段須要進行過濾(好比查找已發佈博客中status屬性爲published的文章)、排序、聚合。keyword類型的字段只能經過精確值搜索到。elasticsearch
elasticsearch 存在三種Java客戶端。性能
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch‐rest‐high‐level‐client</artifactId> <version>6.5.3</version> </dependency>
插入單條數據:ui
HttpHost : url地址封裝
RestClientBuilder: rest客戶端構建器url
IndexRequest: 新增或修改請求
IndexResponse:新增或修改的響應結果rest
//1.鏈接rest接口 HttpHost http=new HttpHost("127.0.0.1",9200,"http"); RestClientBuilder builder= RestClient.builder(http);//rest構建器 RestHighLevelClient restHighLevelClient=new RestHighLevelClient(builder);//高級客戶端對象 //2.封裝請求對象 IndexRequest indexRequest=new IndexRequest("sku","doc","3"); Map skuMap =new HashMap(); skuMap.put("name","華爲p30pro"); skuMap.put("saleNum",101021); skuMap.put("commentNum",10102321); Map spec=new HashMap(); spec.put("網絡制式","移動4G"); spec.put("屏幕尺寸","5"); skuMap.put("spec",spec); indexRequest.source(skuMap); //3.獲取響應結果 IndexResponse response = restHighLevelClient.index(indexRequest,RequestOptions.DEFAULT); int status = response.status().getStatus(); System.out.println(status); restHighLevelClient.close();
若是ID不存在則新增,若是ID存在則修改。
批處理請求:
BulkRequest: 批量請求(用於增刪改操做)
BulkResponse:批量請求(用於增刪改操做)code
//1.鏈接rest接口 HttpHost http=new HttpHost("127.0.0.1",9200,"http"); RestClientBuilder builder= RestClient.builder(http);//rest構建器 RestHighLevelClient restHighLevelClient=new RestHighLevelClient(builder);//高級客戶端對象 //2.封裝請求對象 BulkRequest bulkRequest=new BulkRequest(); IndexRequest indexRequest=new IndexRequest("sku","doc","4"); Map skuMap =new HashMap(); skuMap.put("name","華爲p30pro 火爆上市"); skuMap.put("brandName","華爲"); Map spec=new HashMap(); spec.put("網絡制式","移動4G"); spec.put("屏幕尺寸","5"); skuMap.put("spec",spec); indexRequest.source(skuMap); bulkRequest.add(indexRequest);//能夠屢次添加 //3.獲取響應結果 BulkResponse response =restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT); int status = response.status().getStatus(); System.out.println(status); String message = response.buildFailureMessage(); System.out.println(message); restHighLevelClient.close();
SearchRequest: 查詢請求對象
SearchResponse:查詢響應對象
SearchSourceBuilder:查詢源構建器
MatchQueryBuilder:匹配查詢構建器
示例:查詢商品名稱包含手機的記錄。
//1.鏈接rest接口 HttpHost http=new HttpHost("127.0.0.1",9200,"http"); RestClientBuilder builder= RestClient.builder(http);//rest構建器 RestHighLevelClient restHighLevelClient=new RestHighLevelClient(builder);//高級客戶端對象 //2.封裝查詢請求 SearchRequest searchRequest=new SearchRequest("sku"); searchRequest.types("doc"); //設置查詢的類型 SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder(); MatchQueryBuilder matchQueryBuilder= QueryBuilders.matchQuery("name","手機"); searchSourceBuilder.query(matchQueryBuilder); searchRequest.source(searchSourceBuilder); //3.獲取查詢結果 SearchResponse searchResponse = restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT); SearchHits searchHits = searchResponse.getHits(); long totalHits = searchHits.getTotalHits(); System.out.println("記錄數:"+totalHits); SearchHit[] hits = searchHits.getHits(); for(SearchHit hit:hits){ String source = hit.getSourceAsString(); System.out.println(source); } restHighLevelClient.close();
BoolQueryBuilder:布爾查詢構建器
TermQueryBuilder:詞條查詢構建器
QueryBuilders:查詢構建器工廠
示例:查詢名稱包含手機的,而且品牌爲小米的記錄
//1.鏈接rest接口 同上...... //2.封裝查詢請求 SearchRequest searchRequest=new SearchRequest("sku"); searchRequest.types("doc"); //設置查詢的類型 SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();//布爾查詢構建器 MatchQueryBuilder matchQueryBuilder= QueryBuilders.matchQuery("name","手機"); boolQueryBuilder.must(matchQueryBuilder); TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("brandName","小米"); boolQueryBuilder.must(termQueryBuilder); searchSourceBuilder.query(boolQueryBuilder); searchRequest.source(searchSourceBuilder); //3.獲取查詢結果 同上......
過濾是針對搜索的結果進行過濾,過濾器主要判斷的是文檔是否匹配,不去計算和判斷文檔的匹配度得分,因此過濾器性能比查詢要高,且方便緩存,推薦儘可能使用過濾器去實現查詢或者過濾器和查詢共同使用。
示例:篩選品牌爲小米的記錄
//1.鏈接rest接口 同上..... //2.封裝查詢請求 SearchRequest searchRequest=new SearchRequest("sku"); searchRequest.types("doc"); //設置查詢的類型 SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();//布爾查詢構建器 TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("brandName","小米"); boolQueryBuilder.filter(termQueryBuilder); searchSourceBuilder.query(boolQueryBuilder); searchRequest.source(searchSourceBuilder); //3.獲取查詢結果 同上....
示例:按分組名稱聚合查詢,統計每一個分組的數量
GET /sku/_search { "size" : 0, "aggs" : { "sku_category" : { "terms" : { "field" : "categoryName" } } } } size爲0 不會將數據查詢出來,目的是讓查詢更快。
結果:
{ "took": 6, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 5, "max_score": 0, "hits": [] }, "aggregations": { "sku_category": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "手機", "doc_count": 3 }, { "key": "電視", "doc_count": 2 } ] } } }
AggregationBuilders:聚合構建器工廠
TermsAggregationBuilder:詞條聚合構建器
Aggregations:分組結果封裝
Terms.Bucket: 桶
示例:按商品分類分組查詢,求出每一個分類的文檔數
//1.鏈接rest接口 HttpHost http=new HttpHost("127.0.0.1",9200,"http"); RestClientBuilder builder= RestClient.builder(http);//rest構建器 RestHighLevelClient restHighLevelClient=new RestHighLevelClient(builder);//高級客戶端對象 //2.封裝查詢請求 SearchRequest searchRequest=new SearchRequest("sku"); searchRequest.types("doc"); //設置查詢的類型 SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder(); TermsAggregationBuilder termsAggregationBuilder =AggregationBuilders.terms("sku_category").field("categoryName"); //這個field是真正的列,terms是定義的臨時別名 searchSourceBuilder.aggregation(termsAggregationBuilder); searchSourceBuilder.size(0);//不會將數據查詢出來,目的是讓查詢更快。 searchRequest.source(searchSourceBuilder); //3.獲取查詢結果 SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); Aggregations aggregations = searchResponse.getAggregations(); Map<String, Aggregation> asMap = aggregations.getAsMap(); Terms terms = (Terms) asMap.get("sku_category"); List<? extends Terms.Bucket> buckets = terms.getBuckets(); for(Terms.Bucket bucket:buckets){ System.out.println(bucket.getKeyAsString()+":"+ bucket.getDocCount() ); } restHighLevelClient.close();