Chapter 5. Querying

The second most important capability of Hibernate Search is the ability to execute a Lucene query and retrieve entities managed by an Hibernate session, providing the power of Lucene without living the Hibernate paradigm, and giving another dimension to the Hibernate classic search mechanisms (HQL, Criteria query, native SQL query).

To access the Hibernate Search™ querying facilities, you have to use an Hibernate FullTextSession . A Search Session wraps a regular org.hibernate.Session to provide query and indexing capabilities.

Session session = sessionFactory.openSession();
...
FullTextSession fullTextSession = Search.createFullTextSession(session);    

The search facility is built on native Lucene queries.

org.apache.lucene.queryParser.QueryParser parser = new QueryParser("title", new StopAnalyzer() );

org.apache.lucene.search.Query luceneQuery = parser.parse( "summary:Festina Or brand:Seiko" );
org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery( luceneQuery );
        

List result = fullTextQuery.list(); //return a list of managed objects    

The Hibernate query built on top of the Lucene query is a regular org.hibernate.Query , you are in the same paradigm as the other Hibernate query facilities (HQL, Native or Criteria). The regular list() , uniqueResult() , iterate() and scroll() can be used.

For people using Java Persistence (aka EJB 3.0 Persistence) APIs of Hibernate, the same extensions exist:

EntityManager em = entityManagerFactory.createEntityManager();

FullTextEntityManager fullTextEntityManager = 
    org.hibernate.hibernate.search.jpa.Search.createFullTextEntityManager(em);

...
org.apache.lucene.queryParser.QueryParser parser = new QueryParser("title", new StopAnalyzer() );

org.apache.lucene.search.Query luceneQuery = parser.parse( "summary:Festina Or brand:Seiko" );
javax.persistence.Query fullTextQuery = fullTextEntityManager.createFullTextQuery( luceneQuery );

List result = fullTextQuery.getResultList(); //return a list of managed objects  

The following examples show the Hibernate APIs but the same example can be easily rewritten with the Java Persistence API by just adjusting the way the FullTextQuery is retrieved.

5.1. Building queries

Hibernate Search queries are built on top of Lucene queries. It gives you a total freedom on the kind of Lucene queries you are willing to execute. However, once built, Hibernate Search abstract the query processing from your application using org.hibernate.Query as your primary query manipulation API.

5.1.1. Building a Lucene query

This subject is generally speaking out of the scope of this documentation. Please refer to the Lucene documentation or Lucene In Action.

5.1.2. Building a Hibernate Search query

5.1.2.1. Generality

Once the Lucene query is built, it needs to be wrapped into an Hibernate Query.

FullTextSession fullTextSession = Search.createFullTextSession( session );
org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery( luceneQuery );

If not specified otherwise, the query will be executed against all indexed entities, potentially returning all types of indexed classes. It is advised, from a performance point of view, to restrict the returned types:

org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery( luceneQuery, Customer.class );
//or
fullTextQuery = fullTextSession.createFullTextQuery( luceneQuery, Item.class, Actor.class );

The first example returns only matching customers, the second returns matching actors and items.

5.1.2.2. Pagination

It is recommended to restrict the number of returned objects per query. It is a very common use case as well, the user usually navigate from one page to an other. The way to define pagination is exactly the way you would define pagination in a plain HQL or Criteria query.

org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery( luceneQuery, Customer.class );
fullTextQuery.setFirstResult(15); //start from the 15th element
fullTextQuery.setMaxResults(10); //return 10 elements

Note

It is still possible to get the total number of matching elements regardless of the pagination. See getResultSize() below

5.1.2.3. Sorting

Apache Lucene provides a very flexible and powerful way to sort results. While the default sorting (by relevance) is appropriate most of the time, it can interesting to sort by one or several properties.

Inject the Lucene Sort object to apply a Lucene sorting strategy to an Hibernate Search.

org.hibernate.search.FullTextQuery query = s.createFullTextQuery( query, Book.class );
org.apache.lucene.search.Sort sort = new Sort(new SortField("title"));
query.setSort(sort);
List results = query.list();

One can notice the FullTextQuery interface which is a sub interface of org.hibernate.Query.

Fields used for sorting must not be tokenized.

5.1.2.4. Fetching strategy

When you restrict the return types to one class, Hibernate Search loads the objects using a single query. It also respects the static fetching strategy defined in your domain model.

It is often useful, however, to refine the fetching strategy for a specific use case.

Criteria criteria = s.createCriteria( Book.class ).setFetchMode( "authors", FetchMode.JOIN );
s.createFullTextQuery( luceneQuery ).setCriteriaQuery( criteria );

In this example, the query will return all Books matching the luceneQuery. The authors collection will be loaded from the same query using an SQL outer join.

When defining a criteria query, it is not needed to restrict the entity types returned while creating the Hibernate Search query from the full text session: the type is guessed from the criteria query itself. Only fetch mode can be adjusted, refrain from applying any other restriction.

One cannot use setCriteriaQuery if more than one entity type is expected to be returned.

5.1.2.5. Projection

For some use cases, returning the domain object (graph) is overkill. Only a small subset of the properties is necessary. Hibernate Search allows you to return a subset of properties:

org.hibernate.search.FullTextQuery query = s.createFullTextQuery( luceneQuery, Book.class );
query.setProjection( "id", "summary", "body", "mainAuthor.name" );
List results = query.list();
Object[] firstResult = (Object[]) results.get(0);
Integer id = firstResult[0];
String summary = firstResult[1];
String body = firstResult[2];
String authorName = firstResult[3];

Hibernate Search extracts the properties from the Lucene index and convert them back to their object representation, returning a list of Object[]. Projections avoid a potential database round trip (useful if the query response time is critical), but has some constraints:

  • the properties projected must be stored in the index (@Field(store=Store.YES)), which increase the index size

  • the properties projected must use a FieldBridge implementing org.hibernate.search.bridge.TwoWayFieldBridge or org.hibernate.search.bridge.TwoWayStringBridge, the latter being the simpler version. All Hibernate Search built-in types are two-way.

Projection is useful for another kind of usecases. Lucene provides some metadata informations to the user about the results. By using some special placeholders, the projection mechanism can retrieve them:

org.hibernate.search.FullTextQuery query = s.createFullTextQuery( luceneQuery, Book.class );
query.setProjection( FullTextQuery.SCORE, FullTextQuery.BOOST, FullTextQuery.THIS, "mainAuthor.name" );
List results = query.list();
Object[] firstResult = (Object[]) results.get(0);
float score = firstResult[0];
float boost = firstResult[1];
Book book = firstResult[2];
String authorName = firstResult[3];

You can mix and match regular fields and special placeholders. Here is the list of available placeholders:

  • FullTextQuery.THIS: returns the intialized and managed entity (as a non projected query would have done)

  • FullTextQuery.DOCUMENT: returns the Lucene Document related to the object projected

  • FullTextQuery.SCORE: returns the document score in the query. The score is guatanteed to be between 0 and 1 but the highest score is not necessarily equals to 1. Scores are handy to compare one result against an other for a given query but are useless when comparing the result of different queries.

  • FullTextQuery.BOOST: the boost value of the Lucene Document

  • FullTextQuery.ID: the id property value of the projected object

  • FullTextQuery.DOCUMENT_ID: the Lucene document id. Careful, Lucene document id can change overtime between two different IndexReader opening (this feature is experimental)

5.2. Retrieving the results

Once the Hibernate Search query is built, executing it is in no way different than executing a HQL or Criteria query. The same paradigm and object semantic apply. All the common operations are available: list(), uniqueResult(), iterate(), scroll().

5.2.1. Performance considerations

If you expect a reasonable number of results (for example using pagination) and expect to work on all of them, list() or uniqueResult() are recommended. list() work best if the entity batch-size is set up properly. Note that Hibernate Search has to process all Lucene Hits elements (within the pagination) when using list() , uniqueResult() and iterate().

If you wish to minimize Lucene document loading, scroll() is more appropriate. Don't forget to close the ScrollableResults object when you're done, since it keeps Lucene resources. If you expect to use scroll but wish to load objects in batch, you can use query.setFetchSize(): When an object is accessed, and if not already loaded, Hibernate Search will load the next fetchSize objects in one pass.

Pagination is a preferred method over scrolling though.

5.2.2. Result size

It is sometime useful to know the total number of matching documents:

  • for the Google-like feature 1-10 of about 888,000,000

  • to implement a fast pagination navigation

  • to implement a multi step search engine (adding approximation if the restricted query return no or not enough results)

But it would be costly to retrieve all the matching documents.

Hibernate Search allows you to retrieve the total number of matching documents regardless of the pagination parameters. Even more interesting, you can retrieve the number of matching elements without triggering a single object load.

org.hibernate.search.FullTextQuery query = s.createFullTextQuery( luceneQuery, Book.class );
assert 3245 == query.getResultSize(); //return the number of matching books without loading a single one

org.hibernate.search.FullTextQuery query = s.createFullTextQuery( luceneQuery, Book.class );
query.setMaxResult(10);
List results = query.list();
assert 3245 == query.getResultSize(); //return the total number of matching books regardless of pagination

Note

Like Google, the number of results is approximative if the index is not fully up-to-date with the database (asynchronous cluster for example).

5.2.3. ResultTransformer

Especially when using projection, the data structure returned by a query (an object array in this case), is not always matching the application needs. It is possible to apply a ResultTransformer operation post query to match the targeted data structure:

org.hibernate.search.FullTextQuery query = s.createFullTextQuery( luceneQuery, Book.class );
query.setProjection( "title", "mainAuthor.name" );

query.setResultTransformer( 
    new StaticAliasToBeanResultTransformer( BookView.class, "title", "author" ) 
);
List<BookView> results = (List<BookView>) query.list();
for(BookView view : results) {
    log.info( "Book: " + view.getTitle() + ", " + view.getAuthor() );
}

Examples of ResultTransformer implementations can be found in the Hibernate Core codebase.

5.3. Filters

Apache Lucene has a powerful feature that allows to filters results from a query according to a custom filtering process. This is a very powerful way to apply some data restrictions after a query, especially since filters can be cached and reused. Some interesting usecases are:

  • security

  • temporal data (eg. view only last month's data)

  • population filter (eg. search limited to a given category)

  • and many more

Hibernate Search pushes the concept further by introducing the notion of parameterizable named filters which are transparantly cached. For people familiar with the notion of Hibernate Core filters, the API is very similar.

fullTextQuery = s.createFullTextQuery( query, Driver.class );
fullTextQuery.enableFullTextFilter("bestDriver");
fullTextQuery.enableFullTextFilter("security").setParameter( "login", "andre" );
fullTextQuery.list(); //returns only best drivers where andre has credentials

In this example we enabled 2 filters on top of this query. You can enable (or disable) as many filters as you want.

Declaring filters is done through the @FullTextFilterDef annotation. This annotation can be on any @Indexed entity regardless of the filter operation.

@Entity
@Indexed
@FullTextFilterDefs( {
    @FullTextFilterDef(name = "bestDriver", impl = BestDriversFilter.class, cache=false), //actual Filter implementation
    @FullTextFilterDef(name = "security", impl = SecurityFilterFactory.class) //Filter factory with parameters
})
public class Driver { ... }

Each named filter points to an actual filter implementation.

public class BestDriversFilter extends org.apache.lucene.search.Filter {

    public BitSet bits(IndexReader reader) throws IOException {
        BitSet bitSet = new BitSet( reader.maxDoc() );
        TermDocs termDocs = reader.termDocs( new Term("score", "5") );
        while ( termDocs.next() ) {
            bitSet.set( termDocs.doc() );
        }
        return bitSet;
    }
}

BestDriversFilter is an example of a simple Lucene filter that will filter all results to only return drivers whose score is 5. The filters must have a no-arg constructor when referenced in a FulltextFilterDef.impl.

The cache flag, defaulted to true, tells Hibernate Search to search the filter in its internal cache and reuses it if found.

Note that, usually, filter using the IndexReader are wrapped in a Lucene CachingWrapperFilter to benefit from some caching speed improvement. If your Filter creation requires additional steps or if the filter you are willing to use does not have a no-arg constructor, you can use the factory pattern:

@Entity
@Indexed
@FullTextFilterDef(name = "bestDriver", impl = BestDriversFilterFactory.class) //Filter factory
public class Driver { ... }

public class BestDriversFilterFactory {

    @Factory
    public Filter getFilter() {
        //some additional steps to cache the filter results per IndexReader
        Filter bestDriversFilter = new BestDriversFilter();
        return new CachingWrapperFilter(bestDriversFilter);
    }
}

Hibernate Search will look for a @Factory annotated method and use it to build the filter instance. The factory must have a no-arg constructor. For people familiar with JBoss Seam, this is similar to the component factory pattern, but the annotation is different!

Named filters comes in handy where the filters have parameters. For example a security filter needs to know which credentials you are willing to filter by:

fullTextQuery = s.createFullTextQuery( query, Driver.class );
fullTextQuery.enableFullTextFilter("security").setParameter( "level", 5 );

Each parameter name should have an associated setter on either the filter or filter factory of the targeted named filter definition.

public class SecurityFilterFactory {
    private Integer level;

    /**
     * injected parameter
     */
    public void setLevel(Integer level) {
        this.level = level;
    }

    @Key
    public FilterKey getKey() {
        StandardFilterKey key = new StandardFilterKey();
        key.addParameter( level );
        return key;
    }

    @Factory
    public Filter getFilter() {
        Query query = new TermQuery( new Term("level", level.toString() ) );
        return new CachingWrapperFilter( new QueryWrapperFilter(query) );
    }
}

Note the method annotated @Key and returning a FilterKey object. The returned object has a special contract: the key object must implement equals / hashcode so that 2 keys are equals if and only if the given Filter types are the same and the set of parameters are the same. In other words, 2 filter keys are equal if and only if the filters from which the keys are generated can be interchanged. The key object is used as a key in the cache mechanism.

@Key methods are needed only if:

  • you enabled the filter caching system (enabled by default)

  • your filter has parameters

In most cases, using the StandardFilterKey implementation will be good enough. It delegates the equals/hashcode implementation to each of the parameters equals and hashcode methods.

Why should filters be cached? There are two area where filter caching shines:

  • the system does not update the targeted entity index often (in other words, the IndexReader is reused a lot)

  • the Filter BitSet is expensive to compute (compared to the time spent to execute the query)

Cache is enabled by default and use the notion of SoftReferences to dispose memory when needed. To adjust the size of the hard reference cache, use hibernate.search.filter.cache_strategy.size (defaults to 128). Don't forget to use a CachingWrapperFilter when the filter is cacheable and the Filter's bits methods makes use of IndexReader.

For advance use of filter caching, you can implement your own FilterCachingStrategy. The classname is defined by hibernate.search.filter.cache_strategy.

5.4. Optimizing the query process

Query performance depends on several criteria:

  • the Lucene query itself: read the literature on this subject

  • the number of object loaded: use pagination (always ;-) ) or index projection (if needed)

  • the way Hibernate Search interacts with the Lucene readers: defines the appropriate Reader strategy.

5.5. Native Lucene Queries

If you wish to use some specific features of Lucene, you can always run Lucene specific queries. Check Chapter 8, Accessing Lucene natively for more informations.