Query Builder API Exclude Pages or Paths

This blog article focuses on searching cq:Page under a particular path, but exclude sub pages. In this article we will share you the Query Builder API’s Query, and share with you the Java code implementation of a servlet. At the end of this article, you should be able to search cq:Page nodes under a particular path, but exclude particular resources under some paths, under the sub tree.


Query Builder API’s Query:

Find all cq:Page nodes under /content/my-site/us/en
NOT cq:Page nodes under /content/my-site/us/en/services
NOT cq:Page nodes under /content/my-site/us/en/products

1
2
3
4
5
6
7
8
9
10
path=/content/my-site/us/en
type=cq:Page

group.1_group.p.not=true
group.1_group.path=/content/my-site/us/en/services
group.1_group.path.self=true

group.2_group.p.not=true
group.2_group.path=/content/my-site/us/en/products
group.2_group.path.self=true

XPath Output:
XPath Output


Query Builder API Servlet Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.query.Query;
import org.apache.sling.query.QueryBuilder;
import org.osgi.service.component.annotations.Component;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

@Component(
    service = Servlet.class,
    property = { "sling.servlet.paths=/bin/getPages" }
)
public class GetPagesServlet extends SlingAllMethodsServlet {

    private static final int ITEMS_PER_PAGE = 10;

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
            throws ServletException, IOException {

        int pageNumber = getPageNumber(request.getParameter("pageNumber"));
        String contentPath = "/content/my-site/us/en"; // Root path for the pages.

        // Create the predicate using a Map
        Map<String, String> predicateMap = new HashMap<>();
        predicateMap.put("path", contentPath);
        predicateMap.put("type", "cq:Page");

        // Add additional predicate groups if needed
        predicateMap.put("group.1_group.p.not", "true");
        predicateMap.put("group.1_group.path", contentPath + "/services");
        predicateMap.put("group.1_group.path.self", "true");

        predicateMap.put("group.2_group.p.not", "true");
        predicateMap.put("group.2_group.path", contentPath + "/products");
        predicateMap.put("group.2_group.path.self", "true");

        List<String> paths = getPathsForPage(contentPath, pageNumber, predicateMap);

        // Convert the list of paths to JSON using Gson
        String jsonResponse = convertListToJson(paths);

        // Set the JSON response in the HTTP response
        response.setContentType("application/json");
        response.getWriter().write(jsonResponse);
    }

    private List<String> getPathsForPage(String contentPath, int pageNumber, Map<String, String> predicateMap) {
        // Use QueryBuilder to retrieve pages based on the given predicates.

        List<String> paths = new ArrayList<>();
        int startIndex = (pageNumber - 1) * ITEMS_PER_PAGE;

        QueryBuilder queryBuilder = getResourceResolver().adaptTo(QueryBuilder.class);
        if (queryBuilder != null) {
            // Set the limit and offset for pagination
            predicateMap.put("p.limit", String.valueOf(ITEMS_PER_PAGE));
            predicateMap.put("p.offset", String.valueOf(startIndex));

            Query query = queryBuilder.createQuery(predicateMap);

            Iterator<Map<String, Object>> result = query.getResult().getHits().iterator();

            // Fetch the paths for the current page
            while (result.hasNext()) {
                Map<String, Object> hit = result.next();
                String path = (String) hit.get("path");
                paths.add(path);
            }
        }

        return paths;
    }

    // Rest of the methods remain the same as in the previous version.
    // getPageNumber, convertListToJson, etc.
}

Curl Command

1
curl -X GET "http://localhost:4502/bin/getPages?pageNumber=1"

JSON Output

1
2
3
4
5
6
7
8
9
10
11
12
[
  "/content/my-site/us/en/item-1",
  "/content/my-site/us/en/item-2",
  "/content/my-site/us/en/item-3",
  "/content/my-site/us/en/item-4",
  "/content/my-site/us/en/item-5",
  "/content/my-site/us/en/item-6",
  "/content/my-site/us/en/item-7",
  "/content/my-site/us/en/item-8",
  "/content/my-site/us/en/item-9",
  "/content/my-site/us/en/item-10"
]

Hello, I am an enthusiastic Adobe Community Advisor and a seasoned Lead AEM Developer. I am currently serving as an AEM Technical Lead at MNPDigital.ca, bringing over a decade of extensive web engineering experience and more than eight years of practical AEM experience to the table. My goal is to give back to the AEM Full Stack Development community by sharing my wealth of knowledge with others. You can connect with me on LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back To Top