I've noticed the deprecation warning for Groovy script so I'm trying to port a groovy script to painless. However, it looks like painless scripts don't have access to the _index variable (which I need to access)? Here's the error I'm getting:
[freq_payloads_cosine]: {
"type" : "script_exception",
"reason" : "compile error",
"script_stack" : [
"... '][0];\ndef index_field = _index[field_name];\n\n// Q ...",
" ^---- HERE"
],
"script" : "String field_name = 'visual_words';\nint dot_product = 0;\nint norm_l = 0;\n\n// why index 0... can there be more than one value?\nint norm_r = doc['norm'][0];\ndef index_field = _index[field_name];\n\n// Query words has following shape:\n// [[word1, word1_weight], [word2, word2_weight],...]\nfor (pair in query_words) {\n int word = pair[0];\n int weight_l = pair[1];\n int norm_l = norm_l + (weight_l * weight_l);\n // Get corresponging word token in\n termInfo = index_field.get(word.toString(), _PAYLOADS);\n for (pos in termInfo) { // this iterates only once...\n weight_r = pos.payloadAsInt(0);\n dot_product = dot_product + (weight_l * weight_r);\n }\n}\n\ndenom = Math.sqrt(norm_l * norm_r);\nsimilarity = dot_product / denom;\n\nreturn similarity;\n",
"lang" : "painless",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "Variable [_index] is not defined."
}
}
Here's the painless script:
String field_name = 'visual_words';
int dot_product = 0;
int norm_l = 0;
// why index 0... can there be more than one value?
int norm_r = doc['norm'][0];
def index_field = _index[field_name];
// Query words has following shape:
// [[word1, word1_weight], [word2, word2_weight],...]
for (pair in query_words) {
int word = pair[0];
int weight_l = pair[1];
int norm_l = norm_l + (weight_l * weight_l);
// Get corresponging word token in
termInfo = index_field.get(word.toString(), _PAYLOADS);
for (pos in termInfo) { // this iterates only once...
weight_r = pos.payloadAsInt(0);
dot_product = dot_product + (weight_l * weight_r);
}
}
denom = Math.sqrt(norm_l * norm_r);
similarity = dot_product / denom;
return similarity;
Here's the groovy script I'm trying to port:
field_name = 'visual_words'
dot_product = 0
norm_l = 0
// why index 0... I don't know...
norm_r = doc['norm'][0]
index_field = _index[field_name]
// Query words has following shape:
// [[word1, word1_weight], [word2, word2_weight],...]
for (pair in query_words) {
word = pair[0]
weight_l = pair[1]
norm_l = norm_l + (weight_l * weight_l)
// Get corresponging word token in
termInfo = index_field.get(word.toString(), _PAYLOADS)
for (pos in termInfo) { // this iterates only once...
weight_r = pos.payloadAsInt(0)
dot_product = dot_product + (weight_l * weight_r)
}
}
denom = Math.sqrt(norm_l * norm_r)
similarity = dot_product / denom
return similarity
Assuming painless scripts don't have access to _index and Groovy scripts are getting deprecated, what would be the future proof way to do this. Should I instead write an ES plugin?