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都被成功映射了。
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" }} ] }