侧边栏壁纸
博主头像
福福不服博主等级

孩子会穿过大雨,去懂人间的道理。

  • 累计撰写 77 篇文章
  • 累计创建 91 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Elasticsearch更新文档

Monster
2023-07-26 / 0 评论 / 4 点赞 / 31 阅读 / 12143 字 / 正在检测是否收录...
温馨提示:
请确保在评论和互动中保持礼貌和尊重。避免使用侮辱性、歧视性或攻击性语言。我们鼓励建设性的讨论和意见交流。

Elasticsearch更新文档

POST更新

查询当前ffbf索引信息

GET /ffbf/_search

返回信息

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "ffbf",
        "_type" : "_doc",
        "_id" : "123",
        "_score" : 1.0,
        "_source" : {
          "name" : "Monster",
          "age" : 26,
          "address" : "天安云谷xx栋xx层",
          "url" : "ffbf.top"
        }
      },
      {
        "_index" : "ffbf",
        "_type" : "_doc",
        "_id" : "SQMeaYkB24MJMrhTgLUX",
        "_score" : 1.0,
        "_source" : {
          "name" : "Helen",
          "age" : 20,
          "address" : "天安云谷xx栋xx层",
          "url" : "qqbq.top"
        }
      },
      {
        "_index" : "ffbf",
        "_type" : "_doc",
        "_id" : "SgMgaYkB24MJMrhTTrUM",
        "_score" : 1.0,
        "_source" : {
          "name" : "Helen",
          "age" : 20,
          "address" : "天安云谷xx栋xx层",
          "url" : "qqbq.top"
        }
      },
      {
        "_index" : "ffbf",
        "_type" : "_doc",
        "_id" : "456",
        "_score" : 1.0,
        "_source" : {
          "name" : "Monster",
          "age" : 26,
          "address" : "天安云谷xx栋xx层",
          "url" : "ffbf.top"
        }
      }
    ]
  }
}

执行更新操作

_update/后面跟上要更新的id,需要在doc里面指定更新字段

POST /ffbf/_update/123
{
  "doc": {
    "url":"https://ffbf.top"
  }
}

返回信息,状态updated

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

查询一下被更新的文档信息

GET /ffbf/_doc/123

返回信息,url数据已经变更了

{
  "_index" : "ffbf",
  "_type" : "_doc",
  "_id" : "123",
  "_version" : 3,
  "_seq_no" : 4,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "Monster",
    "age" : 26,
    "address" : "天安云谷xx栋xx层",
    "url" : "https://ffbf.top"
  }
}

注意:如果更新操作重复执行或更新的数据没有变动则状态为noop不执行操作

状态如下

{
  "_index" : "ffbf",
  "_type" : "_doc",
  "_id" : "123",
  "_version" : 3,
  "result" : "noop",
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 1
}

POST添加新字段

直接在doc里面指定不存在的字段即可,比如价格爱好字段

POST /ffbf/_update/123
{
  "doc": {
    "hobby":"羽毛球、爬山、写博客、看美女"
  }
}

返回信息

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

查询文档信息GET /ffbf/_doc/123

{
  "_index" : "ffbf",
  "_type" : "_doc",
  "_id" : "123",
  "_version" : 4,
  "_seq_no" : 5,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "Monster",
    "age" : 26,
    "address" : "天安云谷xx栋xx层",
    "url" : "https://ffbf.top",
    "hobby" : "羽毛球、爬山、写博客、看美女"
  }
}

POST使用脚本更新

使用script脚本更新年龄+10,使用++、--、=都行

赋值的时候如果字段不存在则会创建

添加:_source.add

删除:_source.remove

POST /ffbf/_update/123
{
  "script":{
    "source": "ctx._source.age+=10"
  }
}

返回信息,updated

{
  "_index" : "ffbf",
  "_type" : "_doc",
  "_id" : "123",
  "_version" : 5,
  "result" : "updated",
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "failed" : 0
  },
  "_seq_no" : 6,
  "_primary_term" : 1
}

查询一下现在123文档的信息GET /ffbf/_doc/123,age加了10

{
  "_index" : "ffbf",
  "_type" : "_doc",
  "_id" : "123",
  "_version" : 5,
  "_seq_no" : 6,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "Monster",
    "age" : 36,
    "address" : "天安云谷xx栋xx层",
    "url" : "https://ffbf.top",
    "hobby" : "羽毛球、爬山、写博客、看美女"
  }
}

数组添加删除稍微麻烦一点

比如现在插入了一个girlfriend数组

{
  "_index" : "ffbf",
  "_type" : "_doc",
  "_id" : "123",
  "_version" : 6,
  "_seq_no" : 7,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "Monster",
    "age" : 36,
    "address" : "天安云谷xx栋xx层",
    "url" : "https://ffbf.top",
    "hobby" : "羽毛球、爬山、写博客、看美女",
    "girlfriend" : [
      "女友1",
      "女友2",
      "女友3"
    ]
  }
}

使用脚本添加一个女友4

POST /ffbf/_update/123
{
  "script": {
    "source": "ctx._source.girlfriend.add('女友4')"
  }
}

返回信息

{
  "_index" : "ffbf",
  "_type" : "_doc",
  "_id" : "123",
  "_version" : 7,
  "result" : "updated",
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "failed" : 0
  },
  "_seq_no" : 8,
  "_primary_term" : 1
}

查询文档信息GET /ffbf/_doc/123

{
  "_index" : "ffbf",
  "_type" : "_doc",
  "_id" : "123",
  "_version" : 7,
  "_seq_no" : 8,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "Monster",
    "age" : 36,
    "address" : "天安云谷xx栋xx层",
    "url" : "https://ffbf.top",
    "hobby" : "羽毛球、爬山、写博客、看美女",
    "girlfriend" : [
      "女友1",
      "女友2",
      "女友3",
      "女友4"
    ]
  }
}

使用脚本删除女友1

POST /ffbf/_update/123
{
  "script": {
    "source": "ctx._source.girlfriend.remove(ctx._source.girlfriend.indexOf('女友1'))"
  }
}

返回信息

{
  "_index" : "ffbf",
  "_type" : "_doc",
  "_id" : "123",
  "_version" : 8,
  "result" : "updated",
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "failed" : 0
  },
  "_seq_no" : 9,
  "_primary_term" : 1
}

查询文档信息GET /ffbf/_doc/123,女友1已被删除

{
  "_index" : "ffbf",
  "_type" : "_doc",
  "_id" : "123",
  "_version" : 8,
  "_seq_no" : 9,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "Monster",
    "age" : 36,
    "address" : "天安云谷xx栋xx层",
    "url" : "https://ffbf.top",
    "hobby" : "羽毛球、爬山、写博客、看美女",
    "girlfriend" : [
      "女友2",
      "女友3",
      "女友4"
    ]
  }
}

POST条件脚本更新

使用3双引号,判断女友数组长度,给文档新增message字段并相应赋值

POST /ffbf/_update/123
{
  "script":{
    "source": """
      if (ctx._source.girlfriend.length > 1) {
          ctx._source.message="这是个渣男!!!"
      }else{
          ctx._source.message="这人挺专一!!!"
      }
    """
  }
}

返回信息,新增了message

{
  "_index" : "ffbf",
  "_type" : "_doc",
  "_id" : "123",
  "_version" : 11,
  "_seq_no" : 12,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "Monster",
    "age" : 36,
    "address" : "天安云谷xx栋xx层",
    "url" : "https://ffbf.top",
    "hobby" : "羽毛球、爬山、写博客、看美女",
    "girlfriend" : [
      "女友2",
      "女友3",
      "女友4"
    ],
    "message" : "这是个渣男!!!"
  }
}

POST脚本传参更新

使用params定义变量

每次执行年龄age+1

POST /ffbf/_update/123
{
  "script":{
    "source": "ctx._source.age += params.num",
    "params":{
      "num":1
    }
  }
}

查询文档GET /ffbf/_doc/123

{
  "_index" : "ffbf",
  "_type" : "_doc",
  "_id" : "123",
  "_version" : 12,
  "_seq_no" : 13,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "Monster",
    "age" : 37,
    "address" : "天安云谷xx栋xx层",
    "url" : "https://ffbf.top",
    "hobby" : "羽毛球、爬山、写博客、看美女",
    "girlfriend" : [
      "女友2",
      "女友3",
      "女友4"
    ],
    "message" : "这是个渣男!!!"
  }
}

POST使用Upsert更新插入

原理:当文档不存在就会创建,存在就执行脚本

如下110是不存在的文档,应该会创建一个

POST /ffbf/_update/110
{
  "script": {
    "source": "ctx._source.age++"
  },
  "upsert": {
    "age": 99,
    "name":"monster",
    "hobby":["吃人肉","养身","xxxx","xxxx","xxxx"]
  }
}

返回信息,状态created

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

查询一下110文档

GET /ffbf/_doc/110

返回信息

{
  "_index" : "ffbf",
  "_type" : "_doc",
  "_id" : "110",
  "_version" : 1,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "age" : 99,
    "name" : "monster",
    "hobby" : [
      "吃人肉",
      "养身",
      "xxxx",
      "xxxx",
      "xxxx"
    ]
  }
}

如果再执行,按理应该年龄+1,执行更新操作了

POST /ffbf/_update/110
{
  "script": {
    "source": "ctx._source.age++"
  },
  "upsert": {
    "age": 99,
    "name":"monster",
    "hobby":["吃人肉","养身","xxxx","xxxx","xxxx"]
  }
}

返回信息,状态updated

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

查询一下文档GET /ffbf/_doc/110,age变成100了

{
  "_index" : "ffbf",
  "_type" : "_doc",
  "_id" : "110",
  "_version" : 2,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "age" : 100,
    "name" : "monster",
    "hobby" : [
      "吃人肉",
      "养身",
      "xxxx",
      "xxxx",
      "xxxx"
    ]
  }
}

POST使用查询更新

通过_update_by_query,查询所有文档,给所有文档的age加100

POST /ffbf/_update_by_query
{
  "script": {
    "source": "ctx._source.age += 100",
    "lang": "painless"
  },
  "query": {"match_all": {}}
}

返回信息,updated了5个

{
  "took" : 13972,
  "timed_out" : false,
  "total" : 5,
  "updated" : 5,
  "deleted" : 0,
  "batches" : 1,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}

查询一下ffbf索引GET /ffbf/_search,可以看到所有文档的age都加了100

{
  "took" : 1387,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "ffbf",
        "_type" : "_doc",
        "_id" : "SQMeaYkB24MJMrhTgLUX",
        "_score" : 1.0,
        "_source" : {
          "address" : "天安云谷xx栋xx层",
          "name" : "Helen",
          "age" : 120,
          "url" : "qqbq.top"
        }
      },
      {
        "_index" : "ffbf",
        "_type" : "_doc",
        "_id" : "SgMgaYkB24MJMrhTTrUM",
        "_score" : 1.0,
        "_source" : {
          "address" : "天安云谷xx栋xx层",
          "name" : "Helen",
          "age" : 120,
          "url" : "qqbq.top"
        }
      },
      {
        "_index" : "ffbf",
        "_type" : "_doc",
        "_id" : "123",
        "_score" : 1.0,
        "_source" : {
          "girlfriend" : [
            "女友2",
            "女友3",
            "女友4"
          ],
          "address" : "天安云谷xx栋xx层",
          "name" : "Monster",
          "message" : "这是个渣男!!!",
          "age" : 137,
          "url" : "https://ffbf.top",
          "hobby" : "羽毛球、爬山、写博客、看美女"
        }
      },
      {
        "_index" : "ffbf",
        "_type" : "_doc",
        "_id" : "456",
        "_score" : 1.0,
        "_source" : {
          "address" : "天安云谷xx栋xx层",
          "name" : "Monster",
          "age" : 126,
          "url" : "ffbf.top"
        }
      },
      {
        "_index" : "ffbf",
        "_type" : "_doc",
        "_id" : "110",
        "_score" : 1.0,
        "_source" : {
          "name" : "monster",
          "age" : 200,
          "hobby" : [
            "吃人肉",
            "养身",
            "xxxx",
            "xxxx",
            "xxxx"
          ]
        }
      }
    ]
  }
}
4
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin
  3. QQ打赏

    qrcode qq

评论区