2020-03-21

Hibernate Search - Searching

Searching

Once the data is indexed, you can perform search queries.

The following code will prepare a search query targeting the index for the Book entity, filtering the results so that at least one field among title and authors.name matches the string Refactoring: Improving the Design of Existing Code exactly.

Using Hibernate Search to query the indexes

// Not shown: get the entity manager and open a transaction
SearchSession searchSession = Search.session( entityManager ); 

SearchResult<Book> result = searchSession.search( Book.class ) 
        .where( f -> f.match() 
                .fields( "title", "authors.name" )
                .matching( "Refactoring: Improving the Design of Existing Code" )
        )
        .fetch( 20 ); 

long totalHitCount = result.getTotalHitCount(); 
List<Book> hits = result.getHits(); 

List<Book> hits2 =
        /* ... same DSL calls as above... */
        .fetchHits( 20 ); 


// Not shown: commit the transaction and close the entity manager
Get a Hibernate Search session, called SearchSession, from the EntityManager.
Initiate a search query on the index mapped to the Book entity.
Define that only documents matching the given predicate should be returned. The predicate is created using a factory f passed as an argument to the lambda expression.
Build the query and fetch the results, limiting to the top 20 hits.
Retrieve the total number of matching entities.
Retrieve matching entities.
In case you’re not interested in the whole result, but only in the hits, you can also call fetchHits() directly.
If for some reason you don’t want to use lambdas, you can use an alternative, object-based syntax, but it will be a bit more verbose:

Using Hibernate Search to query the indexes — object-based syntax

// Not shown: get the entity manager and open a transaction
SearchSession searchSession = Search.session( entityManager ); 

SearchScope<Book> scope = searchSession.scope( Book.class ); 

SearchResult<Book> result = searchSession.search( scope ) 
        .where( scope.predicate().match() 
                .fields( "title", "authors.name" )
                .matching( "Refactoring: Improving the Design of Existing Code" )
                .toPredicate()
        )
        .fetch( 20 ); 

long totalHitCount = result.getTotalHitCount(); 
List<Book> hits = result.getHits(); 

List<Book> hits2 =
        /* ... same DSL calls as above... */
        .fetchHits( 20 ); 

// Not shown: commit the transaction and close the entity manager
Get a Hibernate Search session, called SearchSession, from the EntityManager.
Create a "search scope", representing the indexed types that will be queried.
Initiate a search query targeting the search scope.
Define that only documents matching the given predicate should be returned. The predicate is created using the same search scope as the query.
Build the query and fetch the results, limiting to the top 20 hits.
Retrieve the total number of matching entities.
Retrieve matching entities.
In case you’re not interested in the whole result, but only in the hits, you can also call fetchHits() directly.
It is possible to get just the total hit count, using fetchTotalHitCount() method.

Using Hibernate Search to count the matches

// Not shown: get the entity manager and open a transaction

SearchSession searchSession = Search.session( entityManager );

long totalHitCount = searchSession.search( Book.class )
        .where( f -> f.match()
                .fields( "title", "authors.name" )
                .matching( "Refactoring: Improving the Design of Existing Code" )
        )
        .fetchTotalHitCount(); 

// Not shown: commit the transaction and close the entity manager
Fetch the total hit count.
Note that, while the examples above retrieved hits as managed entities, it is just one of the possible hit types. See Query DSL for more information.

No comments:

Post a Comment