I am new to ES, but I am getting stuck on this. This is a really powerful piece of software, but I have to say that the documentation is really lacking and confusing several times.
Here is my question: I have an integer array that looks like this:
"hits_history" : [0,0]
I want to add an integer to this array by calling "update_by_query", I searched and found this link: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html
which has this example:
POST test/type1/1/_update
{
"script" : {
"inline": "ctx._source.tags.add(params.tag)",
"lang": "painless",
"params" : {
"tag" : "blue"
}
}
}
so i tried:
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": {}
}
}
'
but he gave me this error:
"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]."
So, I looked further and found this: https://www.elastic.co/guide/en/elasticsearch/guide/current/partial-updates.html
which has this example:
script .
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]."
, List? , ?
- :
ctx._source.hits_history.add(ctx._source.today_hits);
ctx._source.today_hits = 0;