如何使用Elasticsearch groovy script脚本更新数据
2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
如何使用Elasticsearch groovy script腳本更新數(shù)據(jù) 博客分類: 搜索引擎,爬蟲今天細(xì)說一下elasticsearch的update更新功能,以及如何利用script腳本更新數(shù)據(jù)。
?
想要使用script腳本功能,需要在配置文件elasticsearch.yml里設(shè)置
?
Python
script.disable_dynamic: false script.disable_dynamic:false?
關(guān)于elasticsearch script的文章,總是會沒完沒了的修改,為毛? ?瞎問 ! 點擊原文鏈接查看更新后的文章:
?
http://xiaorui.cc/?p=2368
?
http://xiaorui.cc/2015/11/20/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8elasticsearch-groovy-script%E8%84%9A%E6%9C%AC%E6%9B%B4%E6%96%B0%E6%95%B0%E6%8D%AE/
?
ES支持更新,但是更新的方式是通過一個提供的腳本進(jìn)行的。ES的做法是,通過index找到相應(yīng)的存放記錄的節(jié)點,然后執(zhí)行腳本,執(zhí)行完之 后,返回新的索引。實際上執(zhí)行的是一個get和reindex的過程,在這個過程中,通過versioning來控制沒有其它的更新操作(這個功能是 0.19后可用的)。具體實現(xiàn)的原理應(yīng)該和elasticsearch Versioning相關(guān)。
?
get,reindex的含義是,ES先取出這條記錄,然后根據(jù)新數(shù)據(jù)生成新記錄,然后在把新記錄放回到ES中(并不會覆蓋老的記錄)。
?
現(xiàn)在沒有數(shù)據(jù),首先我們需要創(chuàng)建一條記錄
?
Python
$ curl -XPUT localhost:9200/xiaorui.cc/blog/1 -d '{"counter" : 1,"tags" : ["red"] }' $curl-XPUTlocalhost:9200/xiaorui.cc/blog/1-d'{"counter" : 1,"tags" : ["red"] }'?
直接修改數(shù)據(jù),一定要注意,直接update的化,會覆蓋以前的數(shù)據(jù),另外update的時候,需要/index/type/id ,一定要帶著id。 elasticsearch 應(yīng)該不支持搜索query方式update修改數(shù)據(jù)。?
?
Python
curl -XPUT 'localhost:9200/xiaorui.cc/blog/1?pretty' -d ' {"name": "xiaorui.cc" }' curl-XPUT'localhost:9200/xiaorui.cc/blog/1?pretty'-d' {"name": "xiaorui.cc" }'?
elasticsearch提供了doc這個局部更新參數(shù),他可以局部修改,而不會直接覆蓋以前的數(shù)據(jù),這會針對特定的k v,字段修改。?
?
Python
curl -XPOST 'localhost:9200/xiaorui.cc/blog/1/_update?pretty' -d ' {"doc": { "name": "ruifengyun" } }' curl-XPOST'localhost:9200/xiaorui.cc/blog/1/_update?pretty'-d' {"doc": { "name": "ruifengyun" } }'?
當(dāng)Elasticsearch?API不能滿足要求時,Elasticsearch允許你使用腳本實現(xiàn)自己的邏輯。腳本支持非常多的API,例如搜索、排序、聚合和文檔更新。腳本可以通過請求的一部分、檢索特殊的.scripts索引或者從磁盤加載方式執(zhí)行。
?
下面是es script的用法,這些腳本是groovy開發(fā)的。 下面的語句的意思是說,將counter的值加4
?
Python
$ curl -XPOST 'localhost:9200/xiaorui.cc/blog/1/_update' -d '{"script" : "ctx._source.counter += count","params" : {"count" : 4} }' $curl-XPOST'localhost:9200/xiaorui.cc/blog/1/_update'-d'{"script" : "ctx._source.counter += count","params" : {"count" : 4} }'?
通過上面的例子,我們知道tags是個列表,如果用doc局部更新的語法,他是無法做到append的,還是會覆蓋tags這個字段。 ?那么怎么實現(xiàn)列表擴(kuò)展? ?請使用elasticsearch script實現(xiàn)。?
?
Python
$ curl -XPOST 'localhost:9200/xiaorui.cc/blog/1/_update' -d '{"script" : "ctx._source.tags += tag","params" : {"tag" : "white"} }' $curl-XPOST'localhost:9200/xiaorui.cc/blog/1/_update'-d'{"script" : "ctx._source.tags += tag","params" : {"tag" : "white"} }'?
_update也支持upsert功能,沒有這個字段或者key,也會添加這個記錄。下面是一個例子,如果沒有counter字段,則插入該字段:
?
Python
$ curl -XPOST 'localhost:9200/xiaorui.cc/blog/1/_update' -d '{"script" : "ctx._source.counter += count","params" : {"count" : 4},"upsert" : {"counter" : 1} }' $curl-XPOST'localhost:9200/xiaorui.cc/blog/1/_update'-d'{ "script" : "ctx._source.counter += count", "params" : { "count" : 4 }, "upsert" : { "counter" : 1 } }'?
下面我們來復(fù)雜點的groovy script腳本用法. ?當(dāng)你的source沒有china這個key,那么我會增加一個kv
?
Python
curl -XPOST "http://localhost:9200/xiaorui.cc/blog/80/_update" -d' { "script": "if (!ctx._source.containsKey(\"china\")) { ctx._source.attending = newField }","params" : {"newField" : "blue" },"myfield": "data" }' curl-XPOST"http://localhost:9200/xiaorui.cc/blog/80/_update"-d' { "script": "if (!ctx._source.containsKey(\"china\")) { ctx._source.attending = newField }","params" : {"newField" : "blue" },"myfield": "data" }'?
下面的script語法相對復(fù)雜的,會遍歷一組字典,然后進(jìn)行判斷賦值。
?
{
“55555″: 22,
“name”: “l(fā)isi”,
“distr_pan”: [
{
“k”: 15,
“v”: 15
},
{
“k”: 20,
“v”: 20
}
]
}
?
Python
$ curl -XPUT 'localhost:9200/xiaorui.cc/blog/9123/_update' -d ' {"script" : "def x = false;ctx._source.distr_pan.each({if(it.get('k')==target){x=true}});if(x){ctx._source.distr_pan +=v}","params":{"v":{"k":nlp, "v":35},"target":15} } $curl-XPUT'localhost:9200/xiaorui.cc/blog/9123/_update'-d' { "script" : "def x = false;ctx._source.distr_pan.each({if(it.get('k')==target){x=true}});if(x){ctx._source.distr_pan+=v}", "params":{ "v":{"k":nlp, "v":35},"target":15 } }elasticsearch script就講解到這里了,很多例子已經(jīng)簡單明了… ? script貌似不是很安全,最少遠(yuǎn)程代碼執(zhí)行的漏洞暴露過幾次了. 下次把python版的script走一遍試試. ?貌似對于我們你者來說,不管是groovy python,沒什么太大卻別,語法看起來都一個模子
?
https://www.elastic.co/guide/en/elasticsearch/reference/1.7/modules-scripting.html#_stored_fields
https://www.elastic.co/guide/en/elasticsearch/reference/1.7/docs-update.html#_literal_doc_as_upsert_literal
轉(zhuǎn)載于:https://my.oschina.net/xiaominmin/blog/1599098
總結(jié)
以上是生活随笔為你收集整理的如何使用Elasticsearch groovy script脚本更新数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Kubernetes学习笔记(一)
- 下一篇: hdu2045