Search code examples
javaspringlucenehibernate-search

Searching for a word that contains a 'query'


i've implemented the hibernate search orm (LATEST lucene_version) that search words contains a entry query :

    FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
    QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory()
            .buildQueryBuilder()
            .forEntity(Application.class)
            .get();

    Query queryName = queryBuilder
            .keyword().fuzzy().withEditDistanceUpTo(2).withPrefixLength(2)
            .onField("name")
            .matching("*" + q + "*")
            .createQuery();

this queryName cannot find words like 'Wikicrédit' or 'Wikipro' or 'WikiCash' if my query is 'q=Wiki'

How can i enhance those instructions to get words that start with 'Wiki%' independently of edit distance !


Solution

  • Either:

    1. Use the simpleQueryString query and type your query as Wiki*.
    2. Declare a separate name-edgengram field with an edge-ngram analyzer that will index Wikicrédit as [W, Wi, Wik, Wiki, Wikic, Wikicr, ...] and then query that field, as desribed in this answer.