ElasticSearch - 附加到整数数组
问题描述:
我是ES新手,但我已经掌握了它。 这是一个非常强大的软件,但我不得不说,文档是真的缺乏和困惑有时。ElasticSearch - 附加到整数数组
我的问题是: 我有一个整数数组,看起来像这样:
"hits_history" : [0,0]
我想通过一个“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"
}
}
}
所以我尝试:
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": {}
}
}
'
,但它给了我这个错误:
"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]."
所以,我还看了一下,发现这样的:https://www.elastic.co/guide/en/elasticsearch/guide/current/partial-updates.html
其中有下面的例子:
We can also use a script to add a new tag to the tags array.
POST /website/blog/1/_update
{
"script" : "ctx._source.tags+=new_tag",
"params" : {
"new_tag" : "search"
}
}
所以我试了一下:
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": {}
}
}
'
结果:
"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]."
所以,我怎么能添加项目到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'] }"
}
是的,我是有,不幸的是在索引一个项目与指标,而不是一个列表,以便将其添加时是给错误。 – DarkW