</> POST scripts/painless/execute
{
"script": {
"lang": "painless",
"source": """
String nGramAtPosition(String field, int fieldcount, int n) {
if (fieldcount+n>field.length()) {
return ''
}
else
{
return field.substring(fieldcount, fieldcount+n)
}
}
String fieldtext = ctx[params.field];
if(fieldtext.length()>=params.max_length) {
fieldtext = fieldtext.substring(0, params.max_length);
}
else {
fieldtext = fieldtext;
}
for (int i=0;i<(fieldtext.length());i++) {
ctx[params.field+''+Integer.toString(params.ngram_count)+'-gram_feature'+Integer.toString(i)] = nGramAtPosition(fieldtext, i, params.ngram_count)
}
"""
}
}
</ >
</> I receive
{
"error" : {
"root_cause" : [
{
"type" : "script_exception",
"reason" : "compile error",
"script_stack" : [
"... }\r\n}\r\nString fieldtext = ctx[params.field];\r\nif(fi ...",
" ^---- HERE"
],
"script" : "\r\nString nGramAtPosition(String field, int fieldcount, int n) {\r\n if (fieldcount+n>field.length()) {\r\n return ''\r\n }\r\n else \r\n{\r\n return field.substring(fieldcount, fieldcount+n)\r\n}\r\n}\r\nString fieldtext = ctx[params.field];\r\nif(fieldtext.length()>=params.max_length) {\r\n fieldtext = fieldtext.substring(0, params.max_length);\r\n}\r\nelse {\r\n fieldtext = fieldtext;\r\n}\r\nfor (int i=0;i<(fieldtext.length());i++) {\r\n ctx[params.field+''+Integer.toString(params.ngram_count)+'-gram_feature'+Integer.toString(i)] = nGramAtPosition(fieldtext, i, params.ngram_count)\r\n}\r\n ",
"lang" : "painless",
"position" : {
"offset" : 212,
"start" : 187,
"end" : 237
}
}
],
"type" : "script_exception",
"reason" : "compile error",
"script_stack" : [
"... }\r\n}\r\nString fieldtext = ctx[params.field];\r\nif(fi ...",
" ^---- HERE"
],
"script" : "\r\nString nGramAtPosition(String field, int fieldcount, int n) {\r\n if (fieldcount+n>field.length()) {\r\n return ''\r\n }\r\n else \r\n{\r\n return field.substring(fieldcount, fieldcount+n)\r\n}\r\n}\r\nString fieldtext = ctx[params.field];\r\nif(fieldtext.length()>=params.max_length) {\r\n fieldtext = fieldtext.substring(0, params.max_length);\r\n}\r\nelse {\r\n fieldtext = fieldtext;\r\n}\r\nfor (int i=0;i<(fieldtext.length());i++) {\r\n ctx[params.field+'_'+Integer.toString(params.ngram_count)+'-gram_feature'+Integer.toString(i)] = nGramAtPosition(fieldtext, i, params.ngram_count)\r\n}\r\n ",
"lang" : "painless",
"position" : {
"offset" : 212,
"start" : 187,
"end" : 237
},
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "cannot resolve symbol [ctx]"
}
},
"status" : 400
}
</>
Context (ctx
) is not available in the painless execute test API.
The test script provided in Machine learning in cybersecurity: Training supervised models to detect DGA activity is meant to be used with the reindex
action, where ctx
is available.
Welcome to our community!
Please format your code/logs/config using the </>
button, or markdown style back ticks. It helps to make things easy to read which helps us help you
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.