ES 06 - 經過Kibana插件操做ES中的索引文檔 (CRUD操做)

向ES中添加文檔時, ES會根據文檔中各個字段的類型, 自動推測並建立映射(mapping), 這是比Solr靈活的地方;
這在大部分狀況下是可用的, 但生產環境中不建議如此使用: 應該根據業務需求定製mapping映射. 關於定製mapping, 請參考後續文章.編程

1 建立、刪除索引

1.1 建立索引

# 建立索引API: 
PUT test_index?pretty
# 響應信息以下:
#! Deprecation: the default number of shards will change from [5] to [1] in 7.0.0; 
# if you wish to continue using the default of [5] shards, you must manage this on the create index request or with an index template
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "test_index"
}

# 查看集羣中的索引: 
health status index        uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   test_index   hMeJ-M9pSHSXl0t39OedYw   5   1          0            0      1.2kb          1.2kb
yellow open   .kibana_1    4q7ELvdcTVilW3UwtMWqeg   1   0         18            0     78.5kb         78.5kb

過期說明:json

在建立索引時, Elasticsearch提出過期警告: 從7.0.0版本開始, 默認的Shard個數將從[5]變爲[1].
若是要繼續使用默認的[5]個分片(Shard), 就須要在建立Index時指定, 或者經過索引模板建立Index.
關於建立Index時指定分片個數的方法, 參見後續的博文.bash

2.2 刪除索引

# 刪除索引API:
DELETE test_index?pretty
# 響應信息以下: 
{
  "acknowledged": true
}

2 document的結構

ES是一款面向文檔的數據搜索、分析引擎. document結構說明:數據結構

(1) 基於面向對象的開發思想, 應用系統中的數據結構都是很複雜的: 對象中嵌套對象, 如CRM系統中的客戶對象中, 還會嵌入客戶相關的企業對象.併發

(2) 對象數據存儲到數據庫中, 須要分解, 將嵌套對象分解爲扁平的多張表數據, 每次操做時須要還原回對象格式, 過程繁瑣.app

(3) ES存儲的是JSON格式的文檔, 基於此, ES能夠提供複雜的索引, 全文檢索, 分析聚合等功能.編程語言

(4) document格式示例:學習

{
    "id": "5220",
    "name": "張三",
    "sex": "男",
    "age": 25, 
    "phone": 13312345678, 
    "email": "zhangsan@163.com",
    "company": {
        "name": "Alibaba",
        "location": "杭州"
    },
    "join_date": "2018/11/01"
}

接下來以電商系統中的搜索子系統爲例, 演示對文檔的操做方法.ui

3 添加文檔

(1) 添加API:

PUT index/type/id
{
  "JSON格式的文檔數據"
}

說明: ES會自動建立PUT API中指定的index和type, 不須要提早建立;

並且ES默認對document的每一個field都創建倒排索引, 保證它們均可以被檢索.

(2) 添加示例:

PUT book_shop/it_book/1
{
    "name": "Java編程思想",
    "author": "[美] Bruce Eckel",
    "category": "編程語言",
    "desc":  "Java學習必讀經典,殿堂級著做!",
    "price":  109.0,
    "publisher": "機械工業出版社",
    "date": "2007-06-01",
    "tags": [ "Java", "編程語言" ]
}

(3) 添加以後的響應信息:

{
    "_index" : "book_shop",
    "_type" : "it_book",
    "_id" : "1",
    "_version" : 1,
    "result" : "created",   # 操做結果: created(建立)了索引
    "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
    },
    "_seq_no" : 0,
    "_primary_term" : 1
}

(4) 再添加以下數據:

PUT book_shop/it_book/2
{
    "name": "深刻理解Java虛擬機:JVM高級特性與最佳實踐",
    "author": "周志明",
    "category": "編程語言",
    "desc":  "Java圖書領域公認的經典著做",
    "price":  79.0,
    "date": "2013-10-01",
    "publisher": "機械工業出版社",
    "tags": [ "Java", "虛擬機", "最佳實踐" ]
}

PUT book_shop/it_book/3
{
    "name": "Java併發編程的藝術",
    "author": "方騰飛,魏鵬,程曉明",
    "category": "編程語言",
    "desc":  "阿里系工程師的併發編程實踐",
    "price":  59.0,
    "date": "2015-07-10",
    "publisher": "機械工業出版社",
    "tags": [ "Java", "併發編程" ]
}

4 查詢文檔

(1) 檢索API:

GET index/type/id

(2) 檢索示例:

GET book_shop/it_book/2

(3) 檢索的結果:

{
  "_index" : "book_shop1",
  "_type" : "it_book",
  "_id" : "2",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "深刻理解Java虛擬機:JVM高級特性與最佳實踐",
    "author" : "周志明",
    "category" : "編程語言",
    "desc" : "Java圖書領域公認的經典著做",
    "price" : 79.0,
    "date" : "2013-10-01",
    "publisher" : "機械工業出版社",
    "tags" : [
      "Java",
      "虛擬機",
      "最佳實踐"
    ]
  }
}

5 修改文檔

5.1 替換文檔

(1) 替換API - 與添加API相同, 只不過文檔id要存在, 此時系統將斷定爲修改操做:

—— 不管文檔數據是否存在修改, 對同一id的文檔執行1次以上的PUT操做, 都是修改操做.

PUT index/type/id
{
    "JSON格式的文檔數據"
}

注意: 替換方式的不便之處: 必須填寫要修改文檔的全部field, 若是缺乏, 修改後的文檔中將丟失相關field.

(2) 替換示例 - 爲name添加了"(第4版)"

PUT book_shop/it_book/1
{
    "name": "Java編程思想(第4版)",
    "author": "[美] Bruce Eckel",
    "category": "編程語言",
    "desc":  "Java學習必讀經典,殿堂級著做!",
    "price":  109.0,
    "date": "2007-06-01",
    "publisher": "機械工業出版社",
    "tags": [ "Java", "編程語言" ]
}

(3) 替換的結果信息:

{
  "_index" : "book_shop",
  "_type" : "it_book",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",  // 操做結果: updated(修改)了索引
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

5.2 更新文檔

(1) 更新API:

—— 經過POST_update, 更新文檔中的特定字段(field), 其餘的字段不會改動.

POST index/type/id/_update
{
    "doc": {
        "field u want to update": "new value of ur update's field"
    }
}

注意: 與替換方式相比, 更新方式的好處: 能夠更新指定文檔的指定field, 未指定的field也不會丟失.

(2) 更新示例 - 將name改成英文:

POST book_shop/it_book/1/_update
{
    "doc": {
        "name": "Thinking in Java(4th Edition) "
    }
}

(3) 更新的結果信息:

{
  "_index" : "book_shop",
  "_type" : "it_book",
  "_id" : "1",
  "_version" : 3,
  "result" : "updated",  // 操做結果: updated(修改)了索引
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

此時查看該文檔 GET book_shop/it_book/1, 能夠發現更新成功.

6 刪除文檔

(1) 刪除API:

DELETE index/type/id

(2) 刪除示例:

DELETE book_shop/it_book/1

(3) 刪除的結果信息:

{
  "_index" : "book_shop",
  "_type" : "it_book",
  "_id" : "1",
  "_version" : 4,
  "result" : "deleted",  // 操做結果: deleted(刪除)了索引
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

(4) 刪除確認: 再次查看刪除的文檔:

GET book_shop/it_book/1

(5) 確認的信息:

{
  "_index" : "book_shop",
  "_type" : "it_book",
  "_id" : "1",
  "found" : false   // 沒有查找到相關文檔
}

爲了後期演示的方便, 再次將該文檔添加至索引中:

PUT book_shop/it_book/1
{
    "name": "Java編程思想(第4版)",
    "author": "[美] Bruce Eckel",
    "category": "編程語言",
    "desc":  "Java學習必讀經典,殿堂級著做!",
    "price":  109.0,
    "date": "2007-06-01",
    "publisher": "機械工業出版社",
    "tags": [ "Java", "編程語言" ]
}

版權聲明

做者: 馬瘦風(https://healchow.com)

出處: 博客園 馬瘦風的博客(https://www.cnblogs.com/shoufeng)

感謝閱讀, 若是文章有幫助或啓發到你, 點個[好文要頂👆] 或 [推薦👍] 吧😜

本文版權歸博主全部, 歡迎轉載, 但 [必須在文章頁面明顯位置標明原文連接], 不然博主保留追究相關人員法律責任的權利.

相關文章
相關標籤/搜索