On Fri, May 8, 2009 at 12:03 PM, Aleshko, Oleg
<[email protected]> wrote:
> Hi!
> Is there a way to get number of nodes for xpath query without iterating 
> through returned collection?
> When I try to use count xpath function I'm getting an exception saying that 
> it's not supported.
> This code works, but I feel that there must be  a better solution.
>        final QueryManager queryManager = 
> getObjectContentManager().getQueryManager();
>
>        final Iterator objectIterator = 
> getObjectContentManager().getObjectIterator(queryManager.createQuery(
>                
> queryManager.createFilter(NewsComponentType.class).addJCRExpression(
>                        
> "topicSet/collection-element/topic/collection-element/property/collection-element/@value='"
>                                + productType + "'")));
>
>        // returns wrong size in some cases, see NodeIteratorImpl#getSize
>        // return (int) ((RangeIterator) objectIterator).getSize();
>        return Iterators.size(objectIterator);

No, there is no count() function in JCR Xpath, because of the way the
result is given through node or row iterators. NodeIterator and
RowIterator both have a getSize method from the RangeIterator
interface [1]. But as noted in the comments of your code snippet, this
depends on the optimization configurations of your search index [2].
"resultFetchSize" can limit the number of results that are fetched
initially - anything beyond that will only be loaded once the iterator
actually reaches it. Hence the real size is unknown at the beginning.
There might also be other parameters that influence wether this
lazy-loading approach is done.

[1] 
http://www.day.com/maven/jsr170/javadocs/jcr-1.0/javax/jcr/RangeIterator.html
[2] http://wiki.apache.org/jackrabbit/Search

Regards,
Alex

-- 
Alexander Klimetschek
[email protected]

Reply via email to