Elasticsearch學習六

一、document數據格式
二、電商網站商品管理案例:背景介紹
三、簡單的集羣管理
四、商品的CRUD操做(document CRUD操做)node

----------------------------------------------------------------------------------------------------------------------------數據庫

一、document數據格式json

面向文檔的搜索分析引擎api

(1)應用系統的數據結構都是面向對象的,複雜的
(2)對象數據存儲到數據庫中,只能拆解開來,變爲扁平的多張表,每次查詢的時候還得還原回對象格式,至關麻煩
(3)ES是面向文檔的,文檔中存儲的數據結構,與面向對象的數據結構是同樣的,基於這種文檔數據結構,es能夠提供複雜的索引,全文檢索,分析聚合等功能
(4)es的document用json數據格式來表達數據結構

public class Employee {elasticsearch

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

}ui

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

}對象

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

Employee employee = new Employee();
employee.setEmail("zhangsan@sina.com");
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":      "zhangsan@sina.com",
    "first_name": "san",
    "last_name": "zhang",
    "info": {
        "bio":         "curious and modest",
        "age":         30,
        "interests": [ "bike", "climb" ]
    },
    "join_date": "2017/01/01"
}

咱們就明白了es的document數據格式和數據庫的關係型數據格式的區別

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

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

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

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

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

三、簡單的集羣管理

(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

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

四、商品的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 }

相關文章
相關標籤/搜索