0

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 !!

1 Answer 1

1

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()';
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your answer and explanation, but it is also return me the same error, i have also update my question with a sample document, can you kindly have a look, thanks a lot
You probably need to wipe your myIndex index and recreate it, because your update_field has already been indexed as a string, i.e. "[funny]".
@oops my bad, thanks a lot Val, thanks a lot for your help :), you are awesome
i have reindex my documents and it is working perfectly, thanks a lot for your help again :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.