# Most efficient way to search an array for a value (Painless)

**URL:** <https://discuss.elastic.co/t/most-efficient-way-to-search-an-array-for-a-value-painless/248247>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [September 10, 2020, 10:51pm UTC](https://discuss.elastic.co/t/most-efficient-way-to-search-an-array-for-a-value-painless/248247 "2020-09-10T22:51:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![emarthinsen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emarthinsen/32/23172_2.png) [@emarthinsen](https://discuss.elastic.co/u/emarthinsen)\
**Post date:** [September 10, 2020, 10:51pm UTC](https://discuss.elastic.co/t/most-efficient-way-to-search-an-array-for-a-value-painless/248247/1 "2020-09-10T22:51:14Z")

</div>

I wrote a script for generating a calculated field. It's written in Painless. I'm adding a feature to it where I need to see if an array that is on the document has a value in it. The property in the document is called `colors` and the data looks a bit like this:

```auto
[
  { id: 1, name: 'red' },
  { id: 2, name: 'green' },
  { id: 3, name: 'blue'}
]

```

There actually a ton of values in there. My search query has a parameter called `colors` that looks like this:

```auto
omit_colors: [1, 3]

```

What I want to do is write a bit of code that will iterate through the `colors` property on the doc and see if any values in the `omit_colors` property is in there. Right now, I have it written as a nested loop that iterates over `colors` and checks each against each value in `omit_colors`. It works, but feels a little messy. Can anyone recommend the most efficient way to do this?

BTW, the nature of our query and results are such that we can't just set up a filter in the query.

---

<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 8, 2020, 10:51pm UTC](https://discuss.elastic.co/t/most-efficient-way-to-search-an-array-for-a-value-painless/248247/2 "2020-10-08T22:51:32Z")

</div>

Closed on October 8, 2020, 10:51pm UTC

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