Elastic Search _update_by_query

Hi

I am using _update_by_query in following way:

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": "first"
    }
  }
}

It is giving me following error:

{
    "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 == \"group\"){ } else{ if(ctx._source.fields[i].uid == params.field_uid[j].uid){ ctx._source.fields.remove(i);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 == \"group\"){ } else{ if(ctx._source.fields[i].uid == params.field_uid[j].uid){ ctx._source.fields.remove(i);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?

That's how your script look like:

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);
      } 
    } 
  }
}

It complains about the first empty if statement.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.