Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

JCR Query - Search for two components under a parent

Avatar

Community Advisor

Hi,

I am trying to find the query to extract all the pages which has two components in it. I wrote the query to extract a single component but having trouble to write the query to pull the pages which has both components.

Any help would be appreciated.

Thanks.

7 Replies

Avatar

Level 10

Do you plan to use this query in source code or run it via /crx/de? Based on the use case, it would be easy to figure out if you need SQL2, XPATH or you could use QB etc.

How deep in the hierarchy level these components are configured in node structure?  It's easy to look at just 2 node levels but if you want to go deep then it could become slightly complex and you might have to use APIs rather than native queries.

Avatar

Community Advisor

I am planning to execute it via crx/de. I don't think it makes much difference.

Our hierarchy is very deep probably 10 levels deep.

WHen using Querybuilder - you can specify your path. What is your query now?

I recommend using QueryBuilder for this query - Query Builder API

Avatar

Community Advisor

I don't want to write a program since that requires a code deployment to get the results. I am planning to execute the query in crx/de.

Here is my SQL2 query:

  

SELECT parent.* FROM [nt:unstructured] AS parent INNER JOIN [nt:base] AS child ON ISDESCENDANTNODE(child, parent)

WHERE ISDESCENDANTNODE(parent, '/content/ABC/en-us') AND (parent.[sling:resourceType] = "abc/components/structure/templateA" AND child.[sling:resourceType]='abc/components/content/componentA')

I would like to include componentB in the query as well so I can get the pages have both component A&B.

I am able to get OR working but for some reason AND is not .. will try and update if I find something

Technically, you can write a script in Groovy and run it on http://localhost:4502/apps/groovyconsole.html and you will be able to run code without the need for code deployment

The query to search 10 levels depth would be a big query and might break if it traverses more than 10k nodes.

In my opinion, it would be better to generate the query using QueryBuilder and use it however you would want to

Couple of options:

SQL2 query at same level -

SELECT * FROM [cq:PageContent] AS s WHERE ISDESCENDANTNODE(s,'/content/<project_root>) AND CONTAINS(s.[sling:resourceType], '<resource_type>')

SQL2 query for immediate child -

SELECT parent.* FROM [cq:Page] AS parent INNER JOIN [nt:base] AS child ON ISCHILDNODE(child,parent) WHERE ISDESCENDANTNODE(parent, '/content') AND child.[sling:resourceType] = 'wcm/foundation/components/responsivegrid'

Node API -  Use Node.getNodes() with Iterator to traverse each node and find the property -

https://stackoverflow.com/questions/24282510/cq-access-deep-child-nodes-property-from-current-node-c...

Use QueryBuilder to specify the depth as you mentioned -

https://helpx.adobe.com/experience-manager/6-3/sites/developing/using/reference-materials/javadoc/co...

http://www.aemcq5tutorials.com/tutorials/adobe-aem-cq5-tutorials/aem-query-builder/

property.depth : The number of additional levels to search under a node. eg. if property.depth=2 then the property is searched under

1

(@jcr:title = 'foo'or */@jcr:title = 'foo'or */*/@jcr:title = 'foo')

You could also create xpath query for traversing deep levels like mentioned below -

JCR 1.0: 6.6.3.4 Path Constraint (Content Repository for Java Technology API)  - this is JCR v1.0, it should work..

references:

http://drfits.com/jcr-sql2-query-with-examples/

http://www.6dglobal.com/blog/9-jcr-sql-2-queries-every-aem-dev-should-know-2014-10-07

https://docs.jboss.org/jbossdna/0.7/manuals/reference/html/jcr-query-and-search.html

HTH