TotalHitCount for Scrolling (Hibernate Search Beta 10)

Hello,

the scrolling is a great feature, but is there a way to get the totalHitCount without loading all JPA-Entities? It seems to me, to only way currently is to add up all sizes of the .hits() list.

...
 .scroll(20)...
...
long totalHitCount = 0;
for ( SearchScrollResult<KauffallTE> chunk = scroll.next();
                          chunk.hasHits(); chunk = scroll.next() ) {
    final List<KauffallTE> hits = chunk.hits();
    totalHitCount += hits.size();
    // do some other stuff
}

Hello,

Yes, that’s pretty much what you’d need to do. At the moment the total hit count is not exposed through the scroll, though we’ll need to change that. Thanks for the feedback!

As a workaround, if computing the sum yourself is a problem, you can simply run the query a first time before your scroll to get the total hit count.

SearchQuery<KauffallTE> query = ... .toQuery();

long totalHitCount = query.fetchTotalHitCount();
try ( Scroll<KauffallTE> scroll = query.scroll(20) ) {
    for ( SearchScrollResult<KauffallTE> chunk = scroll.next();
                              chunk.hasHits(); chunk = scroll.next() ) {
        final List<KauffallTE> hits = chunk.hits();
        // do some other stuff
    }
}
1 Like

An update on this: the total hit count will be exposed from SearchScrollResult starting with Hibernate Search 6.0.0.CR1.

1 Like