< Return to Feed
Haris Cajic - 07.21.2017

Understanding Groups in AEM Query Builder

AEM Query Builder features groups, which are needed in order to create complex xpath expressions. For example, we may want to OR two complex expressions:

(SELECT SOMETHING BASED ON CONSTRAINTS) OR (SELECT SOMETHING BASED ON CONSTRAINTS) 

Groups are fairly simple, so let’s build a few expressions in order of increasing complexity.

Search through a path for a nodetype of Page with a title of Haris OR containing title of Scott:


(Title IS Haris OR Title IS Scott)


path= /content/geometrixx/en
 
type.type = cq.Page
group.p.or = true
group.1_property = @jcr:content/jcr:title
group.1_property.1_value = Haris
group.2_property = @jcr:content/jcr:title
group.2_property.value = Scott

Let’s expand on this example by selecting a Page which has either a title of Haris OR Scott AND a Property containsDataTable which is set to true:

(Title IS Haris OR Title IS Scott) AND (containsDataTable IS TRUE)


path= /content/geometrixx/en
type.type = cq.Page
1_group.p.or = true
1_group.1_property = @jcr:content/jcr:title
1_group.1_property.value = Haris
1_group.2_property = @jcr:content/jcr:title
1_group.2_property.value = Scott
2_group.1_property = containsDataTable
2_group.1_property.value = true

By default, groups are ANDed together. What if we wanted the query we just built to read as follows:

(Title IS Haris OR Title IS Scott) OR (containsDataTable IS TRUE)


We would have to group together the left and right groups in order to apply an OR. The statement would look like the following:


((Title IS Haris OR Title IS Scott) OR (containsDataTable IS TRUE))


path= /content/geometrixx/en
type.type = cq.Page
1_group.p.or = true
1_group.1_group.p.or = true
1_group.1_group.1_property = @jcr:content/jcr:title
1_group.1_group.1_property.value = Haris
1_group.1_group.2_property = @jcr:content/jcr:title
1_group.1_group.2_property.value = Scott
1_group.2_group.1_property = containsDataTable
1_group.2_group.1_property.value = true

Notice that we had to apply the OR to both the inner and outer grouping.


Let’s expand even more by nesting some more groups.


We want to select a Page which has either a title of Haris OR Scott AND a Property containsDataTable which is set to true OR a Page that just has the Title of Jason.


(((Title IS Haris OR Title IS Scott) OR (containsDataTable IS TRUE)) OR (Title IS Justin))


path= /content/geometrixx/en
type.type = cq.Page
1_group.p.or = true
1_group.1_group.p.or = true
1_group.1_group.1_group.p.or = true
1_group.1_group.1_group.1_property = @jcr:content/jcr:title
1_group.1_group.1_group.1_property.value = Haris
1_group.1_group.1_group.2_property = @jcr:content/jcr:title
1_group.1_group.1_group.2_property.value = Scott
1_group.1_group.2_group.1_property = containsDataTable
1_group.1_group.2_group.1_property.value = true
1_group.1_property = @jcr:content/jcr:title
1_group.1_propert.value = Justin

Hopefully this will give you a clearer understanding of how groups are structured in query builder. If you can visualize the groups by writing them out in a simple format like I did in this post, then you should be able to write some pretty complex queries using groups.