How to do bool Queries in ElasticSearch with Java Api?
Asked Answered
R

1

9

iam trying to send a bool query against my elasticSearch, that filters out documents. How can i filter boolean expressions like (A AND B AND C) AND (D OR E OR F) AND G with BooleanQueryBuilder Api in Java? I tried with the filter method, but i get no results back. For example:

Imagine A, B, C, D, E, F, G are events where the attribute "name" must match "Robin" for example.

private QueryBuilder getQueryBuilder() {

        BoolQueryBuilder boolQueryBuilder1 = QueryBuilders
                .boolQuery()
                .filter(QueryBuilders.matchQuery(D).operator(Operator.OR))
                .filter(QueryBuilders.matchQuery(E).operator(Operator.OR))
                .filter(F);

        BoolQueryBuilder boolQueryBuilder2 = QueryBuilders
                .boolQuery()
                .filter(QueryBuilders.matchQuery(A).operator(Operator.AND))
                .filter(QueryBuilders.matchQuery(B).operator(Operator.AND))
                .filter(QueryBuilders.matchQuery(C));

        BoolQueryBuilder boolQueryBuilder3 = QueryBuilders
                .boolQuery()
                .filter(G);

        BoolQueryBuilder boolQueryBuilder = QueryBuilders
                .boolQuery()
                .filter(boolQueryBuilder1)
                .filter(boolQueryBuilder2)
                .filter(boolQueryBuilder3);

        return boolQueryBuilder;

}

I know this example is not very good, but i hope u can help me! Thanks!

Rangefinder answered 25/4, 2019 at 15:25 Comment(1)
This might not answer your question directly, but I would try and see how does the request look thats going out to Elastic and what response are you getting back. I always use Fiddler to do that sort of stuff. Another idea, you can try using Elastic QueryString (elastic.co/guide/en/elasticsearch/reference/current/…)Postwar
V
10

Is this one?

private static QueryBuilder getQueryBuilder() {
    //condition
    MatchQueryBuilder A = QueryBuilders.matchQuery("content", "A");
    MatchQueryBuilder B = QueryBuilders.matchQuery("content", "B");
    MatchQueryBuilder C = QueryBuilders.matchQuery("content", "C");
    MatchQueryBuilder D = QueryBuilders.matchQuery("content", "D");
    MatchQueryBuilder E = QueryBuilders.matchQuery("content", "E");
    MatchQueryBuilder F = QueryBuilders.matchQuery("content", "F");
    MatchQueryBuilder G = QueryBuilders.matchQuery("name", "Robin");
    //Maybe you should use TermQuery
    //TermQueryBuilder G = QueryBuilders.termQuery("name", "Robin");


    //1.(A AND B AND C)
    BoolQueryBuilder query1 = QueryBuilders.boolQuery();
    query1.must(A).must(B).must(C);

    //2.(D OR E OR F)
    BoolQueryBuilder query2 = QueryBuilders.boolQuery();
    query2.should(D).should(E).should(F);

    //3.Last
    BoolQueryBuilder lastQuery = QueryBuilders.boolQuery();
    lastQuery.must(query1).must(query2).must(G);
    
    //use filter
    BoolQueryBuilder filterBuiler = QueryBuilders.boolQuery().filter(lastQuery);
    return filterBuiler;
}
Verdun answered 26/4, 2019 at 6:16 Comment(1)
Hi, thanks for the response!, I tried ur code, but i still get results where name != robin. i think this comes from the score. The thing is, i dont want the score to be considered. I just need documents, that really match to the given bool expression. Thats why i thought, the filter method should be the right one and not the must and should methods.Rangefinder

© 2022 - 2024 — McMap. All rights reserved.