JCR Query - Search for two components under a parent

Avatar

Avatar
Validate 25
MVP
Singaiah_Chintalapudi
MVP

Likes

131 likes

Total Posts

234 posts

Correct reply

39 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 5
Ignite 3
View profile

Avatar
Validate 25
MVP
Singaiah_Chintalapudi
MVP

Likes

131 likes

Total Posts

234 posts

Correct reply

39 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 5
Ignite 3
View profile
Singaiah_Chintalapudi
MVP

16-01-2019

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.

Replies

Avatar

Avatar
Boost 250
MVP
Gaurav-Behl
MVP

Likes

250 likes

Total Posts

1,147 posts

Correct reply

283 solutions
Top badges earned
Boost 250
Establish
Give back 300
Give Back 50
Give Back 5
View profile

Avatar
Boost 250
MVP
Gaurav-Behl
MVP

Likes

250 likes

Total Posts

1,147 posts

Correct reply

283 solutions
Top badges earned
Boost 250
Establish
Give back 300
Give Back 50
Give Back 5
View profile
Gaurav-Behl
MVP

17-01-2019

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

Avatar
Validate 25
MVP
Singaiah_Chintalapudi
MVP

Likes

131 likes

Total Posts

234 posts

Correct reply

39 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 5
Ignite 3
View profile

Avatar
Validate 25
MVP
Singaiah_Chintalapudi
MVP

Likes

131 likes

Total Posts

234 posts

Correct reply

39 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 5
Ignite 3
View profile
Singaiah_Chintalapudi
MVP

17-01-2019

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.

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

17-01-2019

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

I recommend using QueryBuilder for this query - Query Builder API

Avatar

Avatar
Boost 250
MVP
Gaurav-Behl
MVP

Likes

250 likes

Total Posts

1,147 posts

Correct reply

283 solutions
Top badges earned
Boost 250
Establish
Give back 300
Give Back 50
Give Back 5
View profile

Avatar
Boost 250
MVP
Gaurav-Behl
MVP

Likes

250 likes

Total Posts

1,147 posts

Correct reply

283 solutions
Top badges earned
Boost 250
Establish
Give back 300
Give Back 50
Give Back 5
View profile
Gaurav-Behl
MVP

17-01-2019

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

Avatar

Avatar
Validate 25
MVP
Singaiah_Chintalapudi
MVP

Likes

131 likes

Total Posts

234 posts

Correct reply

39 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 5
Ignite 3
View profile

Avatar
Validate 25
MVP
Singaiah_Chintalapudi
MVP

Likes

131 likes

Total Posts

234 posts

Correct reply

39 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 5
Ignite 3
View profile
Singaiah_Chintalapudi
MVP

17-01-2019

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.

Avatar

Avatar
Validate 25
MVP
bsloki
MVP

Likes

182 likes

Total Posts

1,859 posts

Correct reply

545 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Boost 50
Boost 5
View profile

Avatar
Validate 25
MVP
bsloki
MVP

Likes

182 likes

Total Posts

1,859 posts

Correct reply

545 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Boost 50
Boost 5
View profile
bsloki
MVP

21-01-2019

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