Hi All,
How to write query builder query order preference property.
EX: I have one property and will query that property if that property doesn't exist then will query another property.
@MayurSatav @aanchal-sikka @ManviSharma
Solved! Go to Solution.
Views
Replies
Total Likes
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
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
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".
Views
Likes
Replies
Views
Likes
Replies