javaelasticsearchresthighlevelclient

Multiple queries in E v5.6 REST high level client


How can I have multiple query in Search API in elasticsearch v5.6 in rest high level client? I need to have OR and AND queries in the search query. I've been using searchSourceBuilder, it only has one matchQuery.

QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(field, query);

Thanks in advance!


Solution

  • You can use something like:

    BoolQueryBuilder finalQuery = QueryBuilders.boolQuery();

    For OR clauses you can use:

    finalQuery.should().add(your query);

    For AND clauses you can use:

    finalQuery.must().add(your query);

    You can link them however you want, and you can nest them as the logic dictates.