Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java API to match multiple fields in elasticsearch

Currently I query ES for a single key value pair like this:

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

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

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

How do I modify the same for this?

like image 782
Prachi g Avatar asked Dec 02 '14 12:12

Prachi g


1 Answers

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

ex:-

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

Set this boolQuery in your searchRequest read the elasticsearch boolQueries and use one which fits for your requirement.

The Above example will perform the AND operation among the provided fields. the field1 and field2 must match. if you want to perform the OR operation then you should use sould query you have an option to set minimum_should_match in that you can specify the minimum fields should match.

like image 94
Arun Prakash Avatar answered Nov 05 '22 12:11

Arun Prakash