# Java API for terms

**URL:** <https://discuss.elastic.co/t/java-api-for-terms/345461>\
**Category:** Elasticsearch\
**Created:** [October 20, 2023, 12:39pm UTC](https://discuss.elastic.co/t/java-api-for-terms/345461 "2023-10-20T12:39:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfrob](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mfrob/32/124171_2.png) [@mfrob](https://discuss.elastic.co/u/mfrob)\
**Post date:** [October 20, 2023, 12:39pm UTC](https://discuss.elastic.co/t/java-api-for-terms/345461/1 "2023-10-20T12:39:27Z")

</div>

Hi, I hope someone finds this. I am very new to elasticsearch. Currently I have this code snippet in my java file to search based on customer identification card (IC) number.

I am able to fetch and search based off just ONE IC number at a time like this

```auto
SearchResponse<MyClass> esResponse = esClient.search((s -> s
                 .index("loans")
                 .size(pageSize)
                 .from(page)
                 .query(q -> q
                     .terms(t -> t
                         .field("ic_number.keyword")
                         .value(icNum)
                     )
                 ).sort(so -> so
                     .field(FieldSort.of(f -> f
                         .field("date_created")
                         .order(SortOrder.Desc)
                     ))
                 )
             ), MyClass.class);

```

But I was wondering am I able to search and fetch results from elasticsearch based off a list of IC numbers instead of just one? I read a little about the "terms" query but the documentation doesnt really show anything about the terms query in the Java API Client, therefore I have to seek help from others who are more experienced with this.

Thank you for your time and consideration.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 20, 2023, 12:48pm UTC](https://discuss.elastic.co/t/java-api-for-terms/345461/2 "2023-10-20T12:48:22Z")

</div>

I think you can do something like:

```auto
SearchResponse<MyClass> esResponse = esClient.search((s -> s
                 .index("loans")
                 .size(pageSize)
                 .from(page)
                 .query(q -> q
                     .terms(t -> t
                         .field("ic_number.keyword")
                         .terms(tqf -> tqf.value(/* List<String> here */))
                     )
                 ).sort(so -> so
                     .field(FieldSort.of(f -> f
                         .field("date_created")
                         .order(SortOrder.Desc)
                     ))
                 )
             ), MyClass.class);

```

Not tested... But I guessed it [from the JavaDoc](https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.10.4/co/elastic/clients/elasticsearch/_types/query_dsl/TermsQuery.Builder.html#terms(java.util.function.Function)).

---

<div class="post-metadata">

**Author:** ![mfrob](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mfrob/32/124171_2.png) [@mfrob](https://discuss.elastic.co/u/mfrob)\
**Post date:** [October 20, 2023, 1:34pm UTC](https://discuss.elastic.co/t/java-api-for-terms/345461/3 "2023-10-20T13:34:02Z")

</div>

Thank you so much!, the only change I had to make was ensure the List was of type FieldValue. You are a lifesaver. Once again, I cant thank you enough, Ive been stuck on this for hours

---

<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:** [November 17, 2023, 1:34pm UTC](https://discuss.elastic.co/t/java-api-for-terms/345461/4 "2023-11-17T13:34:45Z")

</div>

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