ElasticSearch 搜索引擎入门到实战 3--文档增删改查

新增文档用put并指定id,为防止覆盖已存在的文档,可以通过_create加以限制
用post,不指定id,会自动创建id

put方式操作 新增文档
PUT /nba/_doc/1
{
  "name": "哈登",
  "team_name": "⽕火箭",
  "position": "得分后卫",
  "play_year": "10",
  "jerse_no": "13"
}
或
PUT /nba/_doc/1/_create
{
  "name": "哈登",
  "team_name": "⽕火箭",
  "position": "得分后卫",
  "play_year": "10",
  "jerse_no": "13"
}

//post方式操作 新增文档
post /nba/_doc/
{
  "name": "库⾥里里",
  "team_name": "勇⼠士",
  "position": "组织后卫",
  "play_year": "10",
  "jerse_no": "30"
}


更新文档

PUT nba/_doc/1
{
  "name": "哈登--1",
  "team_name": "⽕火箭--1",
  "position": "得分后卫--1",
  "play_year": "10--1"
}


更新文档的字段
通过脚本更新制定字段,其中ctx是脚本语言中的一个执行对象,先获取_source,再修改name字段

//更新字段
POST nba/_doc/1/_update
{
  "script": {
    "source": "ctx._source.name=\"姓名-a\""  
  } 
}

//根据参数值,更新文档的指定的字段
POST nba/_doc/1/_update
{
  "script": {
    "source": "ctx._source.jerse_no+=params.age",
    "params": {
      "age": 4
    }
  }
}


添加字段author

POST nba/_doc/1/_update
{
  "script": {
    "source": "ctx._source.author=\"别名\""  
  } 
}


删除字段author

POST nba/_doc/1/_update
{
  "script": {
    "source": "ctx._source.remove(\"author\")"  
  } 
}

 删除文档

DELETE nba/_doc/1

通过文档id获取指定的文档
GET nba/_doc/1

获得多个文档

//方式1
get /_mget
{
  "docs": [
    {
      "_index": "nba",
      "_type": "_doc",
      "_id": 2
    },
    {
      "_index": "nba",
      "_type": "_doc",
      "_id": 1
    }
  ]
}

//方式2
get /nba/_mget
{
  "docs": [
    {
      "_type": "_doc",
      "_id": 2
    },
    {
      "_type": "_doc",
      "_id": 1
    }
  ]
}

//方式3
get /nba/_doc/_mget
{
  "docs": [
    {
      "_id": 2
    },
    {
      "_id": 1
    }
  ]
}

//方式4
get /nba/_doc/_mget
{
 "ids":["1","2"]
}

判定文档是否存在
HEAD nba/_doc/1


新增文档,响应体说明

{
  "_index" : "nba",   //文档所在的索引名
  "_type" : "_doc",   //文档所在的类型名
  "_id" : "1",        //文档ID
  "_version" : 1,     //文档的版本
  "result" : "created", //created已经创建
  "_shards" : {        //_shards表示索引操作的复制过程的信息。
    "total" : 2,       //指示应在其上执行索引操作的分片副本(主分片和副本分片)的数量。
    "successful" : 1,  //表示索引操作成功的分片副本数。
    "failed" : 0       //在副本分片上索引操作失败的情况下包含复制相关错误。
  },
  "_seq_no" : 0,
  "_primary_term" : 2
}

 返回文档,响应体说明

{
  "_index" : "nba",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,  //版本号,修改后版本号会递增
  "found" : true,  //表明查询到该文档
  "_source" : {    //文档的内容
   "name" : "哈登ss",
    "team_name" : "⽕火箭",
    "position" : "得分后卫",
    "play_year" : "10",
    "jerse_no" : "13"
  }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值