How to build query using elastic8 java client

I need one example where using elastic 8 dsl creates query which includes boolquery with should shouldnot must

.query(q -> q.bool(
                            b -> b.must(ListObj).should(listShould).mustNot(listMustNot))));

Hi @suresh_chaudhari

This is a suggestion:

    var boolQuery = BoolQuery.of(bq -> bq
        .should(List.of(MatchQuery.of(mq -> mq.query("value").field("field"))._toQuery()))
        .must(List.of(MatchQuery.of(mq -> mq.query("value").field("field"))._toQuery()))
        .mustNot(List.of(MatchQuery.of(mq -> mq.query("value").field("field"))._toQuery()))
    );

    var request = SearchRequest.of(s -> s.query(boolQuery._toQuery()));

very nice and thanks for this.
I doubt why elastic doc does not cover this

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.