Gucksen
Gucksen

Reputation: 11

Hibernate Search query for first and last name

I am using Hibernate Search to find people in my db.

They have indexed firstName and lastName fields.

I want results that look kinda like that:

Search: "Robe Pau"

Match:

Shouldn't Match:

While searches like "Rob"

should match:

My code looks like this:

    FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);

    Query query = fullTextEntityManager
        .getSearchFactory()
        .buildQueryBuilder()
        .forEntity(Customer.class)
        .overridesForField("firstName", "edgeNGram_query")
        .overridesForField("lastName","edgeNGram_query")
        .get()
        .simpleQueryString()
        .onFields("firstName","lastName")
        .matching(text)
        .createQuery();

I hope someone can enlighten me.

Upvotes: 0

Views: 447

Answers (1)

Guillaume Smet
Guillaume Smet

Reputation: 10539

Considering the searches will be simple as you're only matching first name and last name, I would parse the input and add a * after each token (if not already present) to trigger a wildcard search.

Using a simple query string query is the way to go so you're on the right track but you need to add a .withAndAsDefaultOperator() so that all the searched words are required.

Upvotes: 1

Related Questions