# Using TermsQuery in Java API Client \[7.17\]

**URL:** <https://discuss.elastic.co/t/using-termsquery-in-java-api-client-7-17/304904>\
**Category:** Elasticsearch\
**Created:** [May 17, 2022, 8:58am UTC](https://discuss.elastic.co/t/using-termsquery-in-java-api-client-7-17/304904 "2022-05-17T08:58:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vipul\_Kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vipul_kumar/32/105771_2.png) [@Vipul\_Kumar](https://discuss.elastic.co/u/Vipul_Kumar)\
**Post date:** [May 17, 2022, 8:58am UTC](https://discuss.elastic.co/t/using-termsquery-in-java-api-client-7-17/304904/1 "2022-05-17T08:58:19Z")

</div>

I'm using Java API Client [7.17] in my project. I'm able to do most of the queries, but I'm trying to figure out how to search for multiple values in a particular fields and return values containing any of the values provided. I would assume Terms Query is what I'm looking for. But how do I pass a String array/list into the TermsQuery in Java API Client.

For example how do I write the below in Java:

```auto
GET /_search
{
  "query": {
    "terms": {
      "referenceno": ["xxx", "yyy"]
    }
  }
}

```

For MatchQuery I'm using the following block of code:

```auto
Query docField = MatchQuery.of(m -> m 
    			    .field(field)
    			    .query(String.valueOf(value))
    			)._toQuery();

```

And passing _docField_ as parameter in bool query. Is there a similar way I can write terms query?

Thanks in advance!

---

<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:** [May 17, 2022, 12:34pm UTC](https://discuss.elastic.co/t/using-termsquery-in-java-api-client-7-17/304904/2 "2022-05-17T12:34:02Z")

</div>

Hi!!

I found this option:

```auto
    TermsQueryField termsQueryField = new TermsQueryField.Builder()
        .value(List.of(FieldValue.of("Value1"), FieldValue.of("Value2")))
        .build();

    TermsQuery termsQuery = new TermsQuery.Builder()
        .field("[FIELD]")
        .terms(termsQueryField)
        .build();

    Query query = new Query.Builder()
        .terms(termsQuery)
        .build();

```

---

<div class="post-metadata">

**Author:** ![Vipul\_Kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vipul_kumar/32/105771_2.png) [@Vipul\_Kumar](https://discuss.elastic.co/u/Vipul_Kumar)\
**Post date:** [May 17, 2022, 5:37pm UTC](https://discuss.elastic.co/t/using-termsquery-in-java-api-client-7-17/304904/3 "2022-05-17T17:37:30Z")

</div>

@RabBit_BR Thank you so much for the quick response.

---

<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:** [June 14, 2022, 5:38pm UTC](https://discuss.elastic.co/t/using-termsquery-in-java-api-client-7-17/304904/4 "2022-06-14T17:38:13Z")

</div>

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