Elasticsearch Java Client create query for field with list of values

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.

Hi @tcpeiris

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

Below is my suggestion:

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

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

1 Like

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