0

I'm creating a (ElasticSearch) Painless script. And as parameter I'm setting an array, but I'm not able to loop trough that array in my script.

Elastic params setter (with NEST):

Params = new Dictionary<string, object>
             {
               { "ages", new []{2,4,6}},
             }

Painless script:

for(int age in params.ages)
{
 // do something
}

Error:

"script_stack": [
                    "... for(int age in params.ages){ ...",
                    "                ^---- HERE"
                ],

How can I use the param as an array?

BvdVen
  • 2,921
  • 23
  • 33
  • Did you have a look at the documentation? https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-walkthrough.html#_accessing_doc_values_from_painless They seem to use a full for loop to iterate through the items in an array. OR https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-statements.html#_for – Qpirate Oct 30 '19 at 14:05
  • @Qpirate Yes, that seems to work indeed. So I think for params you must use the full for loop and with document properties it's not necessary. – BvdVen Oct 30 '19 at 14:06

1 Answers1

2

Looks like you need to change your loop definition from

for(int age in params.ages)

to

for(age in params.ages)

Hope that helps.

Rob
  • 9,664
  • 3
  • 41
  • 43