2

Is there any way to build the elasticsearch bool query dynamically. In my application user may use multiple filters at a time. Its like, he may use 2 filters in one request and 3 filters for another request.I'm storing that filter details in hashmap. so based upon the user request the query should change by adding filters.

BoolQueryBuilder query = QueryBuilders.boolQuery()
                    .must(QueryBuilders.matchQuery("client_code",
                            "SSSS")); 

1 Answer 1

7

You can iterate over your hashmap keys/values and build your bool query accordingly for each key/value pair you'll find:

BoolQueryBuilder query = QueryBuilders.boolQuery();
for (String key : hashmap.keySet()) {
    query.must(QueryBuilders.matchQuery(key, hashmap.get(key)));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah... Sorry for the late reply

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.