Monday, April 22, 2013

How to create custom query predicate in CQ

Use Case: Your search has some custom requirement that can not be covered by existing predicate

Pre requisite:
Example: 

Suppose you want to create a custom predicate to copare and sort case sensitive property.
Your predicate expression will look like

-----Signature -----

path=<Path under which search would be performed>
type=<node type>
customcase.property=<property name for which case sensitive search needs to be performed>
customcase.fulltext=<Search Text>
customcase.case=upper/lower/UPPER/LOWER/no_case/NO_CASE
orderby=customcase

You can test these example after deploying code HOST:PORT/libs/cq/search/content/querydebug.html

---- Example 1 (Find all node with subtitle as "MAIL" and do upper case compare) -----
path=/content/geometrixx/en
type=cq:Page
customcase.property=jcr:content/subtitle
customcase.fulltext=MAIL
customcase.case=upper
orderby=customcase

---- Example 2 (find all node and sort by subtitle property) -----
path=/content/geometrixx/en
type=cq:Page
customcase.property=jcr:content/subtitle
customcase.case=no_case
orderby=customcase



Note: As you can see, Code is not optimal. It is just an example of how you can create your own predicate. Also Example assumes that you know how to import dependencies for this code. Let me know if you have any question.