1. 程式人生 > >Elasticsearch快速入門案例

Elasticsearch快速入門案例

nod esp 每次 行高 dice climb 添加 last 我們

寫在前面的話:讀書破萬卷,編碼如有神
--------------------------------------------------------------------

參考內容:

  《Elasticsearch頂尖高手系列-快速入門篇》,中華石杉

--------------------------------------------------------------------

主要內容包括:

  • es的document數據格式和數據庫的關系型數據格式的區別
  • 簡單的集群管理
  • 電商網站商品管理案例背景介紹
  • 商品的CRUD操作(document 的CRUD操作 )

--------------------------------------------------------------------

1、es的document數據格式和數據庫的關系型數據格式的區別

比如有如下的json:

 1 {
 2 "email":"[email protected]",
 3 "first_name":"san",
 4 "last_name":"zhang",
 5 "info":{
 6     "bio":"curious and modest",
 7     "age":30,
 8     "interests":["bike","climb"]
 9
"join_date":"2017/01/01" 10 } 11 }

es的docuement可以直接用上面的json數據格式來表達。

但是在java中需要兩個類來表達:

 1 public class Employee {
 2 
 3   private String email;
 4   private String firstName;
 5   private String lastName;
 6   private EmployeeInfo info;
 7 
 8 }
 9 
10 private class EmployeeInfo {
11   
12   private
String bio; // 性格 13 private Integer age; 14 private String[] interests; // 興趣愛好 15 private Date joinDate; 16 }

可以看出employee對象裏面包含了Employee類自己的屬性,而且還有一個EmployeeInfo對象。

在數據庫中的話,就需要兩張表:employee表、employee_info表,將employee對象的數據重新拆開來,變成Employee數據和EmployeeInfo數據

employee表:email,first_name,last_name,join_date,4個字段
employee_info表:bio,age,interests,3個字段;此外還有一個外鍵字段,比如employee_id,關聯著employee表。

我們就明白了es的document數據格式和數據庫的關系型數據格式的區別:

  • 應用系統的數據結構都是面向對象的,復雜的
  • 對象數據存儲到數據庫中,只能拆解開來,變為扁平的多張表,每次查詢的時候還得還原回對象格式,相當麻煩
  • ES是面向文檔的,文檔中存儲的數據結構,與面向對象的數據結構是一樣的,基於這種文檔數據結構,es可以提供復雜的索引、全文檢索、分析聚合等功能
  • es的document用json數據格式來表達

--------------------------------------------------------------------

2、簡單的集群管理

(2.1)快速檢查集群的健康狀況

在Kibana中執行如下命令:  GET _cat/health?v

1 epoch      timestamp cluster                 status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
2 1518657893 09:24:53  huobaopaocai-es-cluster yellow          1         1      1   1    0    0        1             0                  -                 50.0%

如何快速的了解集群的健康狀態? green、yellow、red

  • green: 每個索引的primary shard和replica shard都是active狀態
  • yellow: 每個索引的primary shard都是active狀態,但是部分replica shard不是active狀態,處於不可用狀態
  • red: 不是所有索引的primary shard都是active狀態的,部分索引有數據丟失

為什麽現在我們的是處於yellow狀態?

  我們現在就一個筆記本電腦,就啟動了一個es進程,相當於就只有一個node。現在es中有一個index,就是kibana自己內置建立的index。由於默認的配置是給每個index分配5個primary shard和5個replica shard,而且primary shard和replica shard不能在同一臺機器上(為了容錯)。現在kibana自己建立的index是1個primary shard和1個replica shard。當前就一個node,所以只有1個primary shard被分配了和啟動了,但是一個replica shard沒有第二臺機器去啟動。

(2.2)快速查看集群中有些索引

在Kibana中執行如下命令:  GET _cat/indices?v

1 health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
2 yellow open   .kibana CaxZ5uJGSJy3rCzv_3RIzQ   1   1          1            0      3.1kb          3.1kb

(2.3)簡單的索引操作

創建索引: PUT /test_index?pretty

1 {
2   "acknowledged": true,
3   "shards_acknowledged": true
4 }

再次查看索引:

1 GET _cat/indices?v
2 
3 health status index      uuid                   pri rep docs.count docs.deleted store.size pri.store.size
4 yellow open   .kibana    CaxZ5uJGSJy3rCzv_3RIzQ   1   1          1            0      3.1kb          3.1kb
5 yellow open   test_index i2LdlSIqRXCZQCLauVBiRw   5   1          0            0       650b           650b

刪除索引: DELETE /test_index?pretty

{
  "acknowledged": true
}

再次查看索引:

1 GET _cat/indices?v
2 
3 health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
4 yellow open   .kibana CaxZ5uJGSJy3rCzv_3RIzQ   1   1          1            0      3.1kb          3.1kb

--------------------------------------------------------------------

3、電商網站商品管理案例背景介紹

有一個電商網站,需要為其基於ES構建一個後臺系統,提供以下功能:

  • 對商品信息進行CRUD操作
  • 執行簡單的結構化查詢
  • 可以執行簡單的全文檢索,以及復雜的phrase(短語)檢索
  • 對於全文檢索的結果,可以進行高亮顯示
  • 對數據進行簡單的聚合分析

--------------------------------------------------------------------

4、商品的CRUD操作(document 的CRUD操作 )

(4.1)新增商品:新增文檔、建立索引

基本語法格式:

1 PUT /index/type/id
2 {
3   "json數據"
4 }

準備三條數據:

 1 PUT /ecommerce/product/1
 2 {
 3     "name" : "gaolujie yagao",
 4     "desc" :  "gaoxiao meibai",
 5     "price" :  30,
 6     "producer" :      "gaolujie producer",
 7     "tags": [ "meibai", "fangzhu" ]
 8 }
 9 
10 PUT /ecommerce/product/2
11 {
12     "name" : "jiajieshi yagao",
13     "desc" :  "youxiao fangzhu",
14     "price" :  25,
15     "producer" :      "jiajieshi producer",
16     "tags": [ "fangzhu" ]
17 }
18 
19 PUT /ecommerce/product/3
20 {
21     "name" : "zhonghua yagao",
22     "desc" :  "caoben zhiwu",
23     "price" :  40,
24     "producer" :      "zhonghua producer",
25     "tags": [ "qingxin" ]
26 }

執行每條新增語句的結果:

 1 {
 2   "_index": "ecommerce",
 3   "_type": "product",
 4   "_id": "1",
 5   "_version": 1,
 6   "result": "created",
 7   "_shards": {
 8     "total": 2,
 9     "successful": 1,
10     "failed": 0
11   },
12   "created": true
13 }

(ps:es會自動建立index和type,不需要提前創建,而且es默認會對document每個field建立倒排索引,讓其可以被搜索。)

用java來實現往es中添加docuemnt操作

Elasticsearch - java客戶端連接

    @Autowired
    private ElasticsearchConstant elasticsearchConstant;

    /**
     * 新增es的docuemnt
     */
    @Test
    public void createDocumentTest() throws IOException {
        TransportClient client = elasticsearchConstant.getClient();
        IndexResponse response = client.prepareIndex(elasticsearchConstant.getEsIndex(), elasticsearchConstant.getEsType(),"1")
                .setSource(XContentFactory.jsonBuilder()
                        .startObject()
                        .field("name","gaolujie yagao")
                        .field("desc", "gaoxiao meibai")
                        .field("price", 30)
                        .field("producer", "gaolujie producer")
                        .endObject())
                .get();
        LOG.info(String.format("新增es的docuemnt結果: %s",response.toString()));
    }


執行結果:
2018-02-15 11:37:24  INFO [main] (EcommerceTest.java:56) createDocumentTest - 新增es的docuemnt結果: IndexResponse[index=ecommerce,type=product,id=1,version=1,result=created,shards={"total":2,"successful":1,"failed":0}]

(4.2)檢索索引:檢索文檔

基本語法格式:

1 GET /index/type/id

技術分享圖片

用java來實現查詢es中docuemnt操作

Elasticsearch - java客戶端連接

 1 @Autowired
 2 private ElasticsearchConstant elasticsearchConstant;
 3  
 4    /**
 5      * 查詢es的document
 6      */
 7     @Test
 8     public void getDocumentTest() throws IOException{
 9         TransportClient client = elasticsearchConstant.getClient();
10         GetResponse response = client.prepareGet(elasticsearchConstant.getEsIndex(), elasticsearchConstant.getEsType(), "1").get();
11         LOG.info(String.format("查詢es的docuemnt結果: %s",response.toString()));
12     }
13 
14 執行結果:
15 2018-02-15 11:42:19  INFO [main] (EcommerceTest.java:68) getDocumentTest - 查詢es的docuemnt結果: {"_index":"ecommerce","_type":"product","_id":"1","_version":1,"found":true,"_source":{"name":"gaolujie yagao","desc":"gaoxiao meibai","price":30,"producer":"gaolujie producer"}}

(4.3)修改商品:替換文檔

技術分享圖片

(ps:替換方式有一個不好:必須帶上所有的field才能去進行信息的修改。)

(4.4)修改商品:更新文檔

技術分享圖片

技術分享圖片

用java來實現編輯es中的docuement:

Elasticsearch - java客戶端連接

 1 @Autowired
 2 private ElasticsearchConstant elasticsearchConstant;
 3 
 4     /**
 5      * 更新es的docuemnt
 6      * @throws IOException
 7      */
 8     @Test
 9     public void  updateDocument() throws IOException {
10         TransportClient client = elasticsearchConstant.getClient();
11         UpdateResponse updateResponse = client.prepareUpdate(elasticsearchConstant.getEsIndex(), elasticsearchConstant.getEsType(), "1")
12                 .setDoc(jsonBuilder()
13                         .startObject()
14                         .field("name", "jiaqiang gaolujie yagao")
15                         .endObject())
16                 .get();
17         LOG.info(String.format("更新es的docuemnt結果: %s",updateResponse.toString()));
18     }
19 
20 運行結果:
21 2018-02-15 15:55:13  INFO [main] (EcommerceTest.java:73) updateDocument - 更新es的docuemnt結果: UpdateResponse[index=ecommerce,type=product,id=1,version=2,result=updated,shards=ShardInfo{total=2, successful=1, failures=[]}]

(4.5)刪除商品:刪除文檔

技術分享圖片

技術分享圖片

用java來實現刪除es中的docuement:

Elasticsearch - java客戶端連接

 1 @Autowired
 2 private ElasticsearchConstant elasticsearchConstant;
 3  
 4    /**
 5      * 刪除es中的document
 6      */
 7     @Test
 8     public void deleteDocument(){
 9         TransportClient client = elasticsearchConstant.getClient();
10         DeleteResponse response = client.prepareDelete(elasticsearchConstant.getEsIndex(), elasticsearchConstant.getEsType(), "1").get();
11         LOG.info(String.format("刪除es的docuemnt結果: %s",response.toString()));
12     }
13 
14 運行結果:
15 2018-02-15 16:00:04  INFO [main] (EcommerceTest.java:84) deleteDocument - 刪除es的docuemnt結果: DeleteResponse[index=ecommerce,type=product,id=1,version=3,result=deleted,shards=ShardInfo{total=2, successful=1, failures=[]}]

Elasticsearch快速入門案例