Expand my Community achievements bar.

SOLVED

I want to search for nodes using node names - how can I do it? (querybuilder)

Avatar

Level 9

might be best explain with an example

 

These are 6 sample nodes

 

  1. /content/myhost/products/shoes/mens/shoe1
  2. /content/myhost/products/shoes/mens/shoe2
  3. /content/myhost/products/shoes/womens/shoe1
  4. /content/myhost/products/shoes/womens/shoe2
  5. /content/myhost/products/shoes/kids/shoe1

 

I want to return all nodes where the node name = "shoe1" BUT I do NOT want to return node5. So in my sample nodes, no1 and 3 will be included. Node5 is not included.

 

How can I do it?

 

I looked at the nodes and there's nothing in the node properties that can help me. All I have are the names.

 

Thanks

 

 

 

1 Accepted Solution

Avatar

Correct answer by
Level 4

 

To return all nodes where the node name is "shoe1" but exclude a specific node, you can use the AEM Query Builder API to construct a query that filters the nodes based on their names and paths.

Here's an example of how you might do this:

// Set up the Query Builder
QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);

// Create a query
Map<String, String> map = new HashMap<String, String>();
map.put("path", "/content/myhost/products/shoes");
map.put("type", "cq:Page");
map.put("property", "jcr:content/jcr:title");
map.put("property.value", "shoe1");
map.put("p.excludedPaths", "/content/myhost/products/shoes/kids/shoe1");

// Execute the query
Query query = queryBuilder.createQuery(PredicateGroup.create(map), resourceResolver.adaptTo(Session.class));
SearchResult result = query.getResult();

// Get the nodes from the search result
NodeIterator nodeIterator = result.getNodes();

// Iterate over the nodes and do something with them
while (nodeIterator.hasNext()) {
    Node node = nodeIterator.nextNode();
    // Do something with the node
}

Thanks,

Monendra

 

 

View solution in original post

9 Replies

Avatar

Community Advisor

Hi @jayv25585659 

To search for nodes using node name, you can use the following inside Querybuilder

 

path=/content/myhost/products/shoes
nodename=shoe1


Please give it a try. 

Lemme know in case I've misunderstood your question.

Best regards,
Himanshu Singhal

Avatar

Community Advisor

If the path/node filter logic is too complicated then you can write your own predicates and use that in a query

example https://github.com/arunpatidar02/aem63app-repo/blob/master/java/CaseInsensitiveLikePredicate.java 



Arun Patidar

Avatar

Level 9

can you please tell me how to use a custom predicate when creating my querybuilder parameter in java? thanks

Avatar

Community Advisor

https://github.com/arunpatidar02/aem63app-repo/blob/master/java/CaseInsensitiveLikePredicate.java

 

/**
* Custom case insensitive predicate for like operation e.g.
* caseinsensitive.property=jcr:content/@jcr:title
* caseinsensitive.value=queryString %
*
*/



Arun Patidar

Avatar

Community Advisor

Hi @jayv25585659 

Here is the cheetsheet which can help when writing the Queries.

It explains on how to get the search results based on node name as well

https://github.com/paulrohrbeck/aem-links/blob/master/querybuilder_cheatsheet.md

 

Hope this helps

Avatar

Correct answer by
Level 4

 

To return all nodes where the node name is "shoe1" but exclude a specific node, you can use the AEM Query Builder API to construct a query that filters the nodes based on their names and paths.

Here's an example of how you might do this:

// Set up the Query Builder
QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);

// Create a query
Map<String, String> map = new HashMap<String, String>();
map.put("path", "/content/myhost/products/shoes");
map.put("type", "cq:Page");
map.put("property", "jcr:content/jcr:title");
map.put("property.value", "shoe1");
map.put("p.excludedPaths", "/content/myhost/products/shoes/kids/shoe1");

// Execute the query
Query query = queryBuilder.createQuery(PredicateGroup.create(map), resourceResolver.adaptTo(Session.class));
SearchResult result = query.getResult();

// Get the nodes from the search result
NodeIterator nodeIterator = result.getNodes();

// Iterate over the nodes and do something with them
while (nodeIterator.hasNext()) {
    Node node = nodeIterator.nextNode();
    // Do something with the node
}

Thanks,

Monendra

 

 

Avatar

Employee

This is just for the people looking to search a node/page based on node name using the query builder.

you can get it via the below query -: 

 

path=/content/project/us/en/products
type=cq:Page
nodename=product212

 

or 

 

path=/content/dam/project/us/en
type=nt:file
nodename=product212

 

You can look for example here on this cheat sheet 

https://github.com/paulrohrbeck/aem-links/blob/master/querybuilder_cheatsheet.md