1. 程式人生 > >Elasticserach簡單實戰--1

Elasticserach簡單實戰--1

1、document資料格式
2、電商網站商品管理案例:背景介紹
3、簡單的叢集管理
4、商品的CRUD操作(document CRUD操作)

1、document資料格式

面向文件的搜尋分析引擎

(1)應用系統的資料結構都是面向物件的,複雜的
(2)物件資料儲存到資料庫中,只能拆解開來,變為扁平的多張表,每次查詢的時候還得還原回物件格式,相當麻煩
(3)ES是面向文件的,文件中儲存的資料結構,與面向物件的資料結構是一樣的,基於這種文件資料結構,es可以提供複雜的索引,全文檢索,分析聚合等功能
(4)es的document用json資料格式來表達


 class Employee:
 

  private String email;
  private String firstName;
  private String lastName;
  private EmployeeInfo info;
  private Date joinDate;

}

private class EmployeeInfo {
  
  private String bio; // 性格
  private Integer age;
  private String[] interests; // 興趣愛好

}

EmployeeInfo info = new EmployeeInfo();
info.setBio("curious and modest");
info.setAge(30);
info.setInterests(new String[]{"bike", "climb"});

Employee employee = new Employee();
employee.setEmail("
[email protected]
"); employee.setFirstName("san"); employee.setLastName("zhang"); employee.setInfo(info); employee.setJoinDate(new Date());

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表
{
    "email":      "[email protected]",
    "first_name": "san",
    "last_name": "zhang",
    "info": {
        "bio":         "curious and modest",
        "age":         30,
        "interests": [ "bike", "climb" ]
    },
    "join_date": "2017/01/01"
}

我們就明白了es的document資料格式和資料庫的關係型資料格式的區別

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

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

(1)對商品資訊進行CRUD(增刪改查)操作
(2)執行簡單的結構化查詢
(3)可以執行簡單的全文檢索,以及複雜的phrase(短語)檢索
(4)對於全文檢索的結果,可以進行高亮顯示
(5)對資料進行簡單的聚合分析

3、簡單的叢集管理

(1)快速檢查叢集的健康狀況

es提供了一套api,叫做cat api,可以檢視es中各種各樣的資料

GET /_cat/health?v

epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1488006741 15:12:21 elasticsearch yellow 1 1 1 1 0 0 1 0 - 50.0%

epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1488007113 15:18:33 elasticsearch green 2 2 2 1 0 0 0 0 - 100.0%

epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1488007216 15:20:16 elasticsearch 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沒有第二臺機器去啟動。

做一個小實驗:此時只要啟動第二個es程序,就會在es叢集中有2個node,然後那1個replica shard就會自動分配過去,然後cluster status就會變成green狀態。

(2)快速檢視叢集中有哪些索引

GET /_cat/indices?v

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

(3)簡單的索引操作

建立索引:PUT /test_index?pretty

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open test_index XmS9DTAtSkSZSwWhhGEKkQ 5 1 0 0 650b 650b
yellow open .kibana rUm9n9wMRQCCrRDEhqneBg 1 1 1 0 3.1kb 3.1kb

刪除索引:DELETE /test_index?pretty

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

4、商品的CRUD操作

(1)新增商品:新增文件,建立索引

PUT /index/type/id
{
  "json資料"
}
###############################################
PUT /ecommerce/product/1
{
    "name" : "gaolujie yagao",
    "desc" :  "gaoxiao meibai",
    "price" :  30,
    "producer" :      "gaolujie producer",
    "tags": [ "meibai", "fangzhu" ]
}
---------------結果-----------------------------
{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}
########################################################
PUT /ecommerce/product/2
{
    "name" : "jiajieshi yagao",
    "desc" :  "youxiao fangzhu",
    "price" :  25,
    "producer" :      "jiajieshi producer",
    "tags": [ "fangzhu" ]
}
##############################################################
PUT /ecommerce/product/3
{
    "name" : "zhonghua yagao",
    "desc" :  "caoben zhiwu",
    "price" :  40,
    "producer" :      "zhonghua producer",
    "tags": [ "qingxin" ]
}

es會自動建立index和type,不需要提前建立,而且es預設會對document每個field都建立倒排索引,讓其可以被搜尋

(2)查詢商品:檢索文件

GET /index/type/id
GET /ecommerce/product/1
-----------------結果--------------------
{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "name": "gaolujie yagao",
    "desc": "gaoxiao meibai",
    "price": 30,
    "producer": "gaolujie producer",
    "tags": [
      "meibai",
      "fangzhu"
    ]
  }
}

(3)修改商品:替換文件

PUT /ecommerce/product/1


{
    "name" : "jiaqiangban gaolujie yagao",
    "desc" :  "gaoxiao meibai",
    "price" :  30,
    "producer" :      "gaolujie producer",
    "tags": [ "meibai", "fangzhu" ]
}
--------------------------結果-----------------------
{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": false
}

############################################
PUT /ecommerce/product/1
{
    "name" : "jiaqiangban gaolujie yagao"
}

替換方式有一個不好,即使必須帶上所有的field,才能去進行資訊的修改

(4)修改商品:更新文件

POST /ecommerce/product/1/_update
{
  "doc": {
    "name": "jiaqiangban gaolujie yagao"
  }
}
--------------------------------結果----------------------------------
{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 8,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}

我的風格,其實有選擇的情況下,不太喜歡念ppt,或者照著文件做,或者直接貼上寫好的程式碼,儘量是純手敲程式碼

(5)刪除商品:刪除文件

DELETE /ecommerce/product/1
---------------------結果----------------------------
{
  "found": true,
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 9,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "found": false
}