-1
"reason" : "compile error",
"lang" : "painless",
          "caused_by" : {
            "type" : "illegal_argument_exception",

"reason" : "Variable [ctx] is not defined."

Below is the details

Script used
POST test_index/_update_by_query
{
    "query": {
    "match_all": {}
  },
  "script_fields": {
    "dummy_data": {
      "script": {
        "source": """
        
        String replace(String word) {
          String[] pieces = word.splitOnToken('file://whatstheGuess.intra/wining/Teams');
          int lastElIndex = pieces.length - 2;
          pieces[lastElIndex] = '';
          def list = Arrays.asList(pieces);
          return String.join('',list);
        }
  
        //usage sample
        def path = replace(params['_source']['d_path']);
        boolean dum=false;
        def path1,indexOfGet1,indexGet1,random;
        for (item in doc['d_filesize'])
      {
        path1 = params['_source']['d_path'];
         indexOfGet1=path1.indexOf('Teams');
         indexGet1 =path1.substring(indexOfGet1+6);
         if(params.pathArray.contains(indexGet1)){
          
          ctx.op = 'delete';
          //params.pathArray.remove(item);
         }
       
      }
     
        params.pathArray.add(path);
      
  
        return params.pathArray;
         """,
         "lang": "painless",
        "params": {
          "pathArray": []
        }
      }
    }
  }
}

Requirement is to first replace the part of string with empty"" , which is working , then storing the substring value after replacement into array and then a loop on every  document in ES index , if match then the matched value should be deleted from params array and the also the document as a whole should be deleted from index ,to implement it, I have tried using ctx.op='delete' , which is not working , this has been implemented to remove some duplication

On the other hand ,
POST test_index/_update_by_query
{
  "script": {
    "source": 
    
    "ctx.op = 'delete'",
    "lang": "painless"
    
  },
    "query": {
    "match": {
       "d_title.keyword" : "1210011-Forms"
    }`enter code here`
  }
}

Query :- second script works fine , so why not ctx.op is working in my main script , when it has been defined in both scripts inside source only What is the difference between defining "source" in both cases. Any help would be great!

JavaAnswer
  • 11
  • 1

1 Answers1

0

The _update_by_query endpoint does not accept script_fields, you simply need to specify your script like this:

POST test_index/_update_by_query
{
  "query": {
    "match_all": {}
  },
  "script": {
      "source": """           
        String replace(String word) {
          String[] pieces = word.splitOnToken('file://whatstheGuess.intra/wining/Teams');
          int lastElIndex = pieces.length - 2;
          pieces[lastElIndex] = '';
          def list = Arrays.asList(pieces);
          return String.join('',list);
        }
  
        //usage sample
        def path = replace(params['_source']['d_path']);
        boolean dum=false;
        def path1,indexOfGet1,indexGet1,random;
        for (item in doc['d_filesize'])
      {
        path1 = params['_source']['d_path'];
         indexOfGet1=path1.indexOf('Teams');
         indexGet1 =path1.substring(indexOfGet1+6);
         if(params.pathArray.contains(indexGet1)){
          
          ctx.op = 'delete';
          //params.pathArray.remove(item);
         }
       
      }
     
        params.pathArray.add(path);
      
  
        return params.pathArray;
    """,
    "lang": "painless",
    "params": {
      "pathArray": []
    }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360