_update API 应用
update API 允许基于提供的脚本更新文档。
1、新增字段
2、删除字段
3、修改字段值
新增字段
#new 是新字段,等号后面就是赋予的值 POST test/_doc/1/_update { "script": "ctx._source.new = 'value_of_new_field'" } #date是新增字段,等号后面是赋予该字段的值 POST test/_doc/1/_update { "script": "ctx._source.date = '2019-10-31 15:35:40'" }
POST test/_doc/1/_update { "doc":{"name":"new_name"} }
也能新增字段 ,字段名为name,值为new_name
删除字段
#移除了new这个字段 POST test/_doc/1/_update { "script": "ctx._source.remove('new')" }
修改字段值
PUT test/_doc/1 { "counter":1, #数值型 "tags":["red"] #字符型 } 数值型字段,修改 POST test/_doc/1/_update { "script": { "source":"ctx._source.counter += params.count", "lang":"painless", "params": {"count":4} } }
字符数组增加字段
POST test/_doc/1/_update { "script": { "source":"ctx._source.tags.add(params.tag)", "lang": "painless", "params": { "tag":"blue" } } }
#如果文档存在,则执行脚本内容。如果文档不存在,则执行upsert内容 POST test/_doc/1/_update { "script": { "source":"ctx._source.counter += params.count", "lang": "painless", "params": {"count":10} }, "upsert": { "counter":1 } }