Java API for terms

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

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.

I think you can do something like:

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.

1 Like

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

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