本章節提供了一個簡單示例,詳情如下方場景描述所示。您可以參考此場景示例數據,使用云搜索服務的Elasticsearch搜索引擎搜索數據,基本操作流程如下所示:
1.創建集群
2.導入數據
3.搜索數據
4.刪除集群
場景描述
某女裝品牌在網上經營電商業務,其以前是使用傳統數據庫來為用戶提供商品搜索功能,但隨著用戶數量和業務的增長,使用傳統數據庫的弊端愈來愈明顯。主要問題表現為:響應速度慢、準確性低。為了改善用戶體驗從而避免用戶流失,該電商網站開始使用Elasticsearch搜索引擎來為用戶提供商品搜索功能,使用了一段時間后,不僅解決了之前使用傳統數據庫產生的問題,而且實現了用戶數量的增長。
本章節將介紹如何使用Elasticsearch搜索引擎為用戶提供搜索功能。
假設該電商網站經營商品的數據如下所示:
{
"products":[
{"productName":"2017秋裝新款文藝襯衫女裝","size":"L"}
{"productName":"2017秋裝新款文藝襯衫女裝","size":"M"}
{"productName":"2017秋裝新款文藝襯衫女裝","size":"S"}
{"productName":"2018春裝新款牛仔褲女裝","size":"M"}
{"productName":"2018春裝新款牛仔褲女裝","size":"S"}
{"productName":"2017春裝新款休閑褲女裝","size":"L"}
{"productName":"2017春裝新款休閑褲女裝","size":"S"}
]
}
創建集群
在開始搜索數據之前,您需要創建一個集群,其搜索引擎為Elasticsearch。例如,您可以創建一個名稱為“Sample-ESCluster”的集群。此集群僅用于入門指導使用,建議選用“節點規格”為“ess.spec-4u8g”,“節點存儲”為“高I/O”,“節點存儲容量”為“40GB”。
集群創建完成后,在集群列表查看已創建的集群,集群狀態為“可用”表示集群創建成功。如下圖所示。

導入數據
云搜索服務支持通過Logstash、Kibana或API將數據導入到Elasticsearch。其中Kibana是Elasticsearch的圖形化界面,便于交互驗證,因此,這里以Kibana為例介紹將數據導入到Elasticsearch的操作流程。
1.在云搜索服務的“集群管理”頁面上,單擊集群“操作”列的“Kibana”訪問集群。

2.在Kibana的左側導航中選擇“Dev Tools”,單擊“Get to work”,進入Console界面,如下圖所示。
Console左側區域為輸入框,右側為結果輸出區域,為執行命令按鈕。
3.在Console界面,執行如下命令創建索引“my_store”。
(7.x之前版本)
PUT /my_store
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"products": {
"properties": {
"productName": {
"type": "text",
"analyzer": "ik_smart"
},
"size": {
"type": "keyword"
}
}
}
}
}
(7.x之后版本)
PUT /my_store
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"properties": {
"productName": {
"type": "text",
"analyzer": "ik_smart"
},
"size": {
"type": "keyword"
}
}
}
}
返回結果如下所示。
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "my_store"
}
4.在Console界面,執行如下命令,將數據導入到“my_store”索引中。
(7.x之前版本)
POST /my_store/products/_bulk
{"index":{}}
{"productName":"2017秋裝新款文藝襯衫女裝","size":"L"}
{"index":{}}
{"productName":"2017秋裝新款文藝襯衫女裝","size":"M"}
{"index":{}}
{"productName":"2017秋裝新款文藝襯衫女裝","size":"S"}
{"index":{}}
{"productName":"2018春裝新款牛仔褲女裝","size":"M"}
{"index":{}}
{"productName":"2018春裝新款牛仔褲女裝","size":"S"}
{"index":{}}
{"productName":"2017春裝新款休閑褲女裝","size":"L"}
{"index":{}}
{"productName":"2017春裝新款休閑褲女裝","size":"S"}
(7.x之后版本)
POST /my_store/_doc/_bulk
{"index":{}}
{"productName":"2017秋裝新款文藝襯衫女裝","size":"L"}
{"index":{}}
{"productName":"2017秋裝新款文藝襯衫女裝","size":"M"}
{"index":{}}
{"productName":"2017秋裝新款文藝襯衫女裝","size":"S"}
{"index":{}}
{"productName":"2018春裝新款牛仔褲女裝","size":"M"}
{"index":{}}
{"productName":"2018春裝新款牛仔褲女裝","size":"S"}
{"index":{}}
{"productName":"2017春裝新款休閑褲女裝","size":"L"}
{"index":{}}
{"productName":"2017春裝新款休閑褲女裝","size":"S"}
當返回結果信息中“errors”字段的值為“false”時,表示導入數據成功。
搜索數據
全文檢索
假設用戶進入該電商網站,她想要查找名稱包含“春裝牛仔褲”的商品信息,可以搜索“春裝牛仔褲”。這里使用Kibana演示用戶搜索數據在后臺的執行命令和返回結果。
執行命令如下所示。
(7.x之前版本)
GET /my_store/products/_search
{
"query": {"match": {
"productName": "春裝牛仔褲"
}}
}
(7.x之后版本)
GET /my_store/_search
{
"query": {"match": {
"productName": "春裝牛仔褲"
}}
}
返回結果如下所示。
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.7965372,
"hits" : [
{
"_index" : "my_store",
"_type" : "_doc",
"_id" : "9xf6VHIBfClt6SDjw7H5",
"_score" : 1.7965372,
"_source" : {
"productName" : "2018春裝新款牛仔褲女裝",
"size" : "M"
}
},
{
"_index" : "my_store",
"_type" : "_doc",
"_id" : "-Bf6VHIBfClt6SDjw7H5",
"_score" : 1.7965372,
"_source" : {
"productName" : "2018春裝新款牛仔褲女裝",
"size" : "S"
}
},
{
"_index" : "my_store",
"_type" : "_doc",
"_id" : "-Rf6VHIBfClt6SDjw7H5",
"_score" : 0.5945667,
"_source" : {
"productName" : "2017春裝新款休閑褲女裝",
"size" : "L"
}
},
{
"_index" : "my_store",
"_type" : "_doc",
"_id" : "-hf6VHIBfClt6SDjw7H5",
"_score" : 0.5945667,
"_source" : {
"productName" : "2017春裝新款休閑褲女裝",
"size" : "S"
}
}
]
}
}
- Elasticsearch支持分詞,上面執行命令會將“春裝牛仔褲”分詞為“春裝”和“牛仔褲”。
- Elasticsearch支持全文檢索,上面執行命令會在所有商品信息中搜索包含“春裝”或“牛仔褲”的商品信息。
- Elasticsearch與傳統數據庫不同,它能借助倒排索引在毫秒級返回結果。
- Elasticsearch支持評分排序,在上面返回結果中,前兩條商品信息中同時出現了“春裝”和“牛仔褲”,后兩條商品信息中只出現了“春裝”,所以前兩條比后兩條與檢索關鍵詞的匹配度更高,分數更高,排序也更靠前。
聚合結果顯示
該電商網站可以提供聚合結果顯示功能,例如: 對“春裝”對應的產品按照尺碼分類,統計不同尺碼的數量。這里使用Kibana演示聚合結果顯示功能在后臺的執行命令和返回結果。
執行命令如下所示。
(7.x之前版本)
GET /my_store/products/_search
{
"query": {
"match": { "productName": "春裝" }
},
"size": 0,
"aggs": {
"sizes": {
"terms": { "field": "size" }
}
}
}
(7.x之后版本)
GET /my_store/_search
{
"query": {
"match": { "productName": "春裝" }
},
"size": 0,
"aggs": {
"sizes": {
"terms": { "field": "size" }
}
}
}
返回結果如下所示。
(7.x之前版本)
{
"took" : 31,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 4,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"sizes" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "S",
"doc_count" : 2
},
{
"key" : "L",
"doc_count" : 1
},
{
"key" : "M",
"doc_count" : 1
}
]
}
}
}
(7.x之后版本 )
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"sizes" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "S",
"doc_count" : 2
},
{
"key" : "L",
"doc_count" : 1
},
{
"key" : "M",
"doc_count" : 1
}
]
}
}
}
刪除集群
當您已完全了解Elasticsearch搜索引擎的使用流程和方法后,您可以參考如下步驟,刪除示例集群以及示例數據,避免造成資源浪費。
由于集群刪除后,數據無法恢復,請謹慎操作。
1.登錄云搜索服務管理控制臺。在左側菜單欄選擇“集群管理”。
2.進入集群管理頁面,選中“Sample-ESCluster”集群所在行,在操作列單擊“更多”>“刪除”。
3.在彈出的確認對話框中,單擊“確定”完成操作。