Extending QueryBuilder (Custom Predicate Evaluator)

Kinjal P Darji
2 min readJun 1, 2022

--

It is useful sometimes to simplify the queries in QueryBuilder by implementing a custom predicate evaluator that hides complexity and ensures an understandable semantic. Say if you’re looking up for data from some service or you want to do custom filtering on the basis of the calculations, custom predicate evaluator can be very much useful. For example, you want to find out how many page are activated after so and so date. With existing predicate evaluators, you’ll have to provide properties, operations, dateranges (see below). instead of this we can have a query structure that is human understandable like usage of “since” that says dates those are greater than or equal to the given date the type of date is lastReplicated date. This can be achieved using a custom predicate evaluator. Let’s look at the implementation of one such custom predicate evaluator.

You can make use the code provided by Adobe at below location.

Open aem-search-custom-predicate-evaluator project on GitHub

This custom predicate evaluator is simplifying below query which fetches the result paths those are activated by Admin after the date 1st June, 2022 00:00 AM (today).

path=/content

1_property=cq:lastReplicatedBy
1_property.value=admin

2_property=cq:lastReplicationAction
2_property.value=Activate

daterange.property=cq:lastReplicated
daterange.lowerBound=2022-06-01T00:00:00.000+01:00
daterange.lowerOperation=>=
orderby=@jcr:content/jcr:created
orderby.sort=desc

After the predicate is implemented and executed the query becomes

path=/content

replic.by=admin
replic.since=2022-06-01T00:00:00.000+01:00
replic.action=Activate
orderby=@jcr:content/jcr:created
orderby.sort=desc

To confirm the predicate is available for use, following link can be accessed on AEM environment.

http://localhost:4502/system/console/services?filter=%28component.factory%3Dcom.day.cq.search.eval.PredicateEvaluator%2F*%29

And below is the debug log entry

01.06.2022 21:36:19.397 *DEBUG* [[0:0:0:0:0:0:0:1] [1654099579389] GET /libs/cq/search/content/querydebug.html HTTP/1.1] com.adobe.core.search.ReplicationPredicateEvaluator xpath **cq:lastReplicated >= xs:dateTime(‘2013–01–01T00:00:00.000+01:00’) and cq:lastReplicatedBy=’admin’ and cq:lastReplicationAction=’Activate’**

--

--

Kinjal P Darji

Hi, I am an AEM architect and a certified AWS Developer — Associate.