将内部源代码用作脚本时未定义变量[CTX

jobtbby3  于 2022-10-06  发布在  ElasticSearch
关注(0)|答案(1)|浏览(167)
"reason" : "compile error",
"lang" : "painless",
          "caused_by" : {
            "type" : "illegal_argument_exception",

“Reason”:“未定义变量[CTX]。”

以下是详细信息

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`
  }
}

查询:-第二个脚本运行得很好,那么为什么ctx.op不能在我的主脚本中工作,当它在两个脚本中都定义了源代码时,在两种情况下定义“源”有什么不同。任何帮助都是最好的!

mfpqipee

mfpqipee1#

_update_by_query终结点不接受script_fields,您只需按如下方式指定脚本:

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": []
    }
  }
}

相关问题