对Elasticsearch中的文档进行CRUD操作

文档的CRUD(创建、读取、更新、删除)

  • Type名:约定都用_doc。
  • Create:如果ID已存在,会创建失败。
  • Index:如果ID不存在,创建新的文档。否则,先删除现有的文档。再创建新的文档,版本会增加。
  • Update:文档必须已经存在,更新只会对相应字段做增量修改。
    注意:此处的Index并不是名词索引,而是动词。
Index:PUT my_index/_doc/1 {"user":"mike","comment":"you know,for search."}
Create:PUT my_index/_create/1 {"user":"mike","comment":"you know,for search."}  
    POST my_index/_doc/ {"user":"mike","comment":"you know,for search."} 
Read:GET my_index/_doc/1
Update:POST my_index/update/1 {"doc":{"user":"mike","comment":"you know,for search."} }
Delete:DELETE my_index/_doc/1

注意:以上操作都可以通过curl命令来实现。示例:curl -X PUT --data ‘{“user”:“mike”,“comment”:“you know,for search.”}’ http://192.168.X.X:9200/my_index/_doc/1

Create一个文档:

  • 支持自动生成文档ID和指定文档ID两种方式。
  • 通过调用“post /users/_doc”,系统会自动生成document ID。
  • 使用HTTP PUT user/_create/1创建时,URI中显示指定_create,此时,如果该ID的文档已经存在,操作失败。
# 示例
# Create
POST my_index/_create/2
{
  "user":"1",
  "comment":"test"
}

POST my_index/_doc
{
  "user":"2",
  "comment":"test"
}

Get一个文档:

  • 找到文档,返回http 200
    • _index/_type/
    • 版本信息,同一个Id的文档,即使被删除,Version号也会不断增加。
    • _source:中包含了文档的所有原始信息。
  • 找不到文档,返回http 404

示例:

GET my_index/_doc/2
{
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 1,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "user" : "1",
    "comment" : "test"
  }
}

Index文档

  • Index和Create不一样的地方是,如果文档不存在,就创建新的文档。否则,现有文档会被删除,新的文档被索引。版本信息+1.
示例:
# Index
PUT my_index/_doc/1
{
  "user":"li",
  "comment":"you know,for search"
}

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

PUT my_index/_doc/1
{
  "user":"156",
  "comment":"you know,for search"
}
{
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 1
}

GET my_index/_doc/1
{
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 4,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "user" : "156",
    "comment" : "you know,for search"
  }
}

Update文档

  • Update方法不会删除原有的文档,而是实现整整的数据更新。
  • Post方法/payload需要包含在“doc”中。
# 示例:
POST my_index/_update/1
{
  "doc":{
    "user":"4",
    "comment":"search"
  }
}


{
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 5,
  "_primary_term" : 1
}
PUT my_index/_doc/1
{
  "user":"156",
  "comment":"you know,for search"
}

GET /_cat/indices

PUT my_index/_create/3
{
  "user":"2",
  "comment":"test"
}

POST my_index/_doc
{
  "user":"2",
  "comment":"test"
}

GET my_index/_doc/1

POST my_index/_update/1
{
  "doc":{
    "user":"4",
    "comment":"search"
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值