# How to loop through the values of array list to exactly match all the values

**URL:** <https://discuss.elastic.co/t/how-to-loop-through-the-values-of-array-list-to-exactly-match-all-the-values/319787>\
**Category:** Elasticsearch\
**Created:** [November 25, 2022, 6:20am UTC](https://discuss.elastic.co/t/how-to-loop-through-the-values-of-array-list-to-exactly-match-all-the-values/319787 "2022-11-25T06:20:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhavya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bhavya/32/45679_2.png) [@bhavya](https://discuss.elastic.co/u/bhavya)\
**Post date:** [November 25, 2022, 6:20am UTC](https://discuss.elastic.co/t/how-to-loop-through-the-values-of-array-list-to-exactly-match-all-the-values/319787/1 "2022-11-25T06:20:05Z")

</div>

Consider the below documents:

**Documents**

Doc 1

```auto
{
    "id": [
        "1a",
        "1b",
        "1c"
    ]
}

```

Doc 2

```auto
{
    "id": [
        "1a",
        "1b"
    ]
}

```

Doc 3

```auto
{
    "id": [
        "1a"
    ]
}

```

Now if I search

For `"id" : ["1a","1b"]`, then only doc 2 should match

For `"id" : ["1a","1b","1d"]`, no documents should match

For `"id" : ["1a"]` only document 3 should match

It is similar to the below java code implementation

```auto
import java.util.*;
  
class Test {
    public static void main(String[] args)
    {
        List<String> list1 = Arrays.asList("One", "Two");
        List<String> list2 = Arrays.asList("One","Two","Three");
        boolean isMatch = list2.equals(list1);
        System.out.println(isMatch);
    }
}

```

With terms query and a query string, I attempted to build the above use case scenario, but it does not function for an exact match.

So, I think the best way should be to use the script, where I can loop through the values of the array list OR use `equals` from [Shared API](https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-api-reference-shared-java-util.html#painless-api-reference-shared-java-util) to achieve this use case.

Can anyone please help me with the script for this?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [November 25, 2022, 6:40am UTC](https://discuss.elastic.co/t/how-to-loop-through-the-values-of-array-list-to-exactly-match-all-the-values/319787/2 "2022-11-25T06:40:39Z")

</div>

Have you looked at the [terms set query](https://www.elastic.co/guide/en/elasticsearch/reference/8.5/query-dsl-terms-set-query.html)? If youindex the length of the array and add a condition for this you may be able to achieve what you describe.

---

<div class="post-metadata">

**Author:** ![bhavya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bhavya/32/45679_2.png) [@bhavya](https://discuss.elastic.co/u/bhavya)\
**Post date:** [November 25, 2022, 6:46am UTC](https://discuss.elastic.co/t/how-to-loop-through-the-values-of-array-list-to-exactly-match-all-the-values/319787/3 "2022-11-25T06:46:06Z")

</div>

@Christian_Dahlqvist yes, I tried with terms set query also, but I didn't solve all the use case scenarios.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [November 25, 2022, 6:49am UTC](https://discuss.elastic.co/t/how-to-loop-through-the-values-of-array-list-to-exactly-match-all-the-values/319787/4 "2022-11-25T06:49:33Z")

</div>

What case did it not solve? Based on your example it seems like a great fit.

---

<div class="post-metadata">

**Author:** ![bhavya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bhavya/32/45679_2.png) [@bhavya](https://discuss.elastic.co/u/bhavya)\
**Post date:** [November 25, 2022, 6:55am UTC](https://discuss.elastic.co/t/how-to-loop-through-the-values-of-array-list-to-exactly-match-all-the-values/319787/5 "2022-11-25T06:55:01Z")

</div>

yes @Christian_Dahlqvist, I tried again using the `minimum_should_match_script` param, and all the cases are covered. But, there is one more issue, I cannot add one more field in each document (which is required in the `terms set` query) to find the match count.

Is there any way, to do this without adding any extra fields to the documents?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [November 25, 2022, 6:56am UTC](https://discuss.elastic.co/t/how-to-loop-through-the-values-of-array-list-to-exactly-match-all-the-values/319787/6 "2022-11-25T06:56:41Z")

</div>

You can probably add a runtime field to calculate the length of the array and make this available as a field, but that will be slower than indexing it.

---

<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:** [December 23, 2022, 6:57am UTC](https://discuss.elastic.co/t/how-to-loop-through-the-values-of-array-list-to-exactly-match-all-the-values/319787/7 "2022-12-23T06:57:04Z")

</div>

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