Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading