Search code examples
c#elasticsearchnestelasticsearch-painless

Loop through array from Elastic script Params


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?


Solution

  • 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.