Prashant Kamble
Prashant Kamble

Reputation: 145

How to use QueryBuilder and Search String in hibernate search 6?

We wanted use QueryBuilder object in hibernate search 6 .... Something like this

<code>

 QueryBuilder qb = fullTextEntityManager
                    .getSearchFactory()
                    .buildQueryBuilder()
                    .forEntity(getItemClass())
                    .get();

            Query query = createQuery(qb, search);
            Sort sort = createSort(qb);
</code>

We were using hibernate-search 5 and need to migrate to 6

Upvotes: 1

Views: 948

Answers (1)

yrodiere
yrodiere

Reputation: 9977

This is explained in great details in the migration guide:

https://docs.jboss.org/hibernate/search/6.0/migration/html_single/#searching

Something like this in Hibernate Search 5:

int pageIndex = ...;
int pageSize = ...;
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager( em );
QueryBuilder qb = fullTextEntityManager.getSearchFactory()
        .buildQueryBuilder().forEntity( Book.class ).get();

org.apache.lucene.search.Query luceneQuery = qb.keyword()
        .onField( "title" ).boostedTo( 3 )
        .matching( "robot" )
        .createQuery();

FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery( luceneQuery, Book.class );
fullTextQuery.setFirstResult( pageIndex * pageSize );
fullTextQuery.setMaxResults( pageSize );

List hits = fullTextQuery.getResultList();
int totalHitCount = fullTextQuery.getResultSize();

... would become this in Hibernate Search 6:

int pageIndex = ...;
int pageSize = ...;
SearchSession session = Search.session( entityManager );

SearchResult<Book> result = session.search( Book.class )
        .where( f -> f.match().field( "title" ).matching( "robot" ).boost( 3 ) )
        .fetch( pageIndex * pageSize, pageSize );

List<Book> hits = result.hits();
long totalHitCount = result.total().hitCount();

Upvotes: 1

Related Questions