使用上篇文章創建的索引進行學習:https://www.cnblogs.com/wangymd/p/11200996.html
官方文檔:https://www.elastic.co/guide/en/elasticsearch/painless/6.3/painless-examples.html?
?
1、腳本更新指定字段
方式1:
POST test_index/test_type/4/_update
{
"script":{
"source":"ctx._source.count = 10"
}
}
方式2:
POST test_index/test_type/4/_update
{
"script":{
"source":"ctx._source.count = params.count",
"params": {
"count":20
}
}
}
POST test_index/test_type/4/_update
{
"script" : {
"source": "ctx._source.count++" #自增
}
}
POST test_index/test_type/4/_update
{
"script" : {
"source": "ctx._source.count--" #自減
}
}?
2、數組添加值
索引增加字段
PUT test_index/test_type/_mapping
{
"properties": {
"tags" : {
"type": "text"
}
}
}
索引字段設置數組值
POST test_index/test_type/4/_update
{
"doc": {
"tags":["aa"]
}
}
索引字段添加數組值
注意字段無數據時直接添加會發生錯誤。
POST test_index/test_type/4/_update
{
"script":{
"source":"ctx._source.tags.add(params.tags)",
"params": {
"tags":"bb"
}
}
}
3、添加字段
POST test_index/test_type/4/_update
{
"script" : "ctx._source.date = '2019-07-25'" #字段名和字段值
}
4、刪除字段
POST test_index/test_type/4/_update
{
"script" : "ctx._source.remove('date')"
}
5、復雜的腳本
①根據不同條件執行不同的命令
POST test_index/test_type/4/_update
{
"script" : {
"source": "if (ctx._source.tags.contains(params.tag)) { ctx.op = 'delete' } else { ctx.op = 'none' }", #tags包含aa"進行刪除",其他誤操作
"lang": "painless",
"params" : {
"tag" : "aa"
}
}
}
②
?