Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Query builder

Avatar

Level 2

Hi,

Can anyone please help me on how can i write querybuilder query for the following requirement.

There are pages with the below structure.

/content/project/page1/childpage1/jcr:content/par/1

/content/project/page1/childpage1/jcr:content/par/2

/content/project/page1/childpage1/jcr:content/par/3

 Which means under par, any number of nodes (i.e 1,2,3,4...)can be there and those are of type nt:unstructured and has a property as documentType.

 

Now my requirement is to write a query that checks under  the page path /content/project/page1/ for particular documentType property(Ex:PDF)

Can anyone please help on this.

Thanks,

Mahi

1 Accepted Solution

Avatar

Correct answer by
Level 8

Hi @maheswariv26797,

Try executing below query in QueryBuilder debugger console

 

http://localhost:4502/libs/cq/search/content/querydebug.html

 

type=nt:unstructured

path=/content/project/page1
1_property=documentType
1_property.value=PDF
p.limit=-1

 

Java:

Session session = resourceResolver.adaptTo(Session.class);
Map<String, String> queryPredicateMap = new HashMap<>();
queryPredicateMap.put("type", JcrConstants.NT_UNSTRUCTURED);
queryPredicateMap.put("path", "/content/project/page1");
queryPredicateMap.put("p.limit", "-1");
queryPredicateMap.put("1_property", "documentType");
queryPredicateMap.put("1_property.value", "PDF");
Query query = queryBuilder.createQuery(PredicateGroup.create(queryPredicateMap), session);
SearchResult searchResult = query.getResult();
Iterator<Resource> resourceIterator = searchResult.getResources();
Resource documentResource = null;

while (resourceIterator.hasNext()) {
documentResource = resourceIterator.next();
}

 

Hope this helps!

View solution in original post

2 Replies

Avatar

Correct answer by
Level 8

Hi @maheswariv26797,

Try executing below query in QueryBuilder debugger console

 

http://localhost:4502/libs/cq/search/content/querydebug.html

 

type=nt:unstructured

path=/content/project/page1
1_property=documentType
1_property.value=PDF
p.limit=-1

 

Java:

Session session = resourceResolver.adaptTo(Session.class);
Map<String, String> queryPredicateMap = new HashMap<>();
queryPredicateMap.put("type", JcrConstants.NT_UNSTRUCTURED);
queryPredicateMap.put("path", "/content/project/page1");
queryPredicateMap.put("p.limit", "-1");
queryPredicateMap.put("1_property", "documentType");
queryPredicateMap.put("1_property.value", "PDF");
Query query = queryBuilder.createQuery(PredicateGroup.create(queryPredicateMap), session);
SearchResult searchResult = query.getResult();
Iterator<Resource> resourceIterator = searchResult.getResources();
Resource documentResource = null;

while (resourceIterator.hasNext()) {
documentResource = resourceIterator.next();
}

 

Hope this helps!