How To Filter Out List of Word from ElasticSearch Query Result?

Advertisements
context.analyzer("email_indexing_exact")
                .custom()
                .tokenizer("uax_url_email");
        SearchSession searchSession = Search.session(entityManager);
        String[] fieldsArray = {"displayName", "email"};
        String[] filterEmailArray = filterEmail.split(",");

        var query = searchSession.search(User.class)
                .extension(ElasticsearchExtension.get())
                .select(ElasticsearchSearchProjectionFactory::source)
                .where(f -> f.bool()
                        .must(f.match().fields(fieldsArray).matching(word).analyzer("autocomplete_search"))
                        .mustNot(f.match().field("email").matching(filterEmailArray[0]).analyzer("email_indexing_exact"))
                )
                .sort(f -> f.score().desc());

As you can see I only can filter out the 0th index of the array. How to filter out all of them?

>Solution :

You can use the TermsSetContext query instead of the MatchContext.

Replace this line:

.mustNot(f.match().field("email").matching(filterEmailArray[0]).analyzer("email_indexing_exact"))

with the next one:

.mustNot(f.termsSet().field("email").terms(filterEmailArray).analyzer("email_indexing_exact"))

Note: the termsSet query is only available in Elasticsearch 7.0 and later.

Update
for your 6.1.8. version try next:

.mustNot(f.terms().field("email").matching(Arrays.asList(filterEmailArray)).analyzer("email_indexing_exact"))

Note: the terms query has limitations, such as a limit of 65,536 terms.

Leave a ReplyCancel reply