ElasticSearch(二):文檔的基本CRUD與批量操做

ElasticSearch(二):文檔的基本CRUD與批量操做

學習課程連接《Elasticsearch核心技術與實戰》網絡

<br/> ## Create 文檔 支持自動生成文檔_id和指定文檔_id兩種方式。 * 經過調用`POST index_name/_doc`,系統會自動生成文檔 _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" : "TyPHr20BkakgvNgYZu2L",#自動生成文檔的_id "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1 } ``` * 使用`PUT index_name/_create/_id`或`PUT index_name/_doc/_id?op_type=create`建立時,URI中顯示指定`_create`,此時若是該_id的文檔已經存在,操做會失敗。 ``` #1.create document. 指定 _id 若是已經存在,就報錯 PUT users/_create/1 { "user" : "Jack", "post_date" : "2019-05-15T14:12:12", "message" : "trying out Elasticsearch" } #2.create document. 指定_id。若是_id已經存在,報錯 PUT users/_doc/1?op_type=create { "user" : "Jack", "post_date" : "2019-05-15T14:12:12", "message" : "trying out Elasticsearch" } ``` ``` #若是_id已經存在報錯信息,以下: { "error": { "root_cause": [ { "type": "version_conflict_engine_exception", "reason": "[1]: version conflict, document already exists (current version [1])", "index_uuid": "ohLNyzUmTv6cm-Ih9kH0bw", "shard": "0", "index": "users" } ], "type": "version_conflict_engine_exception", "reason": "[1]: version conflict, document already exists (current version [1])", "index_uuid": "ohLNyzUmTv6cm-Ih9kH0bw", "shard": "0", "index": "users" }, "status": 409 } ```post

<br/> ## Index 文檔 Index和Create不同的地方:若是文檔不存在,就索引新的文檔。不然現有的文檔會被刪除,新的文檔被索引,版本信息+1。使用`PUT index_name/_doc/_id`。 ``` PUT users/_doc/1 { "user" : "Mike" } ``` ``` #返回結果 { "_index" : "users", "_type" : "_doc", "_id" : "1", "_version" : 3, #版本增長 "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 4, "_primary_term" : 2 } ```性能

<br/> ## Update 文檔 Update方法不會刪除原來的文檔,而是實現真正的數據更新,更新的文檔必須存在,更新的內容須要包含在`doc`中。 ``` #更新文檔API POST index_name/_update/_id { "doc":{ "field1":"value1", "field2":"value2" } } ```學習

#更新_id=1文檔
POST users/_update/1
{
    "doc":{
        "post_date" : "2019-05-15T14:12:12",
         "message" : "trying out Elasticsearch"
    }  
}

<br/> ## Get 文檔 根據文檔ID,獲取相應文檔信息,`GET index_name/_doc/_id` ``` #Get the document by ID GET users/_doc/1 ``` ``` #返回結果 { "_index" : "users", "_type" : "_doc", "_id" : "1", "_version" : 1, "_seq_no" : 2, "_primary_term" : 1, "found" : true, "_source" : { "user" : "Jack", "post_date" : "2019-05-15T14:12:12", "message" : "trying out Elasticsearch" } } ```ui

<br/> ## Delete 文檔 根據文檔ID,刪除相應文檔信息,`DELETE index_name/_doc/_id` ``` # 刪除文檔 DELETE users/_doc/1 ```url

<br/> ## 批量操做-bulk 批量操做,能夠減小網絡鏈接所產生的開銷,提升性能。 * 支持在一次API調用中,對不一樣的索引進行操做。 * 支持四種類型操做:`Index`,`Create`,`Update`,`Delete`。 * 能夠在URI中指定Index,也能夠在請求中指定。 * 操做中單條操做失敗,並不會影響其餘操做。 * 返回結果包括了每一條操做執行的結果。 * 不要發送過多數據,通常建議是1000-5000個文檔,若是你的文檔很大,能夠適當減小隊列,大小建議是5-15MB,默認不能超過100M,會報錯。spa

### Bulk 操做
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" : 227,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "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" : 1,
          "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" : 1,
          "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" : 1,
          "failed" : 0
        },
        "_seq_no" : 2,
        "_primary_term" : 1,
        "status" : 200
      }
    }
  ]
}

<br/> ## 批量讀取-mget mget 是經過文檔`_id`列表獲得文檔信息。 ``` ### mget 操做 GET /_mget { "docs" : [ { "_index" : "test", "_id" : "1" }, { "_index" : "test", "_id" : "2" } ] }.net

#URI中指定index GET /test/_mget { "docs" : [ { "_id" : "1" }, { "_id" : "2" } ] }code

GET /_mget { "docs" : [ { "_index" : "test", "_id" : "1", "_source" : false }, { "_index" : "test", "_id" : "2", "_source" : ["field3", "field4"] }, { "_index" : "test", "_id" : "3", "_source" : { "include": ["user"], "exclude": ["user.location"] } } ] }索引

 
 

#返回結果 { "docs" : [ { "_index" : "test", "_type" : "_doc", "_id" : "1", "_version" : 4, "_seq_no" : 5, "_primary_term" : 1, "found" : true, "_source" : { "field1" : "value1", "field2" : "value2" } }, { "_index" : "test", "_type" : "_doc", "_id" : "2", "found" : false } ] }

<br/>
## 批量查詢-msearch
msearch 是根據查詢條件,搜索到相應文檔。

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

<br/>
## 常見錯誤返回說明
問題|緣由
---|:--
沒法鏈接|網絡故障或集羣掛了
鏈接沒法關閉|網絡故障或節點出錯
429|集羣過於繁忙
4xx|請求體格式有錯
500|集羣內部錯誤
相關文章
相關標籤/搜索