# Find string in array

**URL:** <https://discuss.elastic.co/t/find-string-in-array/249617>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting, painless\
**Created:** [September 23, 2020, 7:41am UTC](https://discuss.elastic.co/t/find-string-in-array/249617 "2020-09-23T07:41:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![borna\_talebi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/borna_talebi/32/76317_2.png) [@borna\_talebi](https://discuss.elastic.co/u/borna_talebi)\
**Post date:** [September 23, 2020, 7:41am UTC](https://discuss.elastic.co/t/find-string-in-array/249617/1 "2020-09-23T07:41:17Z")

</div>

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:

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

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 23, 2020, 12:03pm UTC](https://discuss.elastic.co/t/find-string-in-array/249617/2 "2020-09-23T12:03:30Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/painless/7.9/painless-debugging.html)

---

<div class="post-metadata">

**Author:** ![borna\_talebi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/borna_talebi/32/76317_2.png) [@borna\_talebi](https://discuss.elastic.co/u/borna_talebi)\
**Post date:** [September 23, 2020, 12:13pm UTC](https://discuss.elastic.co/t/find-string-in-array/249617/3 "2020-09-23T12:13:29Z")

</div>

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.

---

<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:** [October 21, 2020, 12:13pm UTC](https://discuss.elastic.co/t/find-string-in-array/249617/4 "2020-10-21T12:13:33Z")

</div>

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