public Search()

in reference/src/main/java/org/apache/sling/cms/reference/models/Search.java [70:126]


    public Search(@Self SlingHttpServletRequest request, @ValueMapValue @Named("limit") int limit,
            @OSGiService SearchService searchService, @ValueMapValue @Named("basePath") String basePath) {
        this.request = request;

        Set<String> distinct = new HashSet<>();
        String term = Text.escapeIllegalXpathSearchChars(request.getParameter(TERM_PARAMETER)).replace("'", "''");
        List<Resource> queryResults = new ArrayList<>();

        resolver = searchService.getResourceResolver(request);

        String query = "SELECT * FROM [sling:Page] AS p WHERE [jcr:content/sling:published]=true AND (p.[jcr:content/hideInSitemap] <> true OR [jcr:content/hideInSitemap] IS NULL) AND ISDESCENDANTNODE(p, '"
                + basePath + "') AND CONTAINS(p.*, '" + term + "')";
        log.debug("Searching for pages with {} under {} with query: {}", term, basePath, query);
        Iterator<Resource> res = resolver.findResources(query, Query.JCR_SQL2);
        while (res.hasNext()) {
            Resource result = res.next();
            if (!distinct.contains(result.getPath())) {
                queryResults.add(result);
                distinct.add(result.getPath());
            }
        }
        count = queryResults.size();
        log.debug("Found {} results", count);

        if (StringUtils.isNotBlank(request.getParameter("page")) && request.getParameter("page").matches("\\d+")) {
            page = Integer.parseInt(request.getParameter("page"), 10) - 1;
            log.debug("Using page {}", page);
        } else {
            page = 0;
            log.debug("Page not specified or not valid");
        }

        if (page * limit >= count) {
            start = count;
        } else {
            start = page * limit;
        }
        log.debug("Using start {}", start);

        if ((page * limit) + limit >= count) {
            end = count;
        } else {
            end = (page * limit) + limit;
        }
        log.debug("Using end {}", end);
        results = queryResults.subList(start, end);

        List<Integer> pgs = new ArrayList<>();
        int max = ((int) Math.ceil((double) count / limit)) + 1;
        for (int i = 1; i < max; i++) {
            pgs.add(i);
        }
        pages = pgs.toArray(new Integer[pgs.size()]);
        if (log.isDebugEnabled()) {
            log.debug("Loaded pages {}", Arrays.toString(pages));
        }
    }