Search code examples
aemquery-builder

Retrieve a parent node using AEM query builder


I need to retrieve a parent node using AEM Query builder.

For instance, this is my query:

path:/content/test/us/bar
1_property:product
1_property.operation:exists

2_property:product
2_property.value:8003170008212

This query allow me to retrieve the following elements:

1) /content/test/us/bar/table/jcr:content/releated/product/2  
2) /content/test/us/bar/chair/jcr:content/releated/product/1

Using this query, one can retrieve all elements placed under /content/test/us/bar which contain 8003170008212 as value of product property.

Starting from the previous bullet points I need to return just the parent, so for example:

1) /content/test/us/bar/table
2) /content/test/us/bar/chair

I can achieve my target programmatically, iterating the results and using 3 times the getParent() method.

I'am wondering: is there a way to get it with query builder?


Solution

  • If the property that you are searching for is always present at a known path, the query can be rewritten as

    path=/content/test/us/bar
    1_property=jcr:content/related/product
    1_property.operation=exists
    
    2_property=jcr:content/related/product
    2_property.value=8003170008212 
    

    This would then result in

    /content/test/us/bar/table
    /content/test/us/bar/chair
    

    which avoids looping through the result and finding the parent nodes.

    For e.g., the following query in my local environment

    path=/content/we-retail/language-masters/en
    1_property=displayMode
    1_property.operation=exists
    
    2_property=displayMode
    2_property.value=singleText
    

    results in

    /content/we-retail/language-masters/en/experience/wester-australia-by-camper-van/jcr:content/root/responsivegrid/contentfragment
    /content/we-retail/language-masters/en/experience/arctic-surfing-in-lofoten/jcr:content/root/responsivegrid/contentfragment
    /content/we-retail/language-masters/en/experience/steelhead-and-spines-in-alaska/jcr:content/root/responsivegrid/contentfragment
    /content/we-retail/language-masters/en/experience/hours-of-wilderness/jcr:content/root/responsivegrid/contentfragment
    /content/we-retail/language-masters/en/experience/skitouring/jcr:content/root/responsivegrid/contentfragment
    /content/we-retail/language-masters/en/experience/fly-fishing-the-amazon/jcr:content/root/responsivegrid/contentfragment
    

    But rewriting the following query to

    path=/content/we-retail/language-masters/en
    1_property=jcr:content/root/responsivegrid/contentfragment/displayMode
    1_property.operation=exists
    
    2_property=jcr:content/root/responsivegrid/contentfragment/displayMode
    2_property.value=singleText
    

    results in

    /content/we-retail/language-masters/en/experience/wester-australia-by-camper-van
    /content/we-retail/language-masters/en/experience/arctic-surfing-in-lofoten
    /content/we-retail/language-masters/en/experience/steelhead-and-spines-in-alaska
    /content/we-retail/language-masters/en/experience/hours-of-wilderness
    /content/we-retail/language-masters/en/experience/skitouring
    /content/we-retail/language-masters/en/experience/fly-fishing-the-amazon