# TotalHitCount for Scrolling (Hibernate Search Beta 10)

**URL:** <https://discourse.hibernate.org/t/totalhitcount-for-scrolling-hibernate-search-beta-10/4633>\
**Category:** Hibernate Search\
**Created:** [September 29, 2020, 10:26am UTC](https://discourse.hibernate.org/t/totalhitcount-for-scrolling-hibernate-search-beta-10/4633 "2020-09-29T10:26:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Muller](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/peter_muller/32/1095_2.png) [@Peter\_Muller](https://discourse.hibernate.org/u/Peter_Muller)\
**Post date:** [September 29, 2020, 10:26am UTC](https://discourse.hibernate.org/t/totalhitcount-for-scrolling-hibernate-search-beta-10/4633/1 "2020-09-29T10:26:21Z")

</div>

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.

```java
...
 .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
}

```

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [September 29, 2020, 10:49am UTC](https://discourse.hibernate.org/t/totalhitcount-for-scrolling-hibernate-search-beta-10/4633/2 "2020-09-29T10:49:18Z")

</div>

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](https://hibernate.atlassian.net/browse/HSEARCH-4023). 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.

```java
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
    }
}

```

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [October 29, 2020, 12:19pm UTC](https://discourse.hibernate.org/t/totalhitcount-for-scrolling-hibernate-search-beta-10/4633/3 "2020-10-29T12:19:17Z")

</div>

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