Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch Java API - Bool Query Operator

I am using Elasticsearch 2.4.3 in my Spring Boot App and use following Query

    QueryBuilder qb = new BoolQueryBuilder()
        .must(QueryBuilders.multiMatchQuery(term, "phoneticFirstName", "phoneticLastName", "phoneticLocationName", "phoneticCompanyName")
                .analyzer("atsCustomSearchAnalyzer")
                .operator(Operator.AND))
        .must(QueryBuilders.multiMatchQuery(term, "ngramFirstName^3", "ngramLastName^3", "ngramLocationName^3", "ngramCompanyName^3", "_all")
                .analyzer("atsCustomSearchAnalyzer")
                .operator(Operator.AND));

I want to get a response, where the first Query or the second Query get hits.... can you help me to change that in my Code, please?

UPDATE

        "atsCustomPhoneticAnalyzer":{
            "type":"custom",
            "tokenizer":"whitespace",
            "filter":["lowercase","asciifolding","atsPhoneticFilter"]
        },
        "atsCustomSearchAnalyzer":{
            "type":"custom",
            "tokenizer":"whitespace",
            "filter":["lowercase","asciifolding","umlautStemmer","germanStemmer"]   
        }

UPDATE #2

    QueryBuilder qb = new BoolQueryBuilder()
        .should(QueryBuilders.multiMatchQuery(term, "ngramFirstName", "ngramLastName", "ngramLocationName", "ngramCompanyName")
            .type(Type.CROSS_FIELDS)
            .analyzer("atsCustomSearchAnalyzer")
            .operator(Operator.AND)
            .boost(3))
        .should(QueryBuilders.multiMatchQuery(term, "phoneticLastName")
            .analyzer("atsCustomPhoneticAnalyzer")
            .operator(Operator.AND))
        .should(QueryBuilders.matchQuery(term, "_all")
                .analyzer("atsCustomSearchAnalyzer")
                .operator(Operator.AND))
        .minimumNumberShouldMatch(1);

I have 2 indices: persons and activities. When I comment out the second query I get Hits from persons and activities. If all 3 queries are present the hits from activities are not there anymore....

Any ideas?

like image 621
Timo Ademeit Avatar asked Dec 06 '25 06:12

Timo Ademeit


1 Answers

Simply change must with should instead and add minimumShouldMatch(1)

QueryBuilder qb = new BoolQueryBuilder()
    .minimumNumberShouldMatch(1)
    .should(QueryBuilders.multiMatchQuery(term, "phoneticFirstName", "phoneticLastName", "phoneticLocationName", "phoneticCompanyName")
            .analyzer("atsCustomSearchAnalyzer")
            .operator(Operator.AND))
    .should(QueryBuilders.multiMatchQuery(term, "ngramFirstName^3", "ngramLastName^3", "ngramLocationName^3", "ngramCompanyName^3", "_all")
            .analyzer("atsCustomSearchAnalyzer")
            .operator(Operator.AND));
like image 158
Val Avatar answered Dec 07 '25 20:12

Val



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!