spring-bootlucenehibernate-searchhibernate-search-6

Is it possible to entirely replace Lucenes MultiFieldQueryParser with Hibernate Search 6


I am about to migrate from Hibernate 5 to hibernate search 6 and are looking for some advice. As I understand a main intention of the version 6 is to decouple the API more from the underlying implementation. I have something like this in my code:

FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
SearchFactory searchFactory = fullTextEntityManager.getSearchFactory();
QueryBuilder queryBuilder = searchFactory.buildQueryBuilder().forEntity(Service.class).get();
BooleanJunction<?> bool = queryBuilder.bool();
Analyzer analyzer = searchFactory.getAnalyzer("standard");

if (StringUtils.isNotBlank(searchString)) {
    MultiFieldQueryParser multiFieldQueryParser = new MultiFieldQueryParser(new String[] {
            "name", "description", "company.name", "configuration.name",
    }, analyzer);
    multiFieldQueryParser.setDefaultOperator(QueryParserBase.AND_OPERATOR);
    org.apache.lucene.search.Query querySearchString = multiFieldQueryParser.parse(searchString);
    bool.must(querySearchString);
}

Query fullTextQuery = fullTextEntityManager.createFullTextQuery(bool.createQuery(), Service.class);
return fullTextQuery.getResultList();

MultiFieldQueryParser, QueryParserBase and Query are classes from lucene backend. Is it possible to realize the same query with pure hibernate search query syntax (Query DSL)?

I found simple examples for fields but could I use something like "company.name" for a related entity field annotated with @IndexedEmbedded ?

Thanks and best Regards Matt

Thanks


Solution

  • Yes, that should be possible. Something along the next lines:

    searchSession.search( Service.class )
        .where( f -> f.and(
                f.match().field( "name" ).matching( searchString ),
                f.match().field( "description" ).matching( searchString )
                f.match().field( "company.name" ).matching( searchString )
        ) )
        .fetchHits( .. )
    

    Look at the documentation for nested structure here - https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#mapper-orm-indexedembedded-structure. There are examples of what you are looking for. Also, it is recommended to take a look at the migration guide - https://docs.jboss.org/hibernate/search/6.0/migration/html_single/. It contains a lot of helpful info on migration from 5 to 6