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 Mon, 29 Apr 2019 at 17:27, zhouxu <zho...@docworks.cn> wrote: > > We use oak1.10.2 inside a web application to store documents > > How can we program statistics such as : > the total number of documents > compute the number of documents per property values for a given property ? > > we try these code,but the .getSize() return -1,What should I do? > > The code is as follows: > > QueryManager qm = session.getWorkspace().getQueryManager(); > Query q = qm.createQuery("SELECT * FROM [nt:file] WHERE LOCALNAME() LIKE > '%.txt' and [\"jcr:createdBy\"] = 'anonymous'", Query.JCR_SQL2); > QueryResult qr = q.execute(); > long stat = qr.getRows().getSize(); > > > > -- > Sent from: http://jackrabbit.510166.n4.nabble.com/Jackrabbit-Dev-f523400.html -- -Tor