ElasticSearch基本的CURD

1.創(chuàng)建一個名為member的索引數(shù)據(jù)
PUT member
{
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "name": {
        "type": "keyword"
      },
      "age": {
        "type": "byte"
      }
    }
  }
}

創(chuàng)建成功

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "member"
}
2.基本的CURD
image.png
  • T1 如果文檔存在則是修改,則會刪除再創(chuàng)建新的文檔

PUT member/_doc/1
{
  "id":1,
  "age":1,
  "name":"this is first."
}

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

如果再次對 id = 1 這條數(shù)據(jù)進(jìn)行以下操作,可以看到原來的數(shù)據(jù)字段age 和 id不在了。

PUT member/_doc/1
{
  "name":"this is update2"
}

返回結(jié)果:

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

查詢id=1的數(shù)據(jù)

{
  "_index" : "member",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 7,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "this is update2"
  }
}

  • T2 創(chuàng)建指定ID的文檔
POST member/_create/2
{
  "doc": {
    "id": 2,
    "name": "name2",
    "age": 11
  }
}

注:重復(fù)插入數(shù)據(jù)會報錯.拋出一個 409 的錯誤

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


  • T3 創(chuàng)建一個自動生成ID的文檔
POST member/_doc
{
  "doc": {
    "id": 10,
    "name": "rand-name",
    "age": 20
  }
}


  • T4 獲取指定ID的文檔數(shù)據(jù)
GET member/_doc/2
{
  "_index" : "member",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 1,
  "_seq_no" : 10,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "doc" : {
      "id" : 2,
      "name" : "name2",
      "age" : 11
    }
  }
}

  • T5 修改指定ID的文檔數(shù)據(jù)
POST member/_update/3
{
  "doc": {
    "name": "name3 is update"
  }
}


  • T6 刪除指定ID的文檔數(shù)據(jù)
DELETE member/_doc/2

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容