首页  

elasticsearch5.0文档更新API     所属分类 elasticsearch 浏览量 2107
根据原文翻译整理
https://www.elastic.co/guide/en/elasticsearch/reference/5.0/docs-update.html



update a document based on a script provided. 
可以使用脚本更新

The operation gets the document (collocated with the shard) from the index, 
runs the script (with optional script language and parameters), 
and index back the result (also allows to delete, or ignore the operation).

读取文档,执行脚本(可选的脚本语言 和 参数) 重新索引 
it uses versioning to make sure no updates have happened during the "get" and "reindex".
使用版本机制确保读取和重新索引操作期间没有更新操作

Note, this operation still means full reindex of the document, 
it just removes some network roundtrips and reduces 
chances of version conflicts between the get and the index. 
The _source field needs to be enabled for this feature to work.

这个操作对文档进行完全的重新索引,它只是去掉了一些网络往返,减少了读取和索引之间版本冲突的机会。
需要启用_source字段才能使该特性正常工作。

PUT test/type1/1
{
    "counter" : 1,
    "tags" : ["red"]
}


POST test/type1/1/_update
{
    "script" : {
        "inline": "ctx._source.counter += params.count",
        "lang": "painless",
        "params" : {
            "count" : 4
        }
    }
}

列表添加数据
We can add a tag to the list of tags 
(note, if the tag exists, it will still add it, since its a list):


POST test/type1/1/_update
{
    "script" : {
        "inline": "ctx._source.tags.add(params.tag)",
        "lang": "painless",
        "params" : {
            "tag" : "blue"
        }
    }
}

In addition to _source, the following variables are available through the ctx map:
 _index, _type, _id, _version, _routing, _parent, and _now (the current timestamp).

ctx map
 
 
新增一个字段
add a new field to the document

POST test/type1/1/_update
{
    "script" : "ctx._source.new_field = \"value_of_new_field\""
}

删除一个字段
remove a field from the document

POST test/type1/1/_update
{
    "script" : "ctx._source.remove(\"new_field\")"
}

基于条件的操作 
This example deletes the doc if the tags field contain green, otherwise it does nothing (noop):
删除tasg包含green的文档

POST test/type1/1/_update
{
    "script" : {
        "inline": "if (ctx._source.tags.contains(params.tag)) { ctx.op = \"delete\" } else { ctx.op = \"none\" }",
        "lang": "painless",
        "params" : {
            "tag" : "green"
        }
    }
}

合并更新  只传需要更新的字段

passing a partial document, which will be merged into the existing document 
(simple recursive merge, inner merging of objects, replacing core "keys/values" and arrays). 
 
POST test/type1/1/_update
{
    "doc" : {
        "name" : "new_name"
    }
}



Detecting noop updates
If doc is specified its value is merged with the existing _source. 
By default updates that don’t change anything detect that they don’t change anything 
and return "result": "noop" like this:

POST test/type1/1/_update
{
    "doc" : {
        "name" : "new_name"
    }
}
更新检测,文档没有变更时 返回 noop 

{
   "_shards": {
        "total": 0,
        "successful": 0,
        "failed": 0
   },
   "_index": "test",
   "_type": "type1",
   "_id": "1",
   "_version": 6,
   "result": noop
}

"result": noop

disable this behavior by setting "detect_noop": false
POST test/type1/1/_update
{
    "doc" : {
        "name" : "new_name"
    },
    "detect_noop": false
}

upsert


POST test/type1/1/_update
{
    "script" : {
        "inline": "ctx._source.counter += params.count",
        "lang": "painless",
        "params" : {
            "count" : 4
        }
    },
    "upsert" : {
        "counter" : 1
    }
}

文档不存在时 counter 设置为 1 

If the document does not already exist, 
the contents of the upsert element will be inserted as a new document. 
If the document does exist, then the script will be executed instead

文档不存在, upsert 元素作为一个新文档新建
文档存在则执行脚本

scripted_upsert


doc_as_upsert

将doc_as_upsert设置为true将使用doc的内容作为upsert值

POST test/type1/1/_update
{
    "doc" : {
        "name" : "new_name"
    },
    "doc_as_upsert" : true
}

存在部分更新 不存在则Insert 

参数 

The update operation supports the following query-string parameters:

retry_on_conflict 设置版本冲突的重试次数
routing
parent
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.
更新操作成功的副本数 ,默认主副本更新成功即可
refresh   Control when the changes made by this request are visible to search.
_source   返回结果是否包含source字段
version  version_type  force

By setting version type to force you can force the new version of the document after update 
(use with care! with force there is no guarantee the document didn’t change).
可以把 version_type  设置为 force , 可能为导致更新丢失 ,小心使用 

乐观锁  内部 外部 锁 , 强制更新version 






  




The update API does not support external versioning
External versioning (version types external & external_gte) is not supported by the update API as 
it would result in Elasticsearch version numbers being out of sync with the external system.


更新API不支持外部版本控制(版本类型 External & external_gte)
这将导致版本号与外部系统不同步。

上一篇     下一篇
elasticsearch5.0文档删除API

kafka这些年

elasticsearch5.0文档查询删除API

elasticsearch5.0文档查询更新API

elasticsearch5.0批量读取API

elasticsearch5.0批量更新API