limit getNodes | Community
Skip to main content
New Participant
October 16, 2015
Solved

limit getNodes

  • October 16, 2015
  • 8 replies
  • 2429 views

How to limit the number of nodes returned  using Node.getNodes() in AEM 5.6.1 . or is there any option to travese the child nodes with index ., say Node.getNode(index)., of Node.getFirstChild() ?  This will be helpful in retreiving specific Node by its position, instead of getting all the nodes and then travesing ..

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by kautuk_sahni

Hi sakthiT

 

As far as i know, there is no way other than this. We need to Iterators over child nodes.

Example:-     

java.lang.Iterable<Node> custNode = JcrUtils.getChildNodes(content, "customer");

Iterator it = custNode.iterator();

 //only going to be 1 content/customer node if it exists

if (it.hasNext())

 {

 //Count the number of child nodes to customer

 Node customerRoot = content.getNode("customer");

 Iterable itCust = JcrUtils.getChildNodes(customerRoot);

 Iterator childNodeIt = itCust.iterator();          

//Count the number of customer child nodes

while (childNodeIt.hasNext())

{

 childRecs++;

 childNodeIt.next();

}

 

I hope this will answer your question.

 

Thanks and Regards

Kautuk Sahni

8 replies

edubey
New Participant
October 16, 2015

Hi, 

If you need only limited results you can use Query Builder API. Apart from this why would you need node at specified index without knowing what it contains?

This query accepts limit value:

http://localhost:4502/bin/querybuilder.json?path=/content&type=cq:Page&group.p.or=true&group.1_fulltext=Geometrixx&group.1_fulltext.relPath=jcr:content&p.offset=0&p.limit=2

kautuk_sahni
kautuk_sahniAccepted solution
Employee
October 16, 2015

Hi sakthiT

 

As far as i know, there is no way other than this. We need to Iterators over child nodes.

Example:-     

java.lang.Iterable<Node> custNode = JcrUtils.getChildNodes(content, "customer");

Iterator it = custNode.iterator();

 //only going to be 1 content/customer node if it exists

if (it.hasNext())

 {

 //Count the number of child nodes to customer

 Node customerRoot = content.getNode("customer");

 Iterable itCust = JcrUtils.getChildNodes(customerRoot);

 Iterator childNodeIt = itCust.iterator();          

//Count the number of customer child nodes

while (childNodeIt.hasNext())

{

 childRecs++;

 childNodeIt.next();

}

 

I hope this will answer your question.

 

Thanks and Regards

Kautuk Sahni

Kautuk Sahni
edubey
New Participant
October 16, 2015

Hi,

JCR API does not provide any direct method. 

You can use getNodes(java.lang.String namePattern), if you are following some specific pattern in node names.

I guess you can build your own getIndex() method as well with the help of iterator, but its complexity will be O(n). It also depends upon how many nodes you have and how many times you make this request.

I still feel, you can get a better answer from community if you share your use case.

joerghoh
Employee
October 16, 2015

Hi,

what's the reason, that you need to access (let's say) the 3rd childnode of a node? I cannot think of any reason doing this. When you design your structure use names instead of relying on the order of specific subnodes.

kind regards,
Jörg

smacdonald2008
New Participant
October 16, 2015

Write a method that queries nodes (using JCR SQL2 for example) and then reduce the Node result set. This will make the result set a lot smaller and quicker to iterate.  

sakthiTAuthor
New Participant
October 16, 2015

I see SQL Query could help in this situation, but how much is it better on performance aspect ?

edubey
New Participant
October 16, 2015

Hi,

These 2 great post will help you to answer your question on query builder performance:-

  1. http://itgumby.github.io/blog/2014/10/cq-queries-demystified/
  2. http://stackoverflow.com/questions/29717580/cq-querybuilder-api-why

I guess, you can perform some test as well on your own to compare iteration in JCR node vs query builder.

Thanks

joerghoh
Employee
October 16, 2015

Hi,

iterating the child nodes is definitly faster than finding them via JCR query.

kind regards,
Jörg