I know how to search for something in the JCR via JCR SQL2 queries.
However, I would like to use Java in certain cases, using the JCR API: javax.jcr.Node
, javax.jcr.NodeIterator
and the like.
I'm afraid I will simply reinvent the wheel by coding my own.
Is there anything already available (Gist
, Github
or else
)?
I ended up writing my own implementations.
Feel free to improve or add comments for potential improvements.
Further info
Java is probably NOT the most efficient way of searching through the JCR, so mind the performance hit (vs using
JCR SQL2
).However, there are cases where using JCR SQL2 will be rather annoying. For instance: JCR SQL2 - result query order as in JCR browser
I'd recommend launching your search as low as possible in the tree.
Solution
Read the comments above each method to find our more.
Usage of first utility method
Usage of second utility method
Resources:
You could use a ItemVisitor to recursively traverse the repository collecting all items that match your criteria along the way.
E.g. printing out all properties of type
Long
:You can use for that SlingQuery. It's inspired by jQuery and follows it's syntax. You should use it only to search small number of nodes (in best case under 100), because traversal queries are slow.
Edit
Your example may be converted to the following SlingQueries (not tested):
SlingQuery is part of Apache Sling since a while, that's why the github repository seems to be abandoned.
Note: You can static import the Dollar sign and drop the static access over SlingQuery in your syntax such as $(resource).find("...");