2020-03-21

Hibernate Search - Query Fetch size

Fetch size

By default, Hibernate Search will use a fetch size of 100, meaning that for a single fetch*() call on a single query, it will run a first query to load the first 100 entities, then if there are more hits it will run a second query to load the next 100, etc.

The fetch size can be configured by setting the configuration property hibernate.search.query.loading.fetch_size. This property expects a strictly positive Integer value.

It is also possible to override the configured fetch size on a per-query basis, as shown below.

Overriding the fetch size in a single search query

SearchResult<Book> result = searchSession.search( Book.class ) 
        .where( f -> f.match()
                .field( "title" )
                .matching( "robot" ) )
        .loading( o -> o.fetchSize( 50 ) ) 
        .fetch( 200 ); 

Start building the query.
Access the loading options of the query, then set the fetch size to an arbitrary value (must be 1 or more).
Fetch the results, limiting to the top 200 hits. One query will be executed to load the hits if there are less hits than the given fetch size; two queries if there are more hits than the fetch size but less than twice the fetch size, etc.

No comments:

Post a Comment