Expand my Community achievements bar.

SOLVED

limit getNodes

Avatar

Level 2

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 ..

1 Accepted Solution

Avatar

Correct answer by
Administrator

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

View solution in original post

8 Replies

Avatar

Level 10

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_fullt...

Avatar

Correct answer by
Administrator

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

Avatar

Level 10

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.

Avatar

Employee Advisor

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

Avatar

Level 10

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.  

Avatar

Level 2

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

Avatar

Level 10

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

Avatar

Employee Advisor

Hi,

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

kind regards,
Jörg