How to combine two queries in AEM? | Community
Skip to main content
Anderson_Hamer
Level 4
August 11, 2021
Solved

How to combine two queries in AEM?

  • August 11, 2021
  • 3 replies
  • 5731 views

I am trying to get a node based on 2 conditions,
Condition number 1 - It should be /apps/myproject/templates/full-content-page template
Conditionnumber 2 - It should have property as cq:headerVal and value should be "col3"

 

I have created a query for these 2  conditions. Below query will be meet 1st condition

 

 

 

path=/content/myproject/sites/en-us 1_property=jcr:primaryType 1_property.value=cq:PageContent 2_property=cq:template 2_property.value=/apps/myproject/templates/full-content-page

 

 


This query is working for second condition

 

 

path=/content/myproject/sites/en-us 1_property=jcr:primaryType 1_property.value=nt:unstructured 2_property=cq:headerVal 2_property.value=col3

 

 

 

But it is not working when i combine these 2 queries and run

 

 

path=/content/myproject/sites/en-us 1_property=jcr:primaryType 1_property.value=cq:PageContent 2_property=cq:template 2_property.value=/apps/myproject/templates/full-content-page 3_property=jcr:primaryType 3_property.value=nt:unstructured 4_property=cq:headerVal 5_property.value=col3

 

 

I can understand  why it is not working. it is trying to find both cq:template and cq:headerVal on same node but it is actually two different type of node.  Is there any way we can merge these two queries ?

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 rampai

Hi @anderson_hamer ,

 

You may need to use JCR_SQL2 for this use case.

 

To get the node:

SELECT child.* FROM [nt:unstructured] as child INNER JOIN [cq:PageContent] as parent ON ISDESCENDANTNODE(child, parent) WHERE ISDESCENDANTNODE(parent, '/content/myproject/sites/en-us') AND child.[cq:headerVal] = 'col3' AND parent.[cq:template] ='/apps/myproject/templates/full-content-page'

 

or to get the pageContent:

 

SELECT parent.* FROM [nt:unstructured] as child INNER JOIN [cq:PageContent] as parent ON ISDESCENDANTNODE(child, parent) WHERE ISDESCENDANTNODE(parent, '/content/myproject/sites/en-us') AND child.[cq:headerVal] = 'col3' AND parent.[cq:template] ='/apps/myproject/templates/full-content-page'

 

Thanks,

Ram

3 replies

Kishore_Kumar_
Level 9
August 11, 2021

hi @anderson_hamer ,

 

Can you please try with this

 

type=cq:Page
path=/content/myproject/sites/en-us
1_property=jcr:content/cq:template
1_property.value=/apps/myproject/templates/full-content-page
2_property=jcr:content/*/cq:headerVal
2_property.value=col3
path.self=true
p.limit=-1
Anderson_Hamer
Level 4
August 11, 2021
Anderson_Hamer
Level 4
August 11, 2021

Regex is not supported fully though, it supports wild card characters still and above query is working for only one depth. If you know the depth of 2nd property we can use like below.

 

type=cq:Page
path=/content/myproject/sites/en-us
1_property=jcr:content/cq:template
1_property.value=/apps/myproject/templates/full-content-page
2_property=cq:headerVal
2_property.value=col3
2_property.depth=5
path.self=true
p.limit=-1

Kishore - This dept "2_property.depth=5" works 🙂 thanks. But it is returning the pageContent(page). I need to see the node where cq:headerVal lives. Can you tell me why it is not retuning node?

Shubham_borole
Community Advisor
Community Advisor
August 11, 2021

Hi,

 

Can you try this

path=/content/myproject/sites/en-us
1_group.1_property=jcr:primaryType
1_group.1_property.value=cq:PageContent
1_group.2_property=cq:template
1_group.2_property.value=/apps/myproject/templates/full-content-page
1_group.p.and=true
2_group.3_property=jcr:primaryType
2_group.3_property.value=nt:unstructured
2_group.4_property=cq:headerVal
2_group.4_property.value=col3
2_group.p.and=true

Also in your final query I see a 5_ in the last line, should it be 4_? Can you try below too?

path=/content/myproject/sites/en-us
1_property=jcr:primaryType
1_property.value=cq:PageContent
2_property=cq:template
2_property.value=/apps/myproject/templates/full-content-page
3_property=jcr:primaryType
3_property.value=nt:unstructured
4_property=cq:headerVal
4_property.value=col3
Anderson_Hamer
Level 4
August 11, 2021

Sorry, zero result. This groping is not helping.

rampai
Community Advisor
rampaiCommunity AdvisorAccepted solution
Community Advisor
August 11, 2021

Hi @anderson_hamer ,

 

You may need to use JCR_SQL2 for this use case.

 

To get the node:

SELECT child.* FROM [nt:unstructured] as child INNER JOIN [cq:PageContent] as parent ON ISDESCENDANTNODE(child, parent) WHERE ISDESCENDANTNODE(parent, '/content/myproject/sites/en-us') AND child.[cq:headerVal] = 'col3' AND parent.[cq:template] ='/apps/myproject/templates/full-content-page'

 

or to get the pageContent:

 

SELECT parent.* FROM [nt:unstructured] as child INNER JOIN [cq:PageContent] as parent ON ISDESCENDANTNODE(child, parent) WHERE ISDESCENDANTNODE(parent, '/content/myproject/sites/en-us') AND child.[cq:headerVal] = 'col3' AND parent.[cq:template] ='/apps/myproject/templates/full-content-page'

 

Thanks,

Ram

Anderson_Hamer
Level 4
August 11, 2021

Great. Thank you Ram. It works (y). Can we do index if in case of sql2 query?

rampai
Community Advisor
Community Advisor
August 12, 2021

Yes @anderson_hamer. You can use the oakutils to generate the custom oakindex.