0
votes

Root contains one folder, named pending of type sling:folder.

That have numbers of nodes of nt:unstructured type, having name of long value, that long value is very important for my code processing.

Now I want to get top 20 nodes(20 minimum node name , i.e., long value) data from this pending folder.

Can you tell me how can I write the JCR query for this situation ?

Edit No. 1

Repository repository = JcrUtils.getRepository("http://localhost:4502/crx/server");
Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));

// Obtain the query manager for the session via the workspace ...
QueryManager queryManager = session.getWorkspace().getQueryManager();    
// Create a query object ...

String expression = "SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE([/pending])";
Query query = queryManager.createQuery(expression, javax.jcr.query.Query.JCR_SQL2);

// Execute the query and get the results ...
QueryResult result = query.execute();

// Iterate over the nodes in the results ...

NodeIterator nodeIter = result.getNodes();

But it gives some order , different than the order present in root node. But that is not in sort form.

Edit No.2

Now I got the functionality of this function. And it working fine now. The thing that I got is order the node just above the destination node, that is mentioned in second parameter of this function.

But the nodes that is coming is of different names(a number). So how can I sort this using orderBefore. Because everytime we are not able to know the right location(destination Relative Path) where we have to put this node.

1
So the node name is a string of digits that can be interpreted as a long value? Do I understand you correctly? And you want to get the 20 nodes with names that correspond to the highest numerical values?David Gorsline
Yes exactly. I read on one blog that sling:orderedFolder helps in this case. Can you please help me in that.devsda
@DavidGorsline Can you give me some guidance, please. In solving above problem.devsda

1 Answers

1
votes

You probably don't need a query for this, if you have structure such as

/pending/1
/pending/2
...
/pending/999

you can just iterate over the nodes using the JCR Node's getNodes() method, which returns a NodeIterator.

A sling:orderedFolder node type for "pending" gives a predictable ordering of the child nodes.

In general, using the tree structure instead of queries is more efficient in JCR.

Note also that if you're using Jackrabbit having more than about 10'000 child nodes on the same parent can lead to performance issues.