Equivalent of N1qlMetrics.sortCount() in ViewQuery

We are using an N1QL query which aggregates data using group by and sorts it by timestamp. It also uses offset and limit for pagination. To get the total number of records to the caller, we use the N1qlMetrics.sortCount().

Now, we are exploring the use of Map Reduce views to achieve the same behavior. I am using range keys (startKey, endKey) with skip and limit along with group and reduce (for aggregation) to fetch the required data. Is there an easy way to get the total number of records within my startKey and endKey similar to what is returned by the N1qlMetrics.sortCount() ?

Strangely ViewResult.totalRows() always returns zero, even though it is returning the correct number of records as per the limit mentioned in the ViewQuery.

We are using Couchbase 5.1 and Java SDK Client 2.5.6

The total rows is returned by the cluster, and IIRC it is the actual total number of items in the view, not just those that qualify for the range you specified. If you’re seeing 0, that sounds like an issue or a problem with the view definition.

The cluster’s view service doesn’t have an interface that returns the number of rows at the moment, so they’d need to be counted at the client. You should be able to do this by just waiting for the results and counting them. We might be able to add API to do this counting for you, but it’d be doing approximately the same thing.