Search in sources :

Example 16 with SearchResult

use of com.day.cq.search.result.SearchResult in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ContentFragmentListImpl method initModel.

@PostConstruct
private void initModel() {
    // Default path limits search to DAM
    if (StringUtils.isEmpty(parentPath)) {
        parentPath = DEFAULT_DAM_PARENT_PATH;
    }
    if (StringUtils.isEmpty(modelPath)) {
        LOG.warn("Please provide a model path");
        return;
    }
    Session session = resourceResolver.adaptTo(Session.class);
    if (session == null) {
        LOG.warn("Session was null therefore no query was executed");
        return;
    }
    QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
    if (queryBuilder == null) {
        LOG.warn("Query builder was null therefore no query was executed");
        return;
    }
    Map<String, String> queryParameterMap = new HashMap<>();
    queryParameterMap.put("path", parentPath);
    queryParameterMap.put("type", NT_DAM_ASSET);
    queryParameterMap.put("p.limit", Integer.toString(maxItems));
    queryParameterMap.put("1_property", JcrConstants.JCR_CONTENT + "/data/cq:model");
    queryParameterMap.put("1_property.value", modelPath);
    if (StringUtils.isNotEmpty(orderBy)) {
        queryParameterMap.put("orderby", "@" + orderBy);
        if (StringUtils.isNotEmpty(sortOrder)) {
            queryParameterMap.put("orderby.sort", sortOrder);
        }
    }
    ArrayList<String> allTags = new ArrayList<>();
    if (tagNames != null && tagNames.length > 0) {
        allTags.addAll(Arrays.asList(tagNames));
    }
    if (!allTags.isEmpty()) {
        // Check for the taggable mixin
        queryParameterMap.put("2_property", JcrConstants.JCR_CONTENT + "/metadata/" + JcrConstants.JCR_MIXINTYPES);
        queryParameterMap.put("2_property.value", TagConstants.NT_TAGGABLE);
        // Check for the actual tags (by default, tag are or'ed)
        queryParameterMap.put("tagid.property", JcrConstants.JCR_CONTENT + "/metadata/cq:tags");
        for (int i = 0; i < allTags.size(); i++) {
            queryParameterMap.put(String.format("tagid.%d_value", i + 1), allTags.get(i));
        }
    }
    PredicateGroup predicateGroup = PredicateGroup.create(queryParameterMap);
    Query query = queryBuilder.createQuery(predicateGroup, session);
    SearchResult searchResult = query.getResult();
    LOG.debug("Query statement: '{}'", searchResult.getQueryStatement());
    // Query builder has a leaking resource resolver, so the following work around is required.
    ResourceResolver leakingResourceResolver = null;
    try {
        // Iterate over the hits if you need special information
        Iterator<Resource> resourceIterator = searchResult.getResources();
        while (resourceIterator.hasNext()) {
            Resource resource = resourceIterator.next();
            if (leakingResourceResolver == null) {
                // Get a reference to QB's leaking resource resolver
                leakingResourceResolver = resource.getResourceResolver();
            }
            DAMContentFragment contentFragmentModel = new DAMContentFragmentImpl(resource, contentTypeConverter, null, elementNames);
            items.add(contentFragmentModel);
        }
    } finally {
        if (leakingResourceResolver != null) {
            // Always close the leaking query builder resource resolver
            leakingResourceResolver.close();
        }
    }
}
Also used : Query(com.day.cq.search.Query) Resource(org.apache.sling.api.resource.Resource) SearchResult(com.day.cq.search.result.SearchResult) QueryBuilder(com.day.cq.search.QueryBuilder) PredicateGroup(com.day.cq.search.PredicateGroup) DAMContentFragment(com.adobe.cq.wcm.core.components.models.contentfragment.DAMContentFragment) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Session(javax.jcr.Session) PostConstruct(javax.annotation.PostConstruct)

Aggregations

SearchResult (com.day.cq.search.result.SearchResult)16 Query (com.day.cq.search.Query)12 Resource (org.apache.sling.api.resource.Resource)11 Session (javax.jcr.Session)10 PredicateGroup (com.day.cq.search.PredicateGroup)8 QueryBuilder (com.day.cq.search.QueryBuilder)7 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (com.adobe.cq.wcm.core.components.models.List)3 Hit (com.day.cq.search.result.Hit)3 Iterator (java.util.Iterator)3 Objects (java.util.Objects)3 StreamSupport (java.util.stream.StreamSupport)3 RepositoryException (javax.jcr.RepositoryException)3 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)3 ValueMap (org.apache.sling.api.resource.ValueMap)3 ListItem (com.adobe.cq.wcm.core.components.models.ListItem)2 SimpleDataSource (com.adobe.granite.ui.components.ds.SimpleDataSource)2 SimpleSearch (com.day.cq.search.SimpleSearch)2