How to implement property preference in Query Builder | Community
Skip to main content
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 aanchal-sikka

Hello @naruk89179065 

 

abc property exists:

 

path=/content/wknd/language-masters/en/errors type=cq:Page property=jcr:content/abc property.operation=exists

 

 

abc property does not exist:

 

path=/content/wknd/language-masters/en/errors type=cq:Page property=jcr:content/abc property.operation=exists property.value=false

 

 

abc property does not exist and def property=123

 

path=/content/wknd/language-masters/en/errors type=cq:Page 1_property=jcr:content/abc 1_property.operation=exists 1_property.value=false 2_property=jcr:content/def 2_property.value=123

 

Either (abc property exists) or (abc does not exist and def property=123)

Here 1_group is used to establish Or relation between the 2 conditions.

2_group is used to group "abc does not exist and def property=123"

 

path=/content/wknd/language-masters/en/errors type=cq:Page 1_group.1_property=jcr:content/abc 1_group.1_property.operation=exists 1_group.2_group.1_property=jcr:content/abc 1_group.2_group.1_property.operation=exists 1_group.2_group.1_property.value=false 1_group.2_group.2_property=jcr:content/def 1_group.2_group.2_property.value=123 1_group.p.or=true

 

 

 

2 replies

aanchal-sikka
Community Advisor
aanchal-sikkaCommunity AdvisorAccepted solution
Community Advisor
June 10, 2023

Hello @naruk89179065 

 

abc property exists:

 

path=/content/wknd/language-masters/en/errors type=cq:Page property=jcr:content/abc property.operation=exists

 

 

abc property does not exist:

 

path=/content/wknd/language-masters/en/errors type=cq:Page property=jcr:content/abc property.operation=exists property.value=false

 

 

abc property does not exist and def property=123

 

path=/content/wknd/language-masters/en/errors type=cq:Page 1_property=jcr:content/abc 1_property.operation=exists 1_property.value=false 2_property=jcr:content/def 2_property.value=123

 

Either (abc property exists) or (abc does not exist and def property=123)

Here 1_group is used to establish Or relation between the 2 conditions.

2_group is used to group "abc does not exist and def property=123"

 

path=/content/wknd/language-masters/en/errors type=cq:Page 1_group.1_property=jcr:content/abc 1_group.1_property.operation=exists 1_group.2_group.1_property=jcr:content/abc 1_group.2_group.1_property.operation=exists 1_group.2_group.1_property.value=false 1_group.2_group.2_property=jcr:content/def 1_group.2_group.2_property.value=123 1_group.p.or=true

 

 

 

Aanchal Sikka
ManviSharma
Adobe Employee
Adobe Employee
June 12, 2023

Hi,

 

To write a query builder query with a preference for a specific property and fallback to another property if the first one doesn't exist, you can use the "orderby" clause in the query.

Here's an example of how you can write the query:

 

// Assuming you are using the QueryBuilder API in Java

QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
Session session = resourceResolver.adaptTo(Session.class);

String query = "path=/content/mypath";
query += "&type=cq:Page";
query += "&orderby.property=propertyName1";
query += "&orderby.property.operation=exist";
query += "&orderby.property.2=propertyName2";

Query resultQuery = queryBuilder.createQuery(PredicateGroup.create(Predicates.createOrPredicates()), session);
resultQuery.setStart(0);
resultQuery.setHitsPerPage(10);

SearchResult result = resultQuery.getResult();

 

 

 

In the example above, the query is set to order by "propertyName1" with the operation "exist", meaning it will prioritize results where "propertyName1" exists. If "propertyName1" is not found, the query will then order by "propertyName2".