aem 6.2 querybuilder api not returning results | Community
Skip to main content
mohanb
Level 4
October 13, 2016
Solved

aem 6.2 querybuilder api not returning results

  • October 13, 2016
  • 4 replies
  • 3949 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by kautuk_sahni

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

4 replies

Feike_Visser1
Adobe Employee
Adobe Employee
October 13, 2016

Are you using the same user/permissions?

mohanb
mohanbAuthor
Level 4
October 13, 2016

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

kautuk_sahni
Community Manager
kautuk_sahniCommunity ManagerAccepted solution
Community Manager
October 13, 2016

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
mohanb
mohanbAuthor
Level 4
October 28, 2016

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.