Java Elasticsearch BoolQueryBuilder for multiple fields

42 Views Asked by At

The index is having 20 fields. I am having one search value received as query parameter in api.

There are 3 fields in index name, city, state In my spring boot application, I need to return response if the search value say new matches with any of the 3 fields.

Like return the response : city=="new" || state=="new" || name=="new"

What BoolQuery I should write for that ? Or any other way for it. I am using RestHighLevelClient of elastic search 7.7

1

There are 1 best solutions below

0
Murat K. On

I suggest using should query. Must is like more AND logic and should is like an OR.

boolQuery.should(QueryBuilders.matchQuery("name", searchValue)); 
boolQuery.should(QueryBuilders.matchQuery("city", searchValue)); 
boolQuery.should(QueryBuilders.matchQuery("state", searchValue));

minimum_should_match is also worth reading.

Elastic search boolean query