Expand my Community achievements bar.

SOLVED

aem 6.2 querybuilder api not returning results

Avatar

Level 4

Hi

I am using aem 6.2 version. am trying to query JCR in a service to retrieve information at /content/dam using query builder API but search results are returned as zero. Is there anything wrong in this snippet or any pre-requisite to use querybuilder API. 

Map<String,String> map = new HashMap<String,String>();

map.put("path", "/content/dam");

map.put("type", "dam:Asset");

Query damQuery = queryBuilder.createQuery(PredicateGroup.create(map), jcrSession);

SearchResult searchItems = damQuery.getResult();

logger.debug("DAM search query : {}", searchItems.getQueryStatement());

logger.debug("search total match : {}", searchItems.getTotalMatches());

Iterator<Node> nodes = searchItems.getNodes();

while(nodes.hasNext()) {

    Node node = nodes.next();

    logger.debug("node name : {}",node.getName());

}

 

This is the query generated in logs query=select [jcr:path], [jcr:score], * from [nt:base] as a where isdescendantnode(a, '/content/dam') /* xpath: /jcr:root/content/dam//* */, path=/content/dam//*); . If i try to execute same query via CRX query builder screen, it works. Same is not working via API call.

1 Accepted Solution

Avatar

Correct answer by
Administrator

Hi 

Please try to follow this article:-

Link:- https://helpx.adobe.com/experience-manager/using/htl_querybuilder.html  [This is tested article, This article would work for you.]

//  Creating a HTML Template Language component that uses the AEM QueryBuilder API

    Code Used here is:- 

// create query description as hash map 
                Map<String, String> map = new HashMap<String, String>();        
                        map.put("path", "/content");
                map.put("type", "cq:Page");
                map.put("group.p.or", "true"); // combine this group with OR
                map.put("group.1_fulltext", fulltextSearchTerm);
                map.put("group.1_fulltext.relPath", "jcr:content");
                map.put("group.2_fulltext", fulltextSearchTerm);
                map.put("group.2_fulltext.relPath", "jcr:content/@cq:tags");
                // can be done in map or with Query methods
                map.put("p.offset", "0"); // same as query.setStart(0) below
                map.put("p.limit", "20"); // same as query.setHitsPerPage(20) below
                                   
              Query query = builder.createQuery(PredicateGroup.create(map), session);
                query.setStart(0);
                query.setHitsPerPage(20);
                             
                SearchResult result = query.getResult();

I hope this would help you.

~kautuk



Kautuk Sahni

View solution in original post

4 Replies

Avatar

Employee

Are you using the same user/permissions?

Avatar

Level 4

  Yes. i have got resourceResolver via ResourceResolverFactory.getResourceResolver(null). Is there any explicit check to be done.

Avatar

Correct answer by
Administrator

Hi 

Please try to follow this article:-

Link:- https://helpx.adobe.com/experience-manager/using/htl_querybuilder.html  [This is tested article, This article would work for you.]

//  Creating a HTML Template Language component that uses the AEM QueryBuilder API

    Code Used here is:- 

// create query description as hash map 
                Map<String, String> map = new HashMap<String, String>();        
                        map.put("path", "/content");
                map.put("type", "cq:Page");
                map.put("group.p.or", "true"); // combine this group with OR
                map.put("group.1_fulltext", fulltextSearchTerm);
                map.put("group.1_fulltext.relPath", "jcr:content");
                map.put("group.2_fulltext", fulltextSearchTerm);
                map.put("group.2_fulltext.relPath", "jcr:content/@cq:tags");
                // can be done in map or with Query methods
                map.put("p.offset", "0"); // same as query.setStart(0) below
                map.put("p.limit", "20"); // same as query.setHitsPerPage(20) below
                                   
              Query query = builder.createQuery(PredicateGroup.create(map), session);
                query.setStart(0);
                query.setHitsPerPage(20);
                             
                SearchResult result = query.getResult();

I hope this would help you.

~kautuk



Kautuk Sahni

Avatar

Level 4

Thanks Kautuk.

I was able to resolve it earlier, issue was i wasn't getting admin resource resolver. Now i've service user to obtain resource resolver and obtaining search results via that. Thanks for your response.