# Accessing \_index variable inside painless script?

**URL:** <https://discuss.elastic.co/t/accessing--index-variable-inside-painless-script/82362>\
**Category:** Elasticsearch\
**Created:** [April 13, 2017, 10:22pm UTC](https://discuss.elastic.co/t/accessing--index-variable-inside-painless-script/82362 "2017-04-13T22:22:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![olalonde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olalonde/32/17118_2.png) [@olalonde](https://discuss.elastic.co/u/olalonde)\
**Post date:** [April 13, 2017, 10:22pm UTC](https://discuss.elastic.co/t/accessing--index-variable-inside-painless-script/82362/1 "2017-04-13T22:22:23Z")

</div>

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:

```auto
[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:

```auto
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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![olalonde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olalonde/32/17118_2.png) [@olalonde](https://discuss.elastic.co/u/olalonde)\
**Post date:** [April 13, 2017, 10:27pm UTC](https://discuss.elastic.co/t/accessing--index-variable-inside-painless-script/82362/2 "2017-04-13T22:27:12Z")

</div>

So it seems \_index is no longer accessible from painless scripts:

> [@Error when accessing \_index in script\_score](https://discuss.elastic.co/t/error-when-accessing--index-in-script-score/63109/2):
>
> It seems \_index is not supported in painless language. Change "lang" to "groovy", and the query will work. Also note that you need to turn on groovy inline script in ES 5.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 11, 2017, 10:35pm UTC](https://discuss.elastic.co/t/accessing--index-variable-inside-painless-script/82362/3 "2017-05-11T22:35:43Z")

</div>

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