Java API for matching multiple fields in elasticsearch

I am currently requesting an ES for a single key value pair as follows:

String query_key = "abc";
String query_val = "def";

searchRequestBuilder.setQuery(QueryBuilders.matchQuery(query_key, query_val)).execute().actionGet();

Now, instead of a single key-value pair, I have the following key-value pair map: Map<String,String> query_list

How do I change the same for this?

+4
source share
1 answer

You can use MuliMatchQuery or Boolean Query to fulfill your requirement.

eg: -

BoolQueryBuilder boolQuery = new BoolQueryBuilder();
for (Map.Entry<String, String> entry : fields.entrySet()){
    boolQuery.must(QueryBuilders.matchQuery(entry.getKey(), entry.getValue()));
}

Set this boolQueryto searchRequestread boolQueries elasticsearch and use the one that fits your requirements.

AND . field1 field2 . OR, sould, minimum_should_match, , .

+13

All Articles