Using TermsQuery in Java API Client [7.17]

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:

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

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

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!

Hi!!

I found this option:

    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();
1 Like

@RabBit_BR Thank you so much for the quick response.

1 Like

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