This is a long outstanding request, that apache doesn't seem to want
to fully support. There's workarounds though.

Here's what I've written in the past;

Oak doesn't guarantee the count to be accurate, it might be an
estimate. You can get the estimate by sorting by jcr:score. Just
append order by [jcr:score] to your query.

To make sure you have a correct count, you really need to iterate over
all elements and count yourself. One might not agree with the design
decisions leading to requiring such a workaround, but that's where
it's at. Here's an example method that takes offset and page size into
account when loading a paged query result with correct total count;

    private QueryResults<Asset> toAssets(NodeIterator iterator, int
offset, int pageSize, Session session)
        throws RepositoryException {

        QueryResults<Asset> assets = new QueryResults<>(); //
structure holding entries to return, total count, and some other
properties
        List<Asset> entries = new ArrayList<>();
        int totalCount = 0;

        Date timestamp = new Date(); int count = (pageSize < 1 ?
Integer.MIN_VALUE : 0);
        while (iterator.hasNext()&& offset-- > 0 ) {
            iterator.nextNode();
            ++totalCount;
        }

        while (iterator.hasNext() && count++ < pageSize) {
            Node node = iterator.nextNode();
            entries.add(toAsset(node, session)); // only load items
that we're requesting
            ++totalCount;
        }

        // count remaining items
        while (iterator.hasNext()) {
            iterator.nextNode();
            ++totalCount;
        }

        assets.setTotalCount(totalCount);
        assets.setEntries(entries);
        return assets;

On Wed, 9 Mar 2022 at 06:25, Vishnu Aggarwal
<[email protected]> wrote:
>
> Hello Team,
>
> The use case is to draw pagination in UI for all the nodes fetched on the
> basis of a search query. For that, we fetch 10 records on one page. But
> also, we need to get the total number of records matching our query.
>
> Currently, there doesn't seem to get the count directly for a query, as we
> do in RDBMS like SELECT COUNT(*) FROM <TABLE_NAME> WHERE <SOME_FILTERS>.
> What is being done right now is to get all the nodes in memory and
> calculate the size which would be costly in terms of processing as the
> number of nodes will increase.
>
> There was a ticket created for this
> https://issues.apache.org/jira/browse/OAK-9319. Any update for that? Or any
> workaround/help to get the count of nodes in quick time response.
>
> Regards,
> Vishnu Aggarwal



-- 
-Tor

Reply via email to