0
点赞
收藏
分享

微信扫一扫

关于elasticsearch用_update局部更新失败的问题

律楷粑粑 2022-04-02 阅读 48

关于elasticsearch用_update局部更新失败的问题


错误信息


ElasticSearch部分更新

根据文档学习ElasticSearch的Rest风格对索引增删改,在更新部分,如果不设置某些值,会将不修改的值清空。因为文档是不可变的,不能更改,只能被替换。所以修改文档只是用新的文档,覆盖现有文档中,所以就是修改会把没有提及的值清空的原因。

Elastic Search提供了局部更新_update 关键字。

测试

  • 新增测试数据

POST test/_doc/001
{
  "nick": "及时雨", 
  "name": "宋江"
}
GET /test/_search

结果

{
  "took" : 647,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test",
        "_id" : "001",
        "_score" : 1.0,
        "_source" : {
          "nick" : "及时雨",
          "name" : "曹操"
        }
      }
    ]
  }
}
  • 修改name

POST /test/_doc/001/_update
{
  "doc": {
    "name": "宋江"
  }
}

结果

{
  "error" : "no handler found for uri [/test/_doc/001/_update?pretty=true] and method [POST]"
}

elasticsearch8.0之后对一些API进行修改,正确的URI如下

POST /test/_update/001
{
  "doc": {
    "name": "宋江"
  }
}

结果

{
  "_index" : "test",
  "_id" : "001",
  "_version" : 5,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 1
}
举报

相关推荐

0 条评论