Reputation: 2422
I am trying to update the some values in a particular field, which I want to be unique.
But when I give the script the condition .unique() it returns me an error.
If I do not give the condition unique then it's keep updating again and again. Means the value will be
update_field= array ('world','values','frost','world','values','frost',...)
What I am doing wrong here anyone knows !!!
$script['script'] = 'ctx._source.update_field = (ctx._source.update_field + new_value).unique()';
// unique() is giving the error but why
error message ---
{"error":"ElasticsearchIllegalArgumentException[failed to execute script]; nested: GroovyScriptExecutionException[MissingMethodException[No signature of method: java.lang.String.unique() is applicable for argument types: () values: []\nPossible solutions: minus(java.lang.Object), minus(java.util.regex.Pattern), minus(java.lang.Object), size(), size(), use([Ljava.lang.Object;)]]; ","status":400}
a sample documents ---
hits: {
total: 19,
max_score: 5.5197725,
hits: [
{
_index: "myIndex",
_type: "myType",
_id: "pdd9da099f380b3951a627a5d5a38758680016f16",
_score: 5.5197725,
_source: {
content: "After shooting attacks are several police cars sent to the streets to watch.
title: "The police in Copenhagen when bødemål despite extra tasks",
update_field: "[funny]"
}},}
So there you can see that the update_field is not empty and have some value - when when i try ---
$script['script'] = 'ctx._source.update_field = ((ctx._source.update_field ?: []) - new_value).unique()';
it also returns me the same error ! don't know why !!
Upvotes: 0
Views: 2930
Reputation: 217554
From the error, the key point is No signature of method: java.lang.String.unique()
. It's probably because update_field
does not exist or is empty at some point and thus the +
operator will simply try to concatenate it as a string with the array you're specifying.
One way to fix this would be to make sure update_field
is considered as an array no matter what. You should be able to achieve this by replacing ctx._source.update_field
with (ctx._source.update_field ?: [])
, that way if update_field
does not exist or is empty, then an empty array will be used instead and the +
operator will work on an array instead of on a string field.
$script['script'] = 'ctx._source.update_field = ((ctx._source.update_field ?: []) + new_value).unique()';
Upvotes: 1