elasticsearchelasticsearch-6elasticsearch-java-api

Elasticsearch How to add bool query inside must_not with multiple match in java API?


I have a query like this in Elasticsearch:

{
    "query": { 
        "bool": { 
          "must_not": [
            { "match": "filed1" : "value1"},
            { "match": "filed2" : "value2"}
          ]
        }
      }
}

I have tried below code.but it only for single match.

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.mustNot(QueryBuilders.matchQuery("field1","value1");

What is the way to use must_not with multiple match in java API.Thanks.


Solution

  • Good start! You can simply call mustNot() as many times as conditions you need:

    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); 
    boolQueryBuilder.mustNot(QueryBuilders.matchQuery("field1","value1"));
    boolQueryBuilder.mustNot(QueryBuilders.matchQuery("field2","value2"));
    

    Simple as that ;-)