3

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?

3
  • 1
    Change must with should instead and add minimumShouldMatch(1) Commented Feb 1, 2017 at 12:21
  • Thanks Val, it works! But my Koelnerphonetic Query will not work :( it handles such things like "Mayer / Maier / Meyer / Meier" and say that these ones are sounds same ... in my question I've updated my Mapping/Setting for this Commented Feb 1, 2017 at 12:30
  • Ok, nvm ..... I just used the wrong analyzer in the first query Commented Feb 1, 2017 at 12:33

1 Answer 1

4

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));
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for the example - it worked! But now I've got another problem (see question Update #2)
You need to use the correct analyzer, you have defined atsCustomPhoneticAnalyzer but you're referencing atsCustomSearchAnalyzer
Added my CustomSearchAnalyzer to my mapping example in Question area
Look at the names of the analyzers, they don't match with the one you're using in your query.
Ok, now you've fixed it. I'd say that this new issue would warrant another question, the initial one being solved.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.