Dar*_*rkW 6 arrays append elasticsearch
我是ES的新手,但我已经掌握了它.它是一个非常强大的软件,但我不得不说文档确实缺乏并且有时令人困惑.
这是我的问题:我有一个整数数组,如下所示:
"hits_history" : [0,0]
Run Code Online (Sandbox Code Playgroud)
我想通过一个"update_by_query"呼吁追加到数组的整数,我搜索,发现这个链接:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html 其中有这个例子:
POST test/type1/1/_update
{
"script" : {
"inline": "ctx._source.tags.add(params.tag)",
"lang": "painless",
"params" : {
"tag" : "blue"
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以我试过了:
curl -XPOST 'localhost:9200/example/example/_update_by_query?pretty' -H 'Content-Type: application/json' -d'
{
"script": {
"inline": "ctx._source.hits_history.add(params.hits)",
"params": {"hits": 0}
},
"query": {
"match_all": {}
}
}
'
Run Code Online (Sandbox Code Playgroud)
但它给了我这个错误:
"ctx._source.hits_history.add(params.hits); ",
" ^---- HERE"
"type" : "script_exception",
"reason" : "runtime error",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "Unable to find dynamic method [add] with [1] arguments for class [java.lang.Integer]."
Run Code Online (Sandbox Code Playgroud)
所以,我进一步观察并发现了这个:https://www.elastic.co/guide/en/elasticsearch/guide/current/partial-updates.html
这个例子有:
我们还可以使用脚本向tags数组添加新标记.
POST /website/blog/1/_update
{
"script" : "ctx._source.tags+=new_tag",
"params" : {
"new_tag" : "search"
}
}
Run Code Online (Sandbox Code Playgroud)
所以我试了一下:
curl -XPOST 'localhost:9200/example/example/_update_by_query?pretty' -H 'Content-Type: application/json' -d'
{
"script": {
"inline": "ctx._source.hits_history += 0;"
},
"query": {
"match_all": {}
}
}
'
Run Code Online (Sandbox Code Playgroud)
结果:
"type" : "script_exception",
"reason" : "runtime error",
"caused_by" : {
"type" : "class_cast_exception",
"reason" : "Cannot apply [+] operation to types [java.util.ArrayList] and [java.lang.Integer]."
Run Code Online (Sandbox Code Playgroud)
那么,我如何将项目附加到arrayList?我应该研究一下更新的文档吗?
我想做的只是这样的事情:
ctx._source.hits_history.add(ctx._source.today_hits);
ctx._source.today_hits = 0;
谢谢
您应该将第一个值存储为数组(包含一个值).然后你可以使用add()方法.
POST /website/blog/1/_update
{
"script" : "if (ctx._source.containsKey('tags')) { ctx._source.tags.add('next') } else { ctx._source.tags = ['first'] }"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1536 次 |
| 最近记录: |