文章目錄
0.學習目標
- 獨立安裝Elasticsearch
- 會使用Rest的API操作索引
- 會使用Rest的API查詢數據
- 會使用Rest的API聚合數據
- 掌握Spring Data Elasticsearch使用
1.Elasticsearch介紹和安裝
用戶訪問我們的首頁,一般都會直接搜索來尋找自己想要購買的商品。
而商品的數量非常多,而且分類繁雜。如果能正確的顯示出用戶想要的商品,並進行合理的過濾,儘快促成交易,是搜索系統要研究的核心。
面對這樣複雜的搜索業務和數據量,使用傳統數據庫搜索就顯得力不從心,一般我們都會使用全文檢索技術,比如之前大家學習過的Solr。
不過今天,我們要講的是另一個全文檢索技術:Elasticsearch。
1.1.簡介
1.1.1.Elastic
Elastic官網:https://www.elastic.co/cn/
Elastic有一條完整的產品線及解決方案:Elasticsearch、Kibana、Logstash等,前面說的三個就是大家常說的ELK技術棧。
1.1.2.Elasticsearch
Elasticsearch官網:https://www.elastic.co/cn/products/elasticsearch
如上所述,Elasticsearch具備以下特點:
- 分佈式,無需人工搭建集羣(solr就需要人爲配置,使用Zookeeper作爲註冊中心)
- Restful風格,一切API都遵循Rest原則,容易上手
- 近實時搜索,數據更新在Elasticsearch中幾乎是完全同步的。
1.1.3.版本
目前Elasticsearch最新的版本是6.3.1,我們就使用6.3.0
需要虛擬機JDK1.8及以上
1.2.安裝和配置
爲了模擬真實場景,我們將在linux下安裝Elasticsearch。
1.2.1.新建一個用戶leyou
出於安全考慮,elasticsearch默認不允許以root賬號運行。
創建用戶:
useradd leyou
設置密碼:
passwd leyou
切換用戶:
su - leyou
1.2.2.上傳安裝包,並解壓
我們將安裝包上傳到:/home/leyou目錄
解壓縮:
tar -zxvf elasticsearch-6.2.4.tar.gz
我們把目錄重命名:
mv elasticsearch-6.2.4/ elasticsearch
上面是leyou用戶解壓的,所屬用戶,所屬組是leyou,如果是root用戶解壓的需要改一下所屬用戶,所屬組:
chown leyou:leyou elasticsearch-6.2.4/ -R
-R是遞歸把文件夾下的所有文件都更改爲leyou
進入,查看目錄結構:
1.2.3.修改配置
我們進入config目錄:cd config
需要修改的配置文件有兩個:
- jvm.options
Elasticsearch基於Lucene的,而Lucene底層是java實現,因此我們需要配置jvm參數。
編輯jvm.options:
vim jvm.options
默認配置如下:
-Xms1g
-Xmx1g
內存佔用太多了,我們調小一些:
256或512
-Xms512m
-Xmx512m
- elasticsearch.yml
vim elasticsearch.yml
- 修改數據和日誌目錄:
path.data: /home/leyou/elasticsearch/data # 數據目錄位置
path.logs: /home/leyou/elasticsearch/logs # 日誌目錄位置
我們把data和logs目錄修改指向了elasticsearch的安裝目錄。但是這兩個目錄並不存在,因此我們需要創建出來。
進入elasticsearch的根目錄,然後創建:
mkdir data
mkdir logs
- 修改綁定的ip:
network.host: 0.0.0.0 # 綁定到0.0.0.0,允許任何ip來訪問
默認只允許本機訪問,修改爲0.0.0.0後則可以遠程訪問
目前我們是做的單機安裝,如果要做集羣,只需要在這個配置文件中添加其它節點信息即可。
elasticsearch.yml的其它可配置信息:
屬性名 | 說明 |
---|---|
cluster.name | 配置elasticsearch的集羣名稱,默認是elasticsearch。建議修改成一個有意義的名稱。 |
node.name | 節點名,es會默認隨機指定一個名字,建議指定一個有意義的名稱,方便管理 |
path.conf | 設置配置文件的存儲路徑,tar或zip包安裝默認在es根目錄下的config文件夾,rpm安裝默認在/etc/ elasticsearch |
path.data | 設置索引數據的存儲路徑,默認是es根目錄下的data文件夾,可以設置多個存儲路徑,用逗號隔開 |
path.logs | 設置日誌文件的存儲路徑,默認是es根目錄下的logs文件夾 |
path.plugins | 設置插件的存放路徑,默認是es根目錄下的plugins文件夾 |
bootstrap.memory_lock | 設置爲true可以鎖住ES使用的內存,避免內存進行swap |
network.host | 設置bind_host和publish_host,設置爲0.0.0.0允許外網訪問 |
http.port | 設置對外服務的http端口,默認爲9200。 |
transport.tcp.port | 集羣結點之間通信端口 |
discovery.zen.ping.timeout | 設置ES自動發現節點連接超時的時間,默認爲3秒,如果網絡延遲高可設置大些 |
discovery.zen.minimum_master_nodes | 主結點數量的最少值 ,此值的公式爲:(master_eligible_nodes / 2) + 1 ,比如:有3個符合要求的主結點,那麼這裏要設置爲2 |
1.3.運行
進入elasticsearch/bin目錄,可以看到下面的執行文件:
然後輸入命令:
./elasticsearch
發現報錯了,啓動失敗:
1.3.1.錯誤1:內核過低
我們使用的是centos6,其linux內核版本爲2.6。而Elasticsearch的插件要求至少3.5以上版本。不過沒關係,我們禁用這個插件即可。
修改elasticsearch.yml文件,在最下面添加如下配置:
bootstrap.system_call_filter: false
然後重啓
1.3.2.錯誤2:文件權限不足
再次啓動,又出錯了:
[1]: max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]
我們用的是leyou用戶,而不是root,所以文件權限不足。
首先用root用戶登錄。
然後修改配置文件:
vim /etc/security/limits.conf
添加下面的內容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 4096
* hard nproc 4096
1.3.3.錯誤3:線程數不夠
剛纔報錯中,還有一行:
[1]: max number of threads [1024] for user [leyou] is too low, increase to at least [4096]
這是線程數不夠。
繼續修改配置:
vim /etc/security/limits.d/90-nproc.conf
修改下面的內容:
* soft nproc 1024
改爲:
* soft nproc 4096
1.3.4.錯誤4:進程虛擬內存
[3]: max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
vm.max_map_count:限制一個進程可以擁有的VMA(虛擬內存區域)的數量,繼續修改配置文件, :
vim /etc/sysctl.conf
添加下面內容:
vm.max_map_count=655360
然後執行命令:
sysctl -p
1.3.5.重啓終端窗口
所有錯誤修改完畢,一定要重啓你的 Xshell終端,否則配置無效。
1.3.6.啓動
再次啓動,終於成功了!
可以看到綁定了兩個端口:
- 9300:集羣節點間通訊接口
- 9200:客戶端訪問接口
我們在瀏覽器中訪問:http://192.168.56.101:9200
1.4.安裝kibana
1.4.1.什麼是Kibana?
Kibana是一個基於Node.js的Elasticsearch索引庫數據統計工具,可以利用Elasticsearch的聚合功能,生成各種圖表,如柱形圖,線狀圖,餅圖等。
而且還提供了操作Elasticsearch索引數據的控制檯,並且提供了一定的API提示,非常有利於我們學習Elasticsearch的語法。
1.4.2.安裝
因爲Kibana依賴於node,我們的虛擬機沒有安裝node,而window中安裝過。所以我們選擇在window下使用kibana。
最新版本與elasticsearch保持一致,也是6.3.0
解壓到特定目錄即可
1.4.3.配置運行
配置
進入安裝目錄下的config目錄,修改kibana.yml文件:
修改elasticsearch服務器的地址:
elasticsearch.url: "http://192.168.56.101:9200"
運行
進入安裝目錄下的bin目錄:
雙擊運行:
發現kibana的監聽端口是5601
我們訪問:http://127.0.0.1:5601
1.4.4.控制檯
選擇左側的DevTools菜單,即可進入控制檯頁面:
在頁面右側,我們就可以輸入請求,訪問Elasticsearch了。
1.5.安裝ik分詞器
Lucene的IK分詞器早在2012年已經沒有維護了,現在我們要使用的是在其基礎上維護升級的版本,並且開發爲ElasticSearch的集成插件了,與Elasticsearch一起維護升級,版本也保持一致,最新版本:6.3.0
1.5.1.安裝
上傳課前資料中的zip包,解壓到Elasticsearch目錄的plugins目錄中:
使用unzip命令解壓:
unzip elasticsearch-analysis-ik-6.3.0.zip -d ik-analyzer
然後重啓elasticsearch:
1.5.2.測試
大家先不管語法,我們先測試一波。
在kibana控制檯輸入下面的請求:
POST _analyze
{
"analyzer": "ik_max_word",
"text": "我是中國人"
}
運行得到結果:
{
"tokens": [
{
"token": "我",
"start_offset": 0,
"end_offset": 1,
"type": "CN_CHAR",
"position": 0
},
{
"token": "是",
"start_offset": 1,
"end_offset": 2,
"type": "CN_CHAR",
"position": 1
},
{
"token": "中國人",
"start_offset": 2,
"end_offset": 5,
"type": "CN_WORD",
"position": 2
},
{
"token": "中國",
"start_offset": 2,
"end_offset": 4,
"type": "CN_WORD",
"position": 3
},
{
"token": "國人",
"start_offset": 3,
"end_offset": 5,
"type": "CN_WORD",
"position": 4
}
]
}
1.7.API
Elasticsearch提供了Rest風格的API,即http請求接口,而且也提供了各種語言的客戶端API
1.7.1.Rest風格API
文檔地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-4ZWtCz9m-1572182351919)(H:/%E4%B9%90%E4%BC%98/7%E6%9C%881%E5%8F%B7%E6%9B%B4%E6%96%B0/day11-elasticsearch/assets/1526518410240.png)]
1.7.2.客戶端API
Elasticsearch支持的客戶端非常多:https://www.elastic.co/guide/en/elasticsearch/client/index.html
點擊Java Rest Client後,你會發現又有兩個:
Low Level Rest Client是低級別封裝,提供一些基礎功能,但更靈活
High Level Rest Client,是在Low Level Rest Client基礎上進行的高級別封裝,功能更豐富和完善,而且API會變的簡單
1.7.3.如何學習
建議先學習Rest風格API,瞭解發起請求的底層實現,請求體格式等。
2.操作索引
2.1.基本概念
Elasticsearch也是基於Lucene的全文檢索庫,本質也是存儲數據,很多概念與MySQL類似的。
對比關係:
索引(indices)--------------------------------Databases 數據庫
類型(type)-----------------------------Table 數據表
文檔(Document)----------------Row 行
字段(Field)-------------------Columns 列
詳細說明:
概念 | 說明 |
---|---|
索引庫(indices) | indices是index的複數,代表許多的索引, |
類型(type) | 類型是模擬mysql中的table概念,一個索引庫下可以有不同類型的索引,比如商品索引,訂單索引,其數據格式不同。不過這會導致索引庫混亂,因此未來版本中會移除這個概念 |
文檔(document) | 存入索引庫原始的數據。比如每一條商品信息,就是一個文檔 |
字段(field) | 文檔中的屬性 |
映射配置(mappings) | 字段的數據類型、屬性、是否索引、是否存儲等特性 |
是不是與Lucene和solr中的概念類似。
另外,在SolrCloud中,有一些集羣相關的概念,在Elasticsearch也有類似的:
- 索引集(Indices,index的複數):邏輯上的完整索引
- 分片(shard):數據拆分後的各個部分
- 副本(replica):每個分片的複製
要注意的是:Elasticsearch本身就是分佈式的,因此即便你只有一個節點,Elasticsearch默認也會對你的數據進行分片和副本操作,當你向集羣添加新數據時,數據也會在新加入的節點中進行平衡。
2.2.創建索引
2.2.1.語法
Elasticsearch採用Rest風格API,因此其API就是一次http請求,你可以用任何工具發起http請求
創建索引的請求格式:
-
請求方式:PUT
-
請求路徑:/索引庫名
-
請求參數:json格式:
{ "settings": { "number_of_shards": 3, "number_of_replicas": 2 } }
- settings:索引庫的設置
- number_of_shards:分片數量
- number_of_replicas:副本數量
- settings:索引庫的設置
2.2.2.測試
我們先用RestClient來試試
響應:
可以看到索引創建成功了。
2.2.3.使用kibana創建
kibana的控制檯,可以對http請求進行簡化,示例:
相當於是省去了elasticsearch的服務器地址
而且還有語法提示,非常舒服。
2.3.查看索引設置
語法
Get請求可以幫我們查看索引信息,格式:
GET /索引庫名
或者,我們可以使用*來查詢所有索引庫配置:
2.4.刪除索引
刪除索引使用DELETE請求
語法
DELETE /索引庫名
示例
再次查看heima2:
當然,我們也可以用HEAD請求,查看索引是否存在:
2.5.映射配置
索引有了,接下來肯定是添加數據。但是,在添加數據之前必須定義映射。
什麼是映射?
映射是定義文檔的過程,文檔包含哪些字段,這些字段是否保存,是否索引,是否分詞等
只有配置清楚,Elasticsearch纔會幫我們進行索引庫的創建(不一定)
2.5.1.創建映射字段
語法
請求方式依然是PUT
PUT /索引庫名/_mapping/類型名稱
{
"properties": {
"字段名": {
"type": "類型",
"index": true,
"store": true,
"analyzer": "分詞器"
}
}
}
- 類型名稱:就是前面將的type的概念,類似於數據庫中的不同表
字段名:任意填寫 ,可以指定許多屬性,例如: - type:類型,可以是text、long、short、date、integer、object等
- index:是否索引,默認爲true
- store:是否存儲,默認爲false
- analyzer:分詞器,這裏的
ik_max_word
即使用ik分詞器
示例
發起請求:
PUT heima/_mapping/goods
{
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"images": {
"type": "keyword",
"index": "false"
},
"price": {
"type": "float"
}
}
}
響應結果:
{
"acknowledged": true
}
2.5.2.查看映射關係
語法:
GET /索引庫名/_mapping
示例:
GET /heima/_mapping
響應:
{
"heima": {
"mappings": {
"goods": {
"properties": {
"images": {
"type": "keyword",
"index": false
},
"price": {
"type": "float"
},
"title": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
}
2.5.3.字段屬性詳解
2.5.3.1.type
Elasticsearch中支持的數據類型非常豐富:
我們說幾個關鍵的:
-
String類型,又分兩種:
- text:可分詞,不可參與聚合
- keyword:不可分詞,數據會作爲完整字段進行匹配,可以參與聚合
-
Numerical:數值類型,分兩類
- 基本數據類型:long、interger、short、byte、double、float、half_float
- 浮點數的高精度類型:scaled_float
- 需要指定一個精度因子,比如10或100。elasticsearch會把真實值乘以這個因子後存儲,取出時再還原。
-
Date:日期類型
elasticsearch可以對日期格式化爲字符串存儲,但是建議我們存儲爲毫秒值,存儲爲long,節省空間。
2.5.3.2.index
index影響字段的索引情況。
- true:字段會被索引,則可以用來進行搜索。默認值就是true
- false:字段不會被索引,不能用來搜索
index的默認值就是true,也就是說你不進行任何配置,所有字段都會被索引。
但是有些字段是我們不希望被索引的,比如商品的圖片信息,就需要手動設置index爲false。
2.5.3.3.store
是否將數據進行額外存儲。
在學習lucene和solr時,我們知道如果一個字段的store設置爲false,那麼在文檔列表中就不會有這個字段的值,用戶的搜索結果中不會顯示出來。
但是在Elasticsearch中,即便store設置爲false,也可以搜索到結果。
原因是Elasticsearch在創建文檔索引時,會將文檔中的原始數據備份,保存到一個叫做_source
的屬性中。而且我們可以通過過濾_source
來選擇哪些要顯示,哪些不顯示。
而如果設置store爲true,就會在_source
以外額外存儲一份數據,多餘,因此一般我們都會將store設置爲false,事實上,store的默認值就是false。
2.5.3.4.boost
激勵因子,這個與lucene中一樣
其它的不再一一講解,用的不多,大家參考官方文檔:
2.6.新增數據
2.6.1.隨機生成id
通過POST請求,可以向一個已經存在的索引庫中添加數據。
語法:
POST /索引庫名/類型名
{
"key":"value"
}
示例:
POST /heima/goods/
{
"title":"小米手機",
"images":"http://image.leyou.com/12479122.jpg",
"price":2699.00
}
響應:
{
"_index": "heima",
"_type": "goods",
"_id": "r9c1KGMBIhaxtY5rlRKv",
"_version": 1,
"result": "created",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 2
}
通過kibana查看數據:
get _search
{
"query":{
"match_all":{}
}
}
{
"_index": "heima",
"_type": "goods",
"_id": "r9c1KGMBIhaxtY5rlRKv",
"_version": 1,
"_score": 1,
"_source": {
"title": "小米手機",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2699
}
}
_source
:源文檔信息,所有的數據都在裏面。_id
:這條文檔的唯一標示,與文檔自己的id字段沒有關聯
2.6.2.自定義id
如果我們想要自己新增的時候指定id,可以這麼做:
POST /索引庫名/類型/id值
{
...
}
示例:
POST /heima/goods/2
{
"title":"大米手機",
"images":"http://image.leyou.com/12479122.jpg",
"price":2899.00
}
得到的數據:
{
"_index": "heima",
"_type": "goods",
"_id": "2",
"_score": 1,
"_source": {
"title": "大米手機",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2899
}
}
2.6.3.智能判斷
在學習Solr時我們發現,我們在新增數據時,只能使用提前配置好映射屬性的字段,否則就會報錯。
不過在Elasticsearch中並沒有這樣的規定。
事實上Elasticsearch非常智能,你不需要給索引庫設置任何mapping映射,它也可以根據你輸入的數據來判斷類型,動態添加數據映射。
測試一下:
POST /heima/goods/3
{
"title":"超米手機",
"images":"http://image.leyou.com/12479122.jpg",
"price":2899.00,
"stock": 200,
"saleable":true
}
我們額外添加了stock庫存,和saleable是否上架兩個字段。
來看結果:
{
"_index": "heima",
"_type": "goods",
"_id": "3",
"_version": 1,
"_score": 1,
"_source": {
"title": "超米手機",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2899,
"stock": 200,
"saleable": true
}
}
在看下索引庫的映射關係:
{
"heima": {
"mappings": {
"goods": {
"properties": {
"images": {
"type": "keyword",
"index": false
},
"price": {
"type": "float"
},
"saleable": {
"type": "boolean"
},
"stock": {
"type": "long"
},
"title": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
}
stock和saleable都被成功映射了。
如果存儲的是String數據類型,ES無智能判斷,他就會存入兩個字段,例如存入一個name字段,智能行成兩個字段:
name: text類型
name.keyword: keyword類型
2.7.修改數據
把剛纔新增的請求方式改爲PUT,就是修改了。不過修改必須指定id,
- id對應文檔存在,則修改
- id對應文檔不存在,則新增
比如,我們把id爲3的數據進行修改:
PUT /heima/goods/3
{
"title":"超大米手機",
"images":"http://image.leyou.com/12479122.jpg",
"price":3899.00,
"stock": 100,
"saleable":true
}
結果:
{
"took": 17,
"timed_out": false,
"_shards": {
"total": 9,
"successful": 9,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "3",
"_score": 1,
"_source": {
"title": "超大米手機",
"images": "http://image.leyou.com/12479122.jpg",
"price": 3899,
"stock": 100,
"saleable": true
}
}
]
}
}
2.8.刪除數據
刪除使用DELETE請求,同樣,需要根據id進行刪除:
語法
DELETE /索引庫名/類型名/id值
示例:
3.查詢
我們從4塊來講查詢:
- 基本查詢
_source
過濾- 結果過濾
- 高級查詢
- 排序
3.1.基本查詢:
基本語法
GET /索引庫名/_search
{
"query":{
"查詢類型":{
"查詢條件":"查詢條件值"
}
}
}
這裏的query代表一個查詢對象,裏面可以有不同的查詢屬性
- 查詢類型:
- 例如:
match_all
,match
,term
,range
等等
- 例如:
- 查詢條件:查詢條件會根據類型的不同,寫法也有差異,後面詳細講解
3.1.1 查詢所有(match_all)
示例:
GET /heima/_search
{
"query":{
"match_all": {}
}
}
query
:代表查詢對象match_all
:代表查詢所有
結果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "2",
"_score": 1,
"_source": {
"title": "大米手機",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2899
}
},
{
"_index": "heima",
"_type": "goods",
"_id": "r9c1KGMBIhaxtY5rlRKv",
"_score": 1,
"_source": {
"title": "小米手機",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2699
}
}
]
}
}
- took:查詢花費時間,單位是毫秒
- time_out:是否超時
- _shards:分片信息
- hits:搜索結果總覽對象
- total:搜索到的總條數
- max_score:所有結果中文檔得分的最高分
- hits:搜索結果的文檔對象數組,每個元素是一條搜索到的文檔信息
- _index:索引庫
- _type:文檔類型
- _id:文檔id
- _score:文檔得分
- _source:文檔的源數據
3.1.2 匹配查詢(match)
我們先加入一條數據,便於測試:
PUT /heima/goods/3
{
"title":"小米電視4A",
"images":"http://image.leyou.com/12479122.jpg",
"price":3899.00
}
現在,索引庫中有2部手機,1臺電視:
- or關係
match
類型查詢,會把查詢條件進行分詞,然後進行查詢,多個詞條之間是or的關係
GET /heima/_search
{
"query":{
"match":{
"title":"小米電視"
}
}
}
結果:
"hits": {
"total": 2,
"max_score": 0.6931472,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "tmUBomQB_mwm6wH_EC1-",
"_score": 0.6931472,
"_source": {
"title": "小米手機",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2699
}
},
{
"_index": "heima",
"_type": "goods",
"_id": "3",
"_score": 0.5753642,
"_source": {
"title": "小米電視4A",
"images": "http://image.leyou.com/12479122.jpg",
"price": 3899
}
}
]
}
在上面的案例中,不僅會查詢到電視,而且與小米相關的都會查詢到,多個詞之間是or
的關係。
- and關係
某些情況下,我們需要更精確查找,我們希望這個關係變成and
,可以這樣做:
GET /heima/_search
{
"query":{
"match": {
"title": {
"query": "小米電視",
"operator": "and"
}
}
}
}
結果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "3",
"_score": 0.5753642,
"_source": {
"title": "小米電視4A",
"images": "http://image.leyou.com/12479122.jpg",
"price": 3899
}
}
]
}
}
本例中,只有同時包含小米
和電視
的詞條纔會被搜索到。
- or和and之間?
在 or
與 and
間二選一有點過於非黑即白。 如果用戶給定的條件分詞後有 5 個查詢詞項,想查找只包含其中 4 個詞的文檔,該如何處理?將 operator 操作符參數設置成 and
只會將此文檔排除。
有時候這正是我們期望的,但在全文搜索的大多數應用場景下,我們既想包含那些可能相關的文檔,同時又排除那些不太相關的。換句話說,我們想要處於中間某種結果。
match
查詢支持 minimum_should_match
最小匹配參數, 這讓我們可以指定必須匹配的詞項數用來表示一個文檔是否相關。我們可以將其設置爲某個具體數字,更常用的做法是將其設置爲一個百分數
,因爲我們無法控制用戶搜索時輸入的單詞數量:
GET /heima/_search
{
"query":{
"match":{
"title":{
"query":"小米曲面電視",
"minimum_should_match": "75%"
}
}
}
}
本例中,搜索語句可以分爲3個詞,如果使用and關係,需要同時滿足3個詞纔會被搜索到。這裏我們採用最小品牌數:75%,那麼也就是說只要匹配到總詞條數量的75%即可,這裏3*75% 約等於2。所以只要包含2個詞條就算滿足條件了。
結果:
3.1.3 多字段查詢(multi_match)
multi_match
與match
類似,不同的是它可以在多個字段中查詢
GET /heima/_search
{
"query":{
"multi_match": {
"query": "小米",
"fields": [ "title", "subTitle" ]
}
}
}
本例中,我們會在title字段和subtitle字段中查詢小米
這個詞
3.1.4 詞條匹配(term)
term
查詢被用於精確值 匹配,這些精確值可能是數字、時間、布爾或者那些未分詞的字符串
GET /heima/_search
{
"query":{
"term":{
"price":2699.00
}
}
}
結果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "r9c1KGMBIhaxtY5rlRKv",
"_score": 1,
"_source": {
"title": "小米手機",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2699
}
}
]
}
}
3.1.5 多詞條精確匹配(terms)
terms
查詢和 term 查詢一樣,但它允許你指定多值進行匹配。如果這個字段包含了指定值中的任何一個值,那麼這個文檔滿足條件:
GET /heima/_search
{
"query":{
"terms":{
"price":[2699.00,2899.00,3899.00]
}
}
}
結果:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "2",
"_score": 1,
"_source": {
"title": "大米手機",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2899
}
},
{
"_index": "heima",
"_type": "goods",
"_id": "r9c1KGMBIhaxtY5rlRKv",
"_score": 1,
"_source": {
"title": "小米手機",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2699
}
},
{
"_index": "heima",
"_type": "goods",
"_id": "3",
"_score": 1,
"_source": {
"title": "小米電視4A",
"images": "http://image.leyou.com/12479122.jpg",
"price": 3899
}
}
]
}
}
3.2.結果過濾
默認情況下,elasticsearch在搜索的結果中,會把文檔中保存在_source
的所有字段都返回。
如果我們只想獲取其中的部分字段,我們可以添加_source
的過濾
3.2.1.直接指定字段
示例:
GET /heima/_search
{
"_source": ["title","price"],
"query": {
"term": {
"price": 2699
}
}
}
返回的結果:
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "r9c1KGMBIhaxtY5rlRKv",
"_score": 1,
"_source": {
"price": 2699,
"title": "小米手機"
}
}
]
}
}
3.2.2.指定includes和excludes
我們也可以通過:
- includes:來指定想要顯示的字段
- excludes:來指定不想要顯示的字段
二者都是可選的。
示例:
GET /heima/_search
{
"_source": {
"includes":["title","price"]
},
"query": {
"term": {
"price": 2699
}
}
}
與下面的結果將是一樣的:
GET /heima/_search
{
"_source": {
"excludes": ["images"]
},
"query": {
"term": {
"price": 2699
}
}
}
3.3 高級查詢
3.3.1 布爾組合(bool)
bool
把各種其它查詢通過must
(與)、must_not
(非)、should
(或)的方式進行組合
GET /heima/_search
{
"query":{
"bool":{
"must": { "match": { "title": "大米" }},
"must_not": { "match": { "title": "電視" }},
"should": { "match": { "title": "手機" }}
}
}
}
結果:
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "2",
"_score": 0.5753642,
"_source": {
"title": "大米手機",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2899
}
}
]
}
}
3.3.2 範圍查詢(range)
range
查詢找出那些落在指定區間內的數字或者時間
GET /heima/_search
{
"query":{
"range": {
"price": {
"gte": 1000.0,
"lt": 2800.00
}
}
}
}
range
查詢允許以下字符:
操作符 | 說明 |
---|---|
gt | 大於 |
gte | 大於等於 |
lt | 小於 |
lte | 小於等於 |
3.3.3 模糊查詢(fuzzy)
我們新增一個商品:
POST /heima/goods/4
{
"title":"apple手機",
"images":"http://image.leyou.com/12479122.jpg",
"price":6899.00
}
fuzzy
查詢是 term
查詢的模糊等價。它允許用戶搜索詞條與實際詞條的拼寫出現偏差,但是偏差的編輯距離不得超過2:
GET /heima/_search
{
"query": {
"fuzzy": {
"title": "appla"
}
}
}
上面的查詢,也能查詢到apple手機
我們可以通過fuzziness
來指定允許的編輯距離:
GET /heima/_search
{
"query": {
"fuzzy": {
"title": {
"value":"appla",
"fuzziness":1
}
}
}
}
3.4 過濾(filter)
條件查詢中進行過濾
所有的查詢都會影響到文檔的評分及排名。如果我們需要在查詢結果中進行過濾,並且不希望過濾條件影響評分,那麼就不要把過濾條件作爲查詢條件來用。而是使用filter
方式:
GET /heima/_search
{
"query":{
"bool":{
"must":{ "match": { "title": "小米手機" }},
"filter":{
"range":{"price":{"gt":2000.00,"lt":3800.00}}
}
}
}
}
注意:filter
中還可以再次進行bool
組合條件過濾。
無查詢條件,直接過濾
如果一次查詢只有過濾,沒有查詢條件,不希望進行評分,我們可以使用constant_score
取代只有 filter 語句的 bool 查詢。在性能上是完全相同的,但對於提高查詢簡潔性和清晰度有很大幫助。
GET /heima/_search
{
"query":{
"constant_score": {
"filter": {
"range":{"price":{"gt":2000.00,"lt":3000.00}}
}
}
}
3.5 排序
3.4.1 單字段排序
sort
可以讓我們按照不同的字段進行排序,並且通過order
指定排序的方式
GET /heima/_search
{
"query": {
"match": {
"title": "小米手機"
}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
3.4.2 多字段排序
假定我們想要結合使用 price和 _score(得分) 進行查詢,並且匹配的結果首先按照價格排序,然後按照相關性得分排序:
GET /goods/_search
{
"query":{
"bool":{
"must":{ "match": { "title": "小米手機" }},
"filter":{
"range":{"price":{"gt":200000,"lt":300000}}
}
}
},
"sort": [
{ "price": { "order": "desc" }},
{ "_score": { "order": "desc" }}
]
}
4. 聚合aggregations
聚合可以讓我們極其方便的實現對數據的統計、分析。例如:
- 什麼品牌的手機最受歡迎?
- 這些手機的平均價格、最高價格、最低價格?
- 這些手機每月的銷售情況如何?
實現這些統計功能的比數據庫的sql要方便的多,而且查詢速度非常快,可以實現實時搜索效果。
4.1 基本概念
Elasticsearch中的聚合,包含多種類型,最常用的兩種,一個叫桶
,一個叫度量
:
桶(bucket)
桶的作用,是按照某種方式對數據進行分組,每一組數據在ES中稱爲一個桶
,例如我們根據國籍對人劃分,可以得到中國桶
、英國桶
,日本桶
……或者我們按照年齡段對人進行劃分:010,1020,2030,3040等。
Elasticsearch中提供的劃分桶的方式有很多:
- Date Histogram Aggregation:根據日期階梯分組,例如給定階梯爲周,會自動每週分爲一組
- Histogram Aggregation:根據數值階梯分組,與日期類似
- Terms Aggregation:根據詞條內容分組,詞條內容完全匹配的爲一組
- Range Aggregation:數值和日期的範圍分組,指定開始和結束,然後按段分組
- ……
綜上所述,我們發現bucket aggregations 只負責對數據進行分組,並不進行計算,因此往往bucket中往往會嵌套另一種聚合:metrics aggregations即度量
度量(metrics)
分組完成以後,我們一般會對組中的數據進行聚合運算,例如求平均值、最大、最小、求和等,這些在ES中稱爲度量
比較常用的一些度量聚合方式:
- Avg Aggregation:求平均值
- Max Aggregation:求最大值
- Min Aggregation:求最小值
- Percentiles Aggregation:求百分比
- Stats Aggregation:同時返回avg、max、min、sum、count等
- Sum Aggregation:求和
- Top hits Aggregation:求前幾
- Value Count Aggregation:求總數
- ……
爲了測試聚合,我們先批量導入一些數據
創建索引:
PUT /cars
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"transactions": {
"properties": {
"color": {
"type": "keyword"
},
"make": {
"type": "keyword"
}
}
}
}
}
注意:在ES中,需要進行聚合、排序、過濾的字段其處理方式比較特殊,因此不能被分詞。這裏我們將color和make這兩個文字類型的字段設置爲keyword類型,這個類型不會被分詞,將來就可以參與聚合
導入數據
POST /cars/transactions/_bulk
{ "index": {}}
{ "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
{ "index": {}}
{ "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
{ "index": {}}
{ "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
{ "index": {}}
{ "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }
4.2 聚合爲桶
首先,我們按照 汽車的顏色color
來劃分桶
GET /cars/_search
{
"size" : 0,
"aggs" : {
"popular_colors" : {
"terms" : {
"field" : "color"
}
}
}
}
- size: 查詢條數,這裏設置爲0,因爲我們不關心搜索到的數據,只關心聚合結果,提高效率
- aggs:聲明這是一個聚合查詢,是aggregations的縮寫
- popular_colors:給這次聚合起一個名字,任意。
- terms:劃分桶的方式,這裏是根據詞條劃分
- field:劃分桶的字段
- terms:劃分桶的方式,這裏是根據詞條劃分
- popular_colors:給這次聚合起一個名字,任意。
結果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 8,
"max_score": 0,
"hits": []
},
"aggregations": {
"popular_colors": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "red",
"doc_count": 4
},
{
"key": "blue",
"doc_count": 2
},
{
"key": "green",
"doc_count": 2
}
]
}
}
}
- hits:查詢結果爲空,因爲我們設置了size爲0
- aggregations:聚合的結果
- popular_colors:我們定義的聚合名稱
- buckets:查找到的桶,每個不同的color字段值都會形成一個桶
- key:這個桶對應的color字段的值
- doc_count:這個桶中的文檔數量
通過聚合的結果我們發現,目前紅色的小車比較暢銷!
4.3 桶內度量
前面的例子告訴我們每個桶裏面的文檔數量,這很有用。 但通常,我們的應用需要提供更復雜的文檔度量。 例如,每種顏色汽車的平均價格是多少?
因此,我們需要告訴Elasticsearch使用哪個字段
,使用何種度量方式
進行運算,這些信息要嵌套在桶
內,度量
的運算會基於桶
內的文檔進行
現在,我們爲剛剛的聚合結果添加 求價格平均值的度量:
GET /cars/_search
{
"size" : 0,
"aggs" : {
"popular_colors" : {
"terms" : {
"field" : "color"
},
"aggs":{
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
- aggs:我們在上一個aggs(popular_colors)中添加新的aggs。可見
度量
也是一個聚合,度量是在桶內的聚合 - avg_price:聚合的名稱
- avg:度量的類型,這裏是求平均值
- field:度量運算的字段
結果:
...
"aggregations": {
"popular_colors": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "red",
"doc_count": 4,
"avg_price": {
"value": 32500
}
},
{
"key": "blue",
"doc_count": 2,
"avg_price": {
"value": 20000
}
},
{
"key": "green",
"doc_count": 2,
"avg_price": {
"value": 21000
}
}
]
}
}
...
可以看到每個桶中都有自己的avg_price
字段,這是度量聚合的結果
4.4 桶內嵌套桶
剛剛的案例中,我們在桶內嵌套度量運算。事實上桶不僅可以嵌套運算, 還可以再嵌套其它桶。也就是說在每個分組中,再分更多組。
比如:我們想統計每種顏色的汽車中,分別屬於哪個製造商,按照make
字段再進行分桶
GET /cars/_search
{
"size" : 0,
"aggs" : {
"popular_colors" : {
"terms" : {
"field" : "color"
},
"aggs":{
"avg_price": {
"avg": {
"field": "price"
}
},
"maker":{
"terms":{
"field":"make"
}
}
}
}
}
}
- 原來的color桶和avg計算我們不變
- maker:在嵌套的aggs下新添一個桶,叫做maker
- terms:桶的劃分類型依然是詞條
- filed:這裏根據make字段進行劃分
部分結果:
...
{"aggregations": {
"popular_colors": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "red",
"doc_count": 4,
"maker": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "honda",
"doc_count": 3
},
{
"key": "bmw",
"doc_count": 1
}
]
},
"avg_price": {
"value": 32500
}
},
{
"key": "blue",
"doc_count": 2,
"maker": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "ford",
"doc_count": 1
},
{
"key": "toyota",
"doc_count": 1
}
]
},
"avg_price": {
"value": 20000
}
},
{
"key": "green",
"doc_count": 2,
"maker": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "ford",
"doc_count": 1
},
{
"key": "toyota",
"doc_count": 1
}
]
},
"avg_price": {
"value": 21000
}
}
]
}
}
}
...
- 我們可以看到,新的聚合
maker
被嵌套在原來每一個color
的桶中。 - 每個顏色下面都根據
make
字段進行了分組 - 我們能讀取到的信息:
- 紅色車共有4輛
- 紅色車的平均售價是 $32,500 美元。
- 其中3輛是 Honda 本田製造,1輛是 BMW 寶馬製造。
4.5.劃分桶的其它方式
前面講了,劃分桶的方式有很多,例如:
- Date Histogram Aggregation:根據日期階梯分組,例如給定階梯爲周,會自動每週分爲一組
- Histogram Aggregation:根據數值階梯分組,與日期類似
- Terms Aggregation:根據詞條內容分組,詞條內容完全匹配的爲一組
- Range Aggregation:數值和日期的範圍分組,指定開始和結束,然後按段分組
剛剛的案例中,我們採用的是Terms Aggregation,即根據詞條劃分桶。
接下來,我們再學習幾個比較實用的:
4.5.1.階梯分桶Histogram
原理:
histogram是把數值類型的字段,按照一定的階梯大小進行分組。你需要指定一個階梯值(interval)來劃分階梯大小。
舉例:
比如你有價格字段,如果你設定interval的值爲200,那麼階梯就會是這樣的:
0,200,400,600,…
上面列出的是每個階梯的key,也是區間的啓點。
如果一件商品的價格是450,會落入哪個階梯區間呢?計算公式如下:
bucket_key = Math.floor((value - offset) / interval) * interval + offset
value:就是當前數據的值,本例中是450
offset:起始偏移量,默認爲0
interval:階梯間隔,比如200
因此你得到的key = Math.floor((450 - 0) / 200) * 200 + 0 = 400
操作一下:
比如,我們對汽車的價格進行分組,指定間隔interval爲5000:
GET /cars/_search
{
"size":0,
"aggs":{
"price":{
"histogram": {
"field": "price",
"interval": 5000
}
}
}
}
結果:
{
"took": 21,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 8,
"max_score": 0,
"hits": []
},
"aggregations": {
"price": {
"buckets": [
{
"key": 10000,
"doc_count": 2
},
{
"key": 15000,
"doc_count": 1
},
{
"key": 20000,
"doc_count": 2
},
{
"key": 25000,
"doc_count": 1
},
{
"key": 30000,
"doc_count": 1
},
{
"key": 35000,
"doc_count": 0
},
{
"key": 40000,
"doc_count": 0
},
{
"key": 45000,
"doc_count": 0
},
{
"key": 50000,
"doc_count": 0
},
{
"key": 55000,
"doc_count": 0
},
{
"key": 60000,
"doc_count": 0
},
{
"key": 65000,
"doc_count": 0
},
{
"key": 70000,
"doc_count": 0
},
{
"key": 75000,
"doc_count": 0
},
{
"key": 80000,
"doc_count": 1
}
]
}
}
}
你會發現,中間有大量的文檔數量爲0 的桶,看起來很醜。
我們可以增加一個參數min_doc_count爲1,來約束最少文檔數量爲1,這樣文檔數量爲0的桶會被過濾
示例:
GET /cars/_search
{
"size":0,
"aggs":{
"price":{
"histogram": {
"field": "price",
"interval": 5000,
"min_doc_count": 1
}
}
}
}
結果:
{
"took": 15,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 8,
"max_score": 0,
"hits": []
},
"aggregations": {
"price": {
"buckets": [
{
"key": 10000,
"doc_count": 2
},
{
"key": 15000,
"doc_count": 1
},
{
"key": 20000,
"doc_count": 2
},
{
"key": 25000,
"doc_count": 1
},
{
"key": 30000,
"doc_count": 1
},
{
"key": 80000,
"doc_count": 1
}
]
}
}
}
完美,!
如果你用kibana將結果變爲柱形圖,會更好看:
4.5.2.範圍分桶range
範圍分桶與階梯分桶類似,也是把數字按照階段進行分組,只不過range方式需要你自己指定每一組的起始和結束大小。
5.Spring Data Elasticsearch
Elasticsearch提供的Java客戶端有一些不太方便的地方:
- 很多地方需要拼接Json字符串,在java中拼接字符串有多恐怖你應該懂的
- 需要自己把對象序列化爲json存儲
- 查詢到結果也需要自己反序列化爲對象
因此,我們這裏就不講解原生的Elasticsearch客戶端API了。
而是學習Spring提供的套件:Spring Data Elasticsearch。
5.1.簡介
Spring Data Elasticsearch是Spring Data項目下的一個子模塊。
查看 Spring Data的官網:http://projects.spring.io/spring-data/
Spring Data的使命是爲數據訪問提供熟悉且一致的基於Spring的編程模型,同時仍保留底層數據存儲的特殊特性。
它使得使用數據訪問技術,關係數據庫和非關係數據庫,map-reduce框架和基於雲的數據服務變得容易。這是一個總括項目,其中包含許多特定於給定數據庫的子項目。這些令人興奮的技術項目背後,是由許多公司和開發人員合作開發的。
Spring Data 的使命是給各種數據訪問提供統一的編程接口,不管是關係型數據庫(如MySQL),還是非關係數據庫(如Redis),或者類似Elasticsearch這樣的索引數據庫。從而簡化開發人員的代碼,提高開發效率。
包含很多不同數據操作的模塊:
Spring Data Elasticsearch的頁面:https://projects.spring.io/spring-data-elasticsearch/
特徵:
- 支持Spring的基於
@Configuration
的java配置方式,或者XML配置方式 - 提供了用於操作ES的便捷工具類**
ElasticsearchTemplate
**。包括實現文檔到POJO之間的自動智能映射。 - 利用Spring的數據轉換服務實現的功能豐富的對象映射
- 基於註解的元數據映射方式,而且可擴展以支持更多不同的數據格式
- 根據持久層接口自動生成對應實現方法,無需人工編寫基本操作代碼(類似mybatis,根據接口自動得到實現)。當然,也支持人工定製查詢
5.2.創建Demo工程
我們新建一個demo,學習Elasticsearch
pom依賴:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.leyou.demo</groupId>
<artifactId>elasticsearch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>elasticsearch</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml文件配置:
spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 192.168.56.101:9300
5.3.實體類及註解
首先我們準備好實體類:
public class Item {
Long id;
String title; //標題
String category;// 分類
String brand; // 品牌
Double price; // 價格
String images; // 圖片地址
}
映射
Spring Data通過註解來聲明字段的映射屬性,有下面的三個註解:
@Document
作用在類,標記實體類爲文檔對象,一般有兩個屬性- indexName:對應索引庫名稱
- type:對應在索引庫中的類型
- shards:分片數量,默認5
- replicas:副本數量,默認1
@Id
作用在成員變量,標記一個字段作爲id主鍵@Field
作用在成員變量,標記爲文檔的字段,並指定字段映射屬性:- type:字段類型,取值是枚舉:FieldType
- index:是否索引,布爾類型,默認是true
- store:是否存儲,布爾類型,默認是false
- analyzer:分詞器名稱
示例:
@Document(indexName = "item",type = "docs", shards = 1, replicas = 0)
public class Item {
@Id
private Long id;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String title; //標題
@Field(type = FieldType.Keyword)
private String category;// 分類
@Field(type = FieldType.Keyword)
private String brand; // 品牌
@Field(type = FieldType.Double)
private Double price; // 價格
@Field(index = false, type = FieldType.Keyword)
private String images; // 圖片地址
}
5.4.Template索引操作
5.4.1.創建索引和映射
創建索引
ElasticsearchTemplate中提供了創建索引的API:
可以根據類的信息自動生成,也可以手動指定indexName和Settings
映射
映射相關的API:
可以根據類的字節碼信息(註解配置)來生成映射,或者手動編寫映射
我們這裏採用類的字節碼信息創建索引並映射:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ItcastElasticsearchApplication.class)
public class IndexTest {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Test
public void testCreate(){
// 創建索引,會根據Item類的@Document註解信息來創建
elasticsearchTemplate.createIndex(Item.class);
// 配置映射,會根據Item類中的id、Field等字段來自動完成映射
elasticsearchTemplate.putMapping(Item.class);
}
}
結果:
GET /item
{
"item": {
"aliases": {},
"mappings": {
"docs": {
"properties": {
"brand": {
"type": "keyword"
},
"category": {
"type": "keyword"
},
"images": {
"type": "keyword",
"index": false
},
"price": {
"type": "double"
},
"title": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
},
"settings": {
"index": {
"refresh_interval": "1s",
"number_of_shards": "1",
"provided_name": "item",
"creation_date": "1525405022589",
"store": {
"type": "fs"
},
"number_of_replicas": "0",
"uuid": "4sE9SAw3Sqq1aAPz5F6OEg",
"version": {
"created": "6020499"
}
}
}
}
}
5.3.2.刪除索引
刪除索引的API:
可以根據類名或索引名刪除。
示例:
@Test
public void deleteIndex() {
esTemplate.deleteIndex("heima");
}
結果:
5.4.Repository文檔操作
Spring Data 的強大之處,就在於你不用寫任何DAO處理,自動根據方法名或類的信息進行CRUD操作。只要你定義一個接口,然後繼承Repository提供的一些子接口,就能具備各種基本的CRUD功能。
我們只需要定義接口,然後繼承它就OK了。
public interface ItemRepository extends ElasticsearchRepository<Item,Long> {
}
來看下Repository的繼承關係:
我們看到有一個ElasticsearchRepository接口:
5.4.1.新增文檔
@Autowired
private ItemRepository itemRepository;
@Test
public void index() {
Item item = new Item(1L, "小米手機7", " 手機",
"小米", 3499.00, "http://image.leyou.com/13123.jpg");
itemRepository.save(item);
}
去頁面查詢看看:
GET /item/_search
結果:
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "item",
"_type": "docs",
"_id": "1",
"_score": 1,
"_source": {
"id": 1,
"title": "小米手機7",
"category": " 手機",
"brand": "小米",
"price": 3499,
"images": "http://image.leyou.com/13123.jpg"
}
}
]
}
}
5.4.2.批量新增
代碼:
@Test
public void indexList() {
List<Item> list = new ArrayList<>();
list.add(new Item(2L, "堅果手機R1", " 手機", "錘子", 3699.00, "http://image.leyou.com/123.jpg"));
list.add(new Item(3L, "華爲META10", " 手機", "華爲", 4499.00, "http://image.leyou.com/3.jpg"));
// 接收對象集合,實現批量新增
itemRepository.saveAll(list);
}
再次去頁面查詢:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "item",
"_type": "docs",
"_id": "2",
"_score": 1,
"_source": {
"id": 2,
"title": "堅果手機R1",
"category": " 手機",
"brand": "錘子",
"price": 3699,
"images": "http://image.leyou.com/13123.jpg"
}
},
{
"_index": "item",
"_type": "docs",
"_id": "3",
"_score": 1,
"_source": {
"id": 3,
"title": "華爲META10",
"category": " 手機",
"brand": "華爲",
"price": 4499,
"images": "http://image.leyou.com/13123.jpg"
}
},
{
"_index": "item",
"_type": "docs",
"_id": "1",
"_score": 1,
"_source": {
"id": 1,
"title": "小米手機7",
"category": " 手機",
"brand": "小米",
"price": 3499,
"images": "http://image.leyou.com/13123.jpg"
}
}
]
}
}
5.4.3.修改文檔
修改和新增是同一個接口,區分的依據就是id,這一點跟我們在頁面發起PUT請求是類似的。
5.4.4.基本查詢
ElasticsearchRepository提供了一些基本的查詢方法:
我們來試試查詢所有:
@Test
public void testFind(){
// 查詢全部,並安裝價格降序排序
Iterable<Item> items = this.itemRepository.findAll(Sort.by(Sort.Direction.DESC, "price"));
items.forEach(item-> System.out.println(item));
}
結果:
5.4.5.自定義方法
Spring Data 的另一個強大功能,是根據方法名稱自動實現功能。
比如:你的方法名叫做:findByTitle,那麼它就知道你是根據title查詢,然後自動幫你完成,無需寫實現類。
當然,方法名稱要符合一定的約定:
Keyword | Sample | Elasticsearch Query String |
---|---|---|
And |
findByNameAndPrice |
{"bool" : {"must" : [ {"field" : {"name" : "?"}}, {"field" : {"price" : "?"}} ]}} |
Or |
findByNameOrPrice |
{"bool" : {"should" : [ {"field" : {"name" : "?"}}, {"field" : {"price" : "?"}} ]}} |
Is |
findByName |
{"bool" : {"must" : {"field" : {"name" : "?"}}}} |
Not |
findByNameNot |
{"bool" : {"must_not" : {"field" : {"name" : "?"}}}} |
Between |
findByPriceBetween |
{"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : ?,"include_lower" : true,"include_upper" : true}}}}} |
LessThanEqual |
findByPriceLessThan |
{"bool" : {"must" : {"range" : {"price" : {"from" : null,"to" : ?,"include_lower" : true,"include_upper" : true}}}}} |
GreaterThanEqual |
findByPriceGreaterThan |
{"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : null,"include_lower" : true,"include_upper" : true}}}}} |
Before |
findByPriceBefore |
{"bool" : {"must" : {"range" : {"price" : {"from" : null,"to" : ?,"include_lower" : true,"include_upper" : true}}}}} |
After |
findByPriceAfter |
{"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : null,"include_lower" : true,"include_upper" : true}}}}} |
Like |
findByNameLike |
{"bool" : {"must" : {"field" : {"name" : {"query" : "?*","analyze_wildcard" : true}}}}} |
StartingWith |
findByNameStartingWith |
{"bool" : {"must" : {"field" : {"name" : {"query" : "?*","analyze_wildcard" : true}}}}} |
EndingWith |
findByNameEndingWith |
{"bool" : {"must" : {"field" : {"name" : {"query" : "*?","analyze_wildcard" : true}}}}} |
Contains/Containing |
findByNameContaining |
{"bool" : {"must" : {"field" : {"name" : {"query" : "**?**","analyze_wildcard" : true}}}}} |
In |
findByNameIn(Collection<String>names) |
{"bool" : {"must" : {"bool" : {"should" : [ {"field" : {"name" : "?"}}, {"field" : {"name" : "?"}} ]}}}} |
NotIn |
findByNameNotIn(Collection<String>names) |
{"bool" : {"must_not" : {"bool" : {"should" : {"field" : {"name" : "?"}}}}}} |
Near |
findByStoreNear |
Not Supported Yet ! |
True |
findByAvailableTrue |
{"bool" : {"must" : {"field" : {"available" : true}}}} |
False |
findByAvailableFalse |
{"bool" : {"must" : {"field" : {"available" : false}}}} |
OrderBy |
findByAvailableTrueOrderByNameDesc |
{"sort" : [{ "name" : {"order" : "desc"} }],"bool" : {"must" : {"field" : {"available" : true}}}} |
例如,我們來按照價格區間查詢,定義這樣的一個方法:
public interface ItemRepository extends ElasticsearchRepository<Item,Long> {
/**
* 根據價格區間查詢
* @param price1
* @param price2
* @return
*/
List<Item> findByPriceBetween(double price1, double price2);
}
然後添加一些測試數據:
@Test
public void indexList() {
List<Item> list = new ArrayList<>();
list.add(new Item(1L, "小米手機7", "手機", "小米", 3299.00, "http://image.leyou.com/13123.jpg"));
list.add(new Item(2L, "堅果手機R1", "手機", "錘子", 3699.00, "http://image.leyou.com/13123.jpg"));
list.add(new Item(3L, "華爲META10", "手機", "華爲", 4499.00, "http://image.leyou.com/13123.jpg"));
list.add(new Item(4L, "小米Mix2S", "手機", "小米", 4299.00, "http://image.leyou.com/13123.jpg"));
list.add(new Item(5L, "榮耀V10", "手機", "華爲", 2799.00, "http://image.leyou.com/13123.jpg"));
// 接收對象集合,實現批量新增
itemRepository.saveAll(list);
}
不需要寫實現類,然後我們直接去運行:
@Test
public void queryByPriceBetween(){
List<Item> list = this.itemRepository.findByPriceBetween(2000.00, 3500.00);
for (Item item : list) {
System.out.println("item = " + item);
}
}
結果:
雖然基本查詢和自定義方法已經很強大了,但是如果是複雜查詢(模糊、通配符、詞條查詢等)就顯得力不從心了。此時,我們只能使用原生查詢。
5.5.高級查詢
5.5.1.基本查詢
先看看基本玩法
@Test
public void testQuery(){
// 詞條查詢
MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("title", "小米");
// 執行查詢
Iterable<Item> items = this.itemRepository.search(queryBuilder);
items.forEach(System.out::println);
}
Repository的search方法需要QueryBuilder參數,elasticSearch爲我們提供了一個對象QueryBuilders:
QueryBuilders提供了大量的靜態方法,用於生成各種不同類型的查詢對象,例如:詞條、模糊、通配符等QueryBuilder對象。
結果:
elasticsearch提供很多可用的查詢方式,但是不夠靈活。如果想玩過濾或者聚合查詢等就很難了。
5.5.2.自定義查詢
先來看最基本的match query:
@Test
public void testNativeQuery(){
// 構建查詢條件
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
// 添加基本的分詞查詢
queryBuilder.withQuery(QueryBuilders.matchQuery("title", "小米"));
// 執行搜索,獲取結果
Page<Item> items = this.itemRepository.search(queryBuilder.build());
// 打印總條數
System.out.println(items.getTotalElements());
// 打印總頁數
System.out.println(items.getTotalPages());
items.forEach(System.out::println);
}
NativeSearchQueryBuilder:Spring提供的一個查詢條件構建器,幫助構建json格式的請求體
Page<item>
:默認是分頁查詢,因此返回的是一個分頁的結果對象,包含屬性:
- totalElements:總條數
- totalPages:總頁數
- Iterator:迭代器,本身實現了Iterator接口,因此可直接迭代得到當前頁的數據
- 其它屬性:
5.5.4.分頁查詢
利用NativeSearchQueryBuilder
可以方便的實現分頁:
@Test
public void testNativeQuery(){
// 構建查詢條件
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
// 添加基本的分詞查詢
queryBuilder.withQuery(QueryBuilders.termQuery("category", "手機"));
// 初始化分頁參數
int page = 0;
int size = 3;
// 設置分頁參數
queryBuilder.withPageable(PageRequest.of(page, size));
// 執行搜索,獲取結果
Page<Item> items = this.itemRepository.search(queryBuilder.build());
// 打印總條數
System.out.println(items.getTotalElements());
// 打印總頁數
System.out.println(items.getTotalPages());
// 每頁大小
System.out.println(items.getSize());
// 當前頁
System.out.println(items.getNumber());
items.forEach(System.out::println);
}
結果:
[
可以發現,Elasticsearch中的分頁是從第0頁開始。
5.5.5.排序
排序也通用通過NativeSearchQueryBuilder
完成:
@Test
public void testSort(){
// 構建查詢條件
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
// 添加基本的分詞查詢
queryBuilder.withQuery(QueryBuilders.termQuery("category", "手機"));
// 排序
queryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.DESC));
// 執行搜索,獲取結果
Page<Item> items = this.itemRepository.search(queryBuilder.build());
// 打印總條數
System.out.println(items.getTotalElements());
items.forEach(System.out::println);
}
結果:
5.6.聚合
5.6.1.聚合爲桶
桶就是分組,比如這裏我們按照品牌brand進行分組:
@Test
public void testAgg(){
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
// 不查詢任何結果
queryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{""}, null));
// 1、添加一個新的聚合,聚合類型爲terms,聚合名稱爲brands,聚合字段爲brand
queryBuilder.addAggregation(
AggregationBuilders.terms("brands").field("brand"));
// 2、查詢,需要把結果強轉爲AggregatedPage類型
AggregatedPage<Item> aggPage = (AggregatedPage<Item>) this.itemRepository.search(queryBuilder.build());
// 3、解析
// 3.1、從結果中取出名爲brands的那個聚合,
// 因爲是利用String類型字段來進行的term聚合,所以結果要強轉爲StringTerm類型
StringTerms agg = (StringTerms) aggPage.getAggregation("brands");
// 3.2、獲取桶
List<StringTerms.Bucket> buckets = agg.getBuckets();
// 3.3、遍歷
for (StringTerms.Bucket bucket : buckets) {
// 3.4、獲取桶中的key,即品牌名稱
System.out.println(bucket.getKeyAsString());
// 3.5、獲取桶中的文檔數量
System.out.println(bucket.getDocCount());
}
}
顯示的結果:
關鍵API:
-
AggregationBuilders
:聚合的構建工廠類。所有聚合都由這個類來構建,看看他的靜態方法:
-
AggregatedPage
:聚合查詢的結果類。它是Page<T>
的子接口:
AggregatedPage
在Page
功能的基礎上,拓展了與聚合相關的功能,它其實就是對聚合結果的一種封裝,大家可以對照聚合結果的JSON結構來看。
而返回的結果都是Aggregation類型對象,不過根據字段類型不同,又有不同的子類表示
我們看下頁面的查詢的JSON結果與Java類的對照關係:
5.6.2.嵌套聚合,求平均值
代碼:
@Test
public void testSubAgg(){
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
// 不查詢任何結果
queryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{""}, null));
// 1、添加一個新的聚合,聚合類型爲terms,聚合名稱爲brands,聚合字段爲brand
queryBuilder.addAggregation(
AggregationBuilders.terms("brands").field("brand")
.subAggregation(AggregationBuilders.avg("priceAvg").field("price")) // 在品牌聚合桶內進行嵌套聚合,求平均值
);
// 2、查詢,需要把結果強轉爲AggregatedPage類型
AggregatedPage<Item> aggPage = (AggregatedPage<Item>) this.itemRepository.search(queryBuilder.build());
// 3、解析
// 3.1、從結果中取出名爲brands的那個聚合,
// 因爲是利用String類型字段來進行的term聚合,所以結果要強轉爲StringTerm類型
StringTerms agg = (StringTerms) aggPage.getAggregation("brands");
// 3.2、獲取桶
List<StringTerms.Bucket> buckets = agg.getBuckets();
// 3.3、遍歷
for (StringTerms.Bucket bucket : buckets) {
// 3.4、獲取桶中的key,即品牌名稱 3.5、獲取桶中的文檔數量
System.out.println(bucket.getKeyAsString() + ",共" + bucket.getDocCount() + "臺");
// 3.6.獲取子聚合結果:
InternalAvg avg = (InternalAvg) bucket.getAggregations().asMap().get("priceAvg");
System.out.println("平均售價:" + avg.getValue());
}
}
結果: