ElasticSearch 修改

普通修改

POST /target>/_update/<_id>

name字段没有会自动添加字段,如果修改内容和原来一样返回noop而且 _version不变

POST /movies/_update/1
{
    "doc" : {
        "name" : "new_name",
        "year":1996
    }
}

返回结果
{
  "_index" : "movies",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 4,
  "result" : "noop",
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "failed" : 0
  },
  "_seq_no" : 9753,
  "_primary_term" : 10
}

PUT //_doc/<_id>

PUT /movies/_doc/111220
{
  "year" : 1995,
    "title" : "Dracula: Dead and Loving It1", 
    "id" : "12",
    "genre" : [
      "Comedy"
    ]
}

id存在,则修改_version+1

脚本修改

POST movies/_update/1
{
    "script" : {
        "source": "ctx._source.genre.add(params.genre)",
        "lang": "painless",
        "params" : {
            "genre" : "Comedy1"
        }
    }
}

实现往集合中添加元素
POST movies/_update/2
{
    "script" : {
        "source": "if (ctx._source.genre.contains(params.genre)) { ctx._source.genre.remove(ctx._source.genre.indexOf(params.genre)) }",
        "lang": "painless",
        "params" : {
            "genre" : "Children"
        }
    }
}
删除数组中的某一个数据 脚本的if判断
POST movies/_update/1
{
    "script" : "ctx._source.remove('genre')"
}

脚本删除filed
POST movies/_update/1
{
    "script" : "ctx._source.new_field = 'value_of_new_field'"
}

添加 filed 名称 new_field,value value_of_new_field
posted @ 2021-03-11 09:19  RoinBi  阅读(220)  评论(0编辑  收藏  举报