AEM Developer Series
- Day 00: AEM Developer Series
- Day 01: Introduction to AEM
- Day 02: AEM Architecture
- Day 03: Setting up AEM Development Environment
- Day 04: Developing First OSGi Bundle
- Day 05: Working with Sling Servlets in AEM
- Day 06: Playing with Sling Post Servlet
- Day 07: Creating your first component in AEM
- Day 09: Dueling with Java Use API
- Day 10: Getting to know Sling Models
- Day 11: Client Libraries in Action
- Day 12: Creating your custom OSGi Configuration
- Day 13: Schedulers in AEM
- Day 14: Eventing in AEM
- Day 15: Custom Workflows in AEM
- Day 16: Creating JMX Beans in AEM
- Day 17: Working with QueryBuilder API
- Day 18: Working with Granite Datasources in AEM
- Day 19: Replication API in Action
- Day 20: Working with Users and Groups in AEM
- 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.
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.
Happy Coding 😊