2

I am using update_by_query with the following body:

POST /documents/_update_by_query
 {
"script":{
  "source":"for(int i = 0;i < ctx._source.fields.size();i++){for (int j = 0; j < params.field_uid.size();j++){ if(params.field_uid[j].type == \"group\" ){ } else{ if(ctx._source.fields[i].uid == params.field_uid[j].uid){ ctx._source.fields.remove(i);} } }}",
  "params":{
   "field_uid":[{"uid":"number","type":"number"},{"uid":"test","type":"group"}]
   }
 },
 "query": { 
    "term": {
      "name": "test"
    }
  }
}

It is giving me like Extraneous if statement. Here is the error message I got :

{
    "error": {
        "root_cause": [
            {
                "type": "script_exception",
                "reason": "compile error",
                "script_stack": [
                    "... s.field_uid.size();j++){ if(params.field_uid[j].ty ...",
                    "                             ^---- HERE"
                ],
                "script": "for(int i = 0;i < ctx._source.fields.size();i++){for (int j = 0; j < params.field_uid.size();j++){ if(params.field_uid[j].type == 100 ){ } else{ if(ctx._source.fields[i].uid == params.field_uid[j].uid){ ctx._source.fields.remove(i);} } }}",
                "lang": "painless"
            }
        ],
        "type": "script_exception",
        "reason": "compile error",
        "script_stack": [
            "... s.field_uid.size();j++){ if(params.field_uid[j].ty ...",
            "                             ^---- HERE"
        ],
        "script": "for(int i = 0;i < ctx._source.fields.size();i++){for (int j = 0; j < params.field_uid.size();j++){ if(params.field_uid[j].type == 100 ){ } else{ if(ctx._source.fields[i].uid == params.field_uid[j].uid){ ctx._source.fields.remove(i);} } }}",
        "lang": "painless",
        "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Extraneous if statement."
        }
    },
    "status": 500
}

Can anyone help me into this? What is wrong here?

1 Answer 1

3

The if statement written is not doing any operation and is irrelevant as a result ES is throwing extraneous if statement error.

Remove it and update the condition accordingly as below.

for(int i = 0;i < ctx._source.fields.size();i++){ for (int j = 0;j < 
params.field_uid.size();j++){ if(params.field_uid[j].type != \"group\"  
&& ctx._source.fields[i].uid == params.field_uid[j].uid){ 
ctx._source.fields.remove(i);}}}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Chandra, Thanks for the reply, I have one more question, How I can check startsWith in the script. I want to check if the current string starts with some other string. for example here want to check params.field_uid[j].type startsWith group.name or not?

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.