Skip to main content

Day 17: Working with Query Builder API

AEM Developer Series

Search functionality is the backbone of almost all the enterprise web applications and AEM is no exception. AEM provides the QueryBuilder API to perform search operations in the JCR. Query Builder works in the following way -
  • Queries are converted into XPath by the Query Engine. There they are again converted into JCR SQL2.
  • JCR SQL2 query is executed by the Oak Engine.
  • Appropriate Oak Index will be picked up by the query. A query for which the index is not found becomes the traversal queries (traverses the whole repository) which should be avoided by creating an appropriate index.
  • Query Builder does not have its own index. It uses Oak index.

Query Builder

A query builder accepts a query description (in the form of predicates), creates the query and searches the JCR based on the predicates passed. Without further ado, let's see the code. 
Here we are creating the Sling Servlet that will use the Query Builder to search all the assets under /content/dam matching to the search term passed.
  • Create a class named QueryBuilderServlet and paste the following code in it.
  • Here, we are reading the query parameter param and based on this query parameter we will search the JCR in the path /content/dam
  • We are searching only the assets, which is defined by the property type=dam:Asset 
  • Now we are creating the Query object using the predicate map and the session object.
  • At last, we are getting the SearchResult from the query builder and then using the Hit object, we are getting the path of each result.
  • Deploy the code and executing the servlet by hitting the request - http://<host>:<port>/bin/demo/querybuilder?param=we.retail
  • You will get the following the result set on the browser window
1. /content/dam/we-retail-screens/we-retail-instore-logo.png
2. /content/dam/projects/we-retail/cover
3. /content/dam/we-retail-screens/italy.png
4. /content/dam/we-retail-screens/usa.png
5. /content/dam/we-retail/en/activities/running/marathon-shoes.jpg
6. /content/dam/we-retail/en/activities/running/running-woods-woman.jpg
7. /content/dam/we-retail/en/activities/running/running_4.jpg
8. /content/dam/we-retail/en/activities/running/fitness-woman.jpg
9. /content/dam/we-retail/en/activities/running/running_1.jpg
10. /content/dam/we-retail/en/activities/running/running_3.jpg
11. /content/dam/we-retail/en/activities/running/running_5.jpg
12. /content/dam/we-retail/en/activities/running/running_2.jpg
13. /content/dam/we-retail/en/activities/running/running-desert-woman.jpg
14. /content/dam/we-retail/en/activities/running/running-couple-mountain.jpg
15. /content/dam/we-retail/en/activities/snowboarding/man-snowboarder.jpg
16. /content/dam/we-retail/en/activities/snowboarding/extreme-championship-girls.jpg
17. /content/dam/we-retail/en/activities/snowboarding/freeride-downhill.jpg
18. /content/dam/we-retail/en/activities/hiking-camping/alpinists-rochefort-ridge.jpg
19. /content/dam/we-retail/en/activities/hiking-camping/hiker-anapurna.jpg
20. /content/dam/we-retail/en/activities/hiking-camping/trekker-ama-dablam.jpg


Congratulations!! 🙋 today we have created a basic search functionality in AEM JCR. I hope you enjoyed this post.

You can find the complete code of this project on my GitHub in this commit. Feel free to fork or open issues, if any.

I would love to hear your thoughts on this and would like to have suggestions from you to make it better. 

Feel free to befriend me on FacebookTwitter or Linked In or say Hi by email.

Happy Coding 😊


  1. Replies
    1. There is a QueryBuilderServlet class which is present in the page. Since it pulls data from GitHub's gists, make sure it is not blocked in your browser.

  2. I have the below working query in servlet where i am settng a custom property to each page JCR:Content


    It runs on entire Path=/content/dam, so it traverse all nodes(assuming 80000 cq:Pages) for about 20 minutes and gives results.

    How can i make it in batches when using query-builder API code-wise, like - traverse 1000 nodes and do-something code-wise and then continue with query and traversing next 1000 nodes and so-on ? Is it possible ?
    Thanks in advance.


Post a Comment

Popular posts from this blog

Day 00: AEM Developer Series

Hello everyone! Welcome to this AEM development series. We can all see the revolution of Digital Marketing today. Companies are dying to be a part of it and they have made this a war for the Digital Marketing tools.
Adobe is way ahead in this war and has gained a lot of market capture. They are leaders in the Digital Marketing platforms since 2014-15. One of the flagship product in Adobe's Digital Marketing suite is Adobe Experience Manager (AEM).
Since AEM is in huge demand, the people who know how to develop on AEM are also in huge demand. But developing on AEM is not easy as it is made up of various open-source technologies such as Apache Felix (OSGi), Apache Sling, Apache Oak and Adobe's own technologies like Granite, HTL etc. Learning all these technologies in conjunction can sometimes become confusing and frustrating 😫.
When I first started learning AEM in 2016, I was dumbfounded to see there is so much going on under the hood. I then spent months to gather all the res…

Day 05: Working with Sling Servlets in AEM

Day 03: Setting up AEM Development Environment