You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by "Aleshko, Oleg" <o....@itransition.com> on 2009/05/08 12:03:57 UTC

How to get count of nodes matching xpath query

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);


Re: How to get count of nodes matching xpath query

Posted by Alexander Klimetschek <ak...@day.com>.
On Fri, May 8, 2009 at 12:03 PM, Aleshko, Oleg
<o....@itransition.com> 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
alexander.klimetschek@day.com