# Elasticsearch Java Client create query for field with list of values

**URL:** <https://discuss.elastic.co/t/elasticsearch-java-client-create-query-for-field-with-list-of-values/328040>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [March 20, 2023, 4:29am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-create-query-for-field-with-list-of-values/328040 "2023-03-20T04:29:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcpeiris](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@tcpeiris](https://discuss.elastic.co/u/tcpeiris)\
**Post date:** [March 20, 2023, 4:29am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-create-query-for-field-with-list-of-values/328040/1 "2023-03-20T04:29:18Z")

</div>

String searchText = "TEST";

`.query(q -> q.bool(b -> b .must(c-> c .match(t -> t .field("FIELD NAME").query(searchText) ))`

I need to pass String Array to query. Whats the best way I can do it.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [March 20, 2023, 1:34pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-create-query-for-field-with-list-of-values/328040/2 "2023-03-20T13:34:16Z")

</div>

Hi @tcpeiris

If you want to use a list of values, I believe you should use Terms Query.

Below is my suggestion:

```auto
    var array = new ArrayList<FieldValue>();
    array.add(FieldValue.of("news"));
    array.add(FieldValue.of("old"));

    var termsQuery = TermsQuery.of(ts -> ts
        .field("field")
        .terms(TermsQueryField.of(t -> t.value(array))));

    var searchRequest = SearchRequest.of(sr -> sr
        .query(termsQuery._toQuery())
        .index("idx_test"));

```

---

<div class="post-metadata">

**Author:** ![tcpeiris](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@tcpeiris](https://discuss.elastic.co/u/tcpeiris)\
**Post date:** [March 20, 2023, 1:59pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-create-query-for-field-with-list-of-values/328040/3 "2023-03-20T13:59:32Z")

</div>

Thank you @RabBit_BR . This is the solution I was looking for.

---

<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:** [April 17, 2023, 2:00pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-create-query-for-field-with-list-of-values/328040/4 "2023-04-17T14:00:07Z")

</div>

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