9

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?

0

1 Answer 1

26

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.