jackrabbit-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexander Klimetschek <aklim...@day.com>
Subject Re: count function in query?
Date Mon, 19 Jul 2010 14:58:48 GMT
On Mon, Jul 19, 2010 at 16:27, Omid Milani <omilani@gmail.com> wrote:
> Hi,
> I'm implementing a tag cloud in JCR. I wanted to write a query to
> return tags and number of articles tagged with each, similar to RDBMS
> query
>  select tag, count(*) as count from article_tag and tag where
> article_tag.tag=tag.id group by tag order by count
> I expected something like this to do it in jcr
>  /tags/tag( node() |
> count(//article/tag[@tag-link=node()/@jcr:uuid])) order by count(...)
> It doesn't. I couldn't find a way to do it with sql2 and qom either,
> seems there's no count function defined in any of them.
> Now, for tag cloud, I did without that query, by keeping count of
> links in tag's node for each article tagged. But this seems an
> important feature. Is there some way to do this in JCR or Jackrabbit?
> Would it be supported in some point in future? Or should one use
> relational database cases that require such queries?

The nodes or row iterator in the JCR query result API give you the
number of results found. For example:

NodeIterator nodes = QueryResult.getNodes();
int count = nodes.getSize();
// ...

Note that RangeIterator.getSize() is allowed to return -1 for
optimizations, where the query engine itself works lazily and doesn't
count the whole result set. Such optimizations are present with
Jackrabbit 2.x now. To work around that, you can force an ordering in
the query:

//element(*, nt:file) order by @jcr:score descending

(the default sort order is implicitly "order by @jcr:score
descending", so this doesn't affect the result set)


Alexander Klimetschek

View raw message