ElasticSearch API 之 UPDATE
ES本身是一个倾向于查询检索的框架,对于更新的操作,太过频繁总归不好的。
阅读本篇后,你可以使用Script对所有的文档执行更新操作,也可以使用doc对部分文档执行更新,也可以使用upsert对不存在的文档执行添加操作。参考:http://www.cnblogs.com/xing901022/p/5330778.html
更新
更新操作允许ES获得某个指定的文档,可以通过脚本等操作对该文档进行更新。
可以把它看成是先删除再索引的原子操作,只是省略了返回的过程,这样即节省了来回传输的网络流量,也避免了中间时间造成的文档修改冲突。
下面例子,查找id为25的文档:
curl -XGET localhost:9200/bank/account/25?pretty{
- "_index": "bank",
- "_type": "account",
- "_id": "25",
- "_version": 1,
- "found": true,
- "_source": {
- "account_number": 25,
- "balance": 40540,
- "firstname": "Virginia",
- "lastname": "Ayala",
- "age": 39,
- "gender": "F",
- "address": "171 Putnam Avenue",
- "employer": "Filodyne",
- "email": "virginiaayala@filodyne.com",
- "city": "Nicholson",
- "state": "PA"
}
}
脚本更新
Es支持通过脚本更改文档的信息:
curl -XPOST 'localhost:9200/bank/account/25/_update' -d '{
"script" : {
"inline": "ctx._source.age += number",
"params" : {
"number" : 5
}
}
}'结果报错:{
- "error": {
- "root_cause": [
- {
- "type": "remote_transport_exception",
- "reason": "[lihao][127.0.0.1:9300][indices:data/write/update[s]]"
}
],
- {
- "type": "illegal_argument_exception",
- "reason": "failed to execute script",
- "caused_by": {
- "type": "script_exception",
- "reason": "scripts of type [inline], operation [update] and lang [groovy] are disabled"
}
},
- "root_cause": [
- "status": 400
}
报错原因: 在最新版本的Elasticsearch中,基于安全考虑(如果用不到,请保持禁用),默认禁用了动态脚本功能。完全开启动态脚本功能
script.inline: on
script.indexed: on
script.file: on 重启ES
{
- "_index": "bank",
- "_type": "account",
- "_id": "25",
- "_version": 2,
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
}
}
curl -XGET localhost:9200/bank/account/25?pretty
{
"_index" : "bank",
"_type" : "account",
"_id" : "25",
"_version" : 2,
"found" : true,
"_source" : {
"account_number" : 25,
"balance" : 40540,
"firstname" : "Virginia",
"lastname" : "Ayala",
"age" : 44,
"gender" : "F",
"address" : "171 Putnam Avenue",
"employer" : "Filodyne",
"email" : "virginiaayala@filodyne.com",
"city" : "Nicholson",
"state" : "PA"
}
}
上面就是通过参数来为age加5.
除了_source字段,可以通过ctx来获得_index、_type、_id、_version、_parent、_timestamp、_ttl等字段信息。
也可以添加某个字段:
curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"script" : "ctx._source.name_of_new_field = \"value_of_new_field\""
}'
移除字段:
curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"script" : "ctx._source.remove(\"name_of_field\")"
}'
也支持稍微复杂点的操作,逻辑判断,比如根据某个标记执行不同的操作。比如如果有blue这个标记,则删除该文档;否则什么也不做:
curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"script" : {
"inline": "ctx._source.tags.contains(tag) ? ctx.op = \"delete\" : ctx.op = \"none\"",
"params" : {
"tag" : "blue"
}
}
}'
只更新部分文档
上面的脚本是对所有的文档都起作用,这里讲解下如何只对部分文档进行修改。使用doc可以实现简单的递归合并、内部合并、替换KV以及数组。
curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"doc" : {
"name" : "new_name"
}
}'
如果同时使用了doc和script,那么doc的操作会自动忽略。因此最好是把特殊的操作也放在脚本中。
更新检测
如果使用doc,那么会自动合并到现有的文档中。如果doc中定义的部分与现在的文档相同,则默认不会执行任何动作。设置detect_noop=false,就会无视是否修改,强制合并到现有的文档。
curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"doc" : {
"name" : "new_name"
},
"detect_noop": false
}'
上面的例子中,如果name字段为new_name,无论当前的文档是否与doc中定义的相同,都会把doc合并到文档中。
upsert插入
这个参数主要用于当文档不存在时,ES的操作。
curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"script" : {
"inline": "ctx._source.counter += count",
"params" : {
"count" : 4
}
},
"upsert" : {
"counter" : 1
}
}'
在上面的例子中,当文档存在时,执行脚本;当文档不存在时,upsert中的内容就会插入到对应的文档中。
如果你想无论文档是否存在都执行脚本操作,那么可以使用参数scripted_upsert为true。
curl -XPOST 'localhost:9200/sessions/session/dh3sgudg8gsrgl/_update' -d '{
"scripted_upsert":true,
"script" : {
"id": "my_web_session_summariser",
"params" : {
"pageViewEvent" : {
"url":"foo.com/bar",
"response":404,
"time":"2014-01-01 12:32"
}
}
},
"upsert" : {}
}'
相对于之前的使用Upsert中的内容添加到不存在的文档,使用doc_as_upsert可以在文档不存在的时候,把doc中的内容插入到文档中。
curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"doc" : {
"name" : "new_name"
},
"doc_as_upsert" : true
}'ElasticSearch API 之 UPDATE的更多相关文章
- Python Elasticsearch api,组合过滤器,term过滤器,正则查询 ,match查询,获取最近一小时的数据
Python Elasticsearch api 描述:ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.下 ...
- Elasticsearch API响应的一些常用选项
我们可以点击Elasticsearch API以获取所需的响应,但是如果要修改API响应,以便我们更改显示格式或过滤掉某些字段,然后我们可以将这些选项与查询一起应用. 有一些常见的选项可以适用于API ...
- Python Elasticsearch api
描述:ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.下面介绍了利用Python API接口进行数据查询,方便 ...
- java优雅的使用elasticsearch api
本文给出一种优雅的拼装elasticsearch查询的方式,可能会使得使用elasticsearch的方式变得优雅起来,使得代码结构很清晰易读. 建立elasticsearch连接部分请参看另一篇博客 ...
- 用ASP.NET Core 2.0 建立规范的 REST API -- DELETE, UPDATE, PATCH 和 Log
本文所需的一些预备知识可以看这里: http://www.cnblogs.com/cgzl/p/9010978.html 和 http://www.cnblogs.com/cgzl/p/9019314 ...
- ElasticSearch API 简要介绍
调用其API会返回很多信息,例如集群的信息,节点的信息等 检查集群的状态----Restful API说明 1:检查集群状态信息 2:管理集群 3:执行 增删改查 命令 4:执行高级命令 Restfu ...
- ElasticSearch API 之 GET
GET API是Elasticsearch中常用的操作,一般用于验证文档是否存在:或者执行CURD中的文档查询.与检索不同的是,GET查询是实时查询,可以实时查询到索引结果.而检索则是需要经过处理才能 ...
- elasticsearch api约定
elasticsearch REST API 使用JSON通过HTTP协议传输. 本约定贯穿整个REST API,除非有特别的说明. 一.多重索引 大多数APIs引用到一个index参数来在多个索引中 ...
- Java调用Elasticsearch API查询及matchPhraseQuery和matchQuery的区别
一.引入依赖 <!--Elasticsearch client--> <!-- https://mvnrepository.com/artifact/org.elasticsearc ...
随机推荐
- UIButton zoomin pressed
// Scale up on button press - (void) buttonPress:(UIButton*)button { button.transform = CGAffineTran ...
- 使用python批量建立文件
for i in range(101,110): n = repr(i) + '.txt' file = open('c:\\ip\\' + n, 'w')
- Cairo Drawing Model
Cairo Drawing Model Cairo是一个强力的2D绘图库. Destination 是你最终绘图的目标, 可以是一系列Pixel或者绑定到SVG或PDF文件上. Source 是实际在 ...
- UVA 12549 Sentry Robots (最小点覆盖)
这道题挺像hdu 5093 Battle ships的,不过那道题是要求最多放置的点数,而这道题是要求最小点覆盖. 顶点覆盖的定义是:在G中任意边至少有一个端点属于顶点集合S. 一个重要的位置有(x, ...
- 万事先问『为什么』 what why how
万事先问『为什么』! 遇到问题时,很多人的行为模式顺序是,先问『做什么』,『怎么做』,他们从来不问『为什么』,他们对根源性问题很模糊. 而聪明人则是先问『为什么』,再去构建『怎么做』,而『做什么』就是 ...
- Objective-C相关Category的收集(更新)
Categories是给你得不到源码的classes增加功能的一种方法.这个页面收集一些相关的Category,并且持续更新,你可以订阅关注.作者是Fille ?str?m,是@ IMGNRY的联合创 ...
- js 判断是什么浏览器、是否为谷歌浏览器
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http ...
- Vue+webpack+echarts+jQuery=demo
需要的插件: "dependencies": { "bootstrap": "^3.3.7", "echarts": & ...
- python_112_断言
#断言 如果满足断言的执行程序,如果不满足则抛错误 assert type(1) is int print('断言正确的话,就继续执行') # assert type('a') is int #Ass ...
- ThinkPHP5.0-多语言切换
这两天做得项目中需要多语言切换,于是乎就看了看文档,感觉有些乱,就使用了终极必杀--百度. 借鉴了网上各位大佬所集成.整理出一篇比较适合类似我这种比较菜的随笔吧. 请各位大佬轻虐.感谢. 首先,不说其 ...