Find string in array

Hi,
I have an array that contains some bucket keys. i want to do something for each bucket if their key is in my array.
here is my code so far:

"script": {
      "source": """
            def docs = [];
            def removes=ctx.payload.aggregations.event_types.buckets.remove.users.buckets.stream().map(p -> p.key).collect(Collectors.toList());
            def logins=ctx.payload.aggregations.event_types.buckets.login.users.buckets.stream().map(p -> p.key).collect(Collectors.toList());
            def hits = ctx.payload.aggregations.event_types.buckets.add.users.buckets;
            def user = ctx.payload.aggregations.event_types.buckets.add.users.buckets.stream().map(u -> u.key).filter(u -> removes.contains(u)).filter(u -> logins.contains(u)).toArray();
            for (hit in hits){
              if (user.values.contains(hit.key)){
              def document = [
                   ///DO SOMETHING..
                ];
                docs.add(document);
              }
            }
            return ['_doc':docs];
          """,
      "lang": "painless"
    }

But I just can't figure out how to check the condition for that ( check if bucket keys exist in user array)
thanks

There is a Debug.explain() feature in painless, that allows you to check the state of an expression. Running this before the if might help to figure out the values of user.values and hit.key...

See https://www.elastic.co/guide/en/elasticsearch/painless/7.9/painless-debugging.html

Thanks Alex, i'll check it out.
For now I used a nested loop that compare current key value with every key in the user array and it's working fine.

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