Elasticsearch核心技術與實戰學習筆記 第三章 11 | 文檔的基本CRUD與批量操作

一 序

   本文屬於極客時間Elasticsearch核心技術與實戰學習筆記系列。

   本節課學習文檔的基本CRUD操作。

二 文檔CRUD

create 

支持自動生成文檔 Id 和指定文檔 Id 兩種方法.

1 自動生成文檔 Id

#create document. 自動生成 _id
POST users/_doc
{
	"user" : "Mike",
    "post_date" : "2019-04-15T14:12:12",
    "message" : "trying out Kibana"
}

返回:

{
  "_index" : "users",
  "_type" : "_doc",
  "_id" : "zQhRBHIBb3-P948QM8DK",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}
 

#create document. 指定Id。如果id已經存在,報錯
PUT users/_doc/1?op_type=create
{
    "user" : "Jack",
    "post_date" : "2019-05-15T14:12:12",
    "message" : "trying out Elasticsearch"
}

{
  "_index" : "users",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

存在是提示:

{
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[1]: version conflict, document already exists (current version [1])",
        "index_uuid": "fqVg1Y6NQHurMFOXu4fjQw",
        "shard": "0",
        "index": "users"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[1]: version conflict, document already exists (current version [1])",
    "index_uuid": "fqVg1Y6NQHurMFOXu4fjQw",
    "shard": "0",
    "index": "users"
  },
  "status": 409
}

2、Get 一個文檔


1)找到文檔,返回 HTTP 200

GET users/_doc/1 

其中,文檔元信息

  • _index/_type/,_type 在版本7中只有 _doc 類型
  • _version 版本信息,同一個 Id 的文檔被刪除了,版本號也會增加
  • _source 中默認包含文檔的原始信息

2)沒找到文檔,返回HTTP404

3 Index 文檔

Index 也是用於創建文檔的方法,和 Create 不同有一些不同,如果文檔不存在情況,直接創建新文檔,否者刪除原來的文檔,新文檔被索引,_version 版本加一。

   PUT users/_doc/1
{
    "user" : "Mike"
}

 

3)index 操作後查看數據

、Update 文檔

Update 方法不會刪除原來文檔,而是實現真正的數據更新
Post方法/Payload需要包含在 “doc”中

post結果:

{
  "_index" : "users",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 1
}

再次執行查詢,多了兩個字段

5、Delete 文檔
1)通過id刪除文檔
2)請求中要包含在 “doc”中

# 刪除文檔
DELETE users/_doc/1

三、批量操作

腳本:

#執行第1次
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test2", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
{
  "took" : 3665,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "delete" : {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "2",
        "_version" : 1,
        "result" : "not_found",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 1,
        "_primary_term" : 1,
        "status" : 404
      }
    },
    {
      "create" : {
        "_index" : "test2",
        "_type" : "_doc",
        "_id" : "3",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "update" : {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_version" : 2,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 2,
        "_primary_term" : 1,
        "status" : 200
      }
    }
  ]
}

2)批量讀取-mget
批量操作,可以減少網絡連接開銷,提高性能
可以在docs中指定index,也可以在URL路徑中指定index,

### mget 操作
GET /_mget
{
    "docs" : [
        {
            "_index" : "test",
            "_id" : "1"
        },
        {
            "_index" : "test",
            "_id" : "2"
        }
    ]
}

   

#URI中指定index
GET /test/_mget
{
    "docs" : [
        {

            "_id" : "1"
        },
        {

            "_id" : "2"
        }
    ]
}

3)批量查詢-msearch
多個查詢匹配條件組合

### msearch 操作
POST kibana_sample_data_ecommerce/_msearch
{}
{"query" : {"match_all" : {}},"size":1}
{"index" : "kibana_sample_data_flights"}
{"query" : {"match_all" : {}},"size":2}

常見錯誤

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章