1. index api 更新全部文档
curl -X PUT "192.168.94.151:9200/user/_doc/2" -H 'Content-Type: application/json' -d'
{
"name": "CTO",
"description": "首席技术官",
"enabled": false
}
'
{
"_index": "user",
"_type": "_doc",
"_id": "2",
"_version": 2,
"result": "updated",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 4
}
更新后_version会加1,如果有的字段没有提供,更新后会为空
2. Scripted updates
脚本更新,里面执行js脚本,可以只更新某个字段,可以添加field,删除field等。
由于更新要获得现有文档的_source内容,因此脚本会修改_source字段。使用ctx._source来引用_source,使用ctx._source[字段名]或ctx._source.字段名来引用某个指定的字段。
如果需要变量,推荐在params下作为参数单独定义,和脚本本身区分开来。因为脚本需要编译,一旦编译完成,就会被缓存。如果使用不同的参数,多次运行相同的脚本,脚本只需要编译一次。
index: people, type:_doc,id:1的数据,age+1
curl -X POST "192.168.94.151:9200/people/_doc/1/_update" -H 'Content-Type: application/json' -d'
{
"script" : {
"source": "ctx._source.age += params.count",
"lang": "painless",
"params" : {
"count" : 1
}
}
}
'
{
"_index": "people",
"_type": "_doc",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 2
}
数据操作:
添加一个数组
curl -X POST "192.168.94.151:9200/people/_doc/1/_update" -H 'Content-Type: application/json' -d'
{
"script" : {
"source": "ctx._source.tags=params.tag",
"lang": "painless",
"params" : {
"tag" : ["blue"]
}
}
}
'
这样也可以,比较简洁
{
"script" : "ctx._source.new_field = 'value_of_new_field'"
}
向数组中添加数据,请求体:
{
"script" : {
"source": "ctx._source.tags.add(params.tag)",
"lang": "painless",
"params" : {
"tag" : "red"
}
}
}
删除数组中某个数据,请求体:
{
"script" : {
"source": "if (ctx._source.tags.contains(params.tag)) { ctx._source.tags.remove(ctx._source.tags.indexOf(params.tag)) }",
"lang": "painless",
"params" : {
"tag" : "blue"
}
}
}
移除一个属性:
{
"script" : "ctx._source.remove('new_field')"
}
根据条件执行不同的运算,如果tags包含green,就删除整个document,否则什么都不做
{
"script" : {
"source": "if (ctx._source.tags.contains(params.tag)) { ctx.op = 'delete' } else { ctx.op = 'none' }",
"lang": "painless",
"params" : {
"tag" : "green"
}
}
}
3. 更新部分文档
可以只更新部分属性,而index api是更新全部属性
curl -X POST "192.168.94.151:9200/people/_doc/2/_update" -H 'Content-Type: application/json' -d'
{
"doc" : {
"name" : "小花"
}
}
'
如果doc和script都指定,会忽略doc
如果更新前后document是一样的,会返回一个noop空操作
{
"_index": "people",
"_type": "_doc",
"_id": "2",
"_version": 4,
"result": "noop",
"_shards": {
"total": 0,
"successful": 0,
"failed": 0
}
}
如果不希望这种形式,detect_noop指定false,会和正常更新一样:
{
"doc" : {
"name" : "new_name"
},
"detect_noop": false
}
4. upserts
如果文档不存在执行upsert,文档存在执行script
curl -X POST "192.168.94.151:9200/people/_doc/1/_update" -H 'Content-Type: application/json' -d'
{
"script": {
"source": "ctx._source.age += params.age",
"lang": "painless",
"params": {
"age": 1
}
},
"upsert": {
"name": "小白",
"age": 18,
"description": "喜欢画画,学习"
}
}
'
不管document是否存在,都要执行script,scripted_upsert设置为true,如果不存在会upsert执行完执行script
{
"scripted_upsert": true,
"script": {
"source": "ctx._source.age += params.age",
"lang": "painless",
"params": {
"age": 1
}
},
"upsert": {
"name": "小灰",
"age": 18,
"description": "喜欢做饭"
}
}
更新部分文档时如果不存在就进行插入doc_as_upsert
curl -X POST "192.168.94.151:9200/people/_doc/7/_update" -H 'Content-Type: application/json' -d'
{
"doc": {
"name": "小花"
},
"doc_as_upsert": true
}
'
5.Parameters
The update operation supports the following query-string parameters:
retry_on_conflict | In between the get and indexing phases of the update, it is possible that another process might have already updated the same document. By default, the update will fail with a version conflict exception. The retry_on_conflict parameter controls how many times to retry the update before finally throwing an exception. |
|---|---|
routing | Routing is used to route the update request to the right shard and sets the routing for the upsert request if the document being updated doesn’t exist. Can’t be used to update the routing of an existing document. |
timeout | Timeout waiting for a shard to become available. |
wait_for_active_shards | The number of shard copies required to be active before proceeding with the update operation. See here for details. |
refresh | Control when the changes made by this request are visible to search. See ?refresh. |
_source | Allows to control if and how the updated source should be returned in the response. By default the updated source is not returned. See source filtering for details. |
version | The update API uses the Elasticsearch’s versioning support internally to make sure the document doesn’t change during the update. You can use the version parameter to specify that the document should only be updated if its version matches the one specified. |