Sunday 22 November 2015

Querying in AEM

What can we query for ..?






In a relational database one can query against table columns. But in a hierarchical content repository what can we query for.. ?

Well a tree structure has nodes and node can properties as well as child nodes as shown in picture.And every node will have an absolute path which determines its location on the tree structure.

We can create conditions with these things and that is nothing but our query.




Query is simply a set of the conditions alias predicates. Predicates available within AEM are listed below


Query Predicates
Example
Node Name

Wild Card : *
Case Sensitive
nodename=myapp
nodename=myapp* // searches for any or no character after myapp
nodename=myapp? // searches for any character  after myapp

Node Type
type=cq:Page
type=nt:unstructured
Property

Wild card : %
Case Sensitive
property=jcr:title
property.value=catalog
property.operation=equals (default if not provided)

property=jcr:title
property.value=cat% 
property.operation=like
Path
path=/content/myapp/products/catalog/en
Full Text
(Performs a word search in the  entire repository, or within a path if provided)

Wild Card: *
Non Case Sensitive
fulltext=catalog

path=/content/myapp/products
fulltext=cat*
fulltext.relPath=jcr:title

Full text searches all the content, if we need to search for a particular property relPath can be used



How do we Query ? 

AEM provides us Query Builder API to write the queries. Query Builder is very easy to use and it is a wrapper around the actual query language like XPATH, SQL2 etc. 

You just require all of our query parameters put to a map and Java Query Builder API does the rest. 

 Map<String,String> map = new HashMap<String,String> ();
 map.put("path", "
/content/myapp/products");
 map.put("type", "nt:file");

 Query query = builder.createQuery(PredicateGroup.create(map), session);
 SearchResult searchResult   =  query.getResult();
 List<Hit> resultHits   =  searchResult .getHits();


Now we can iterate through the result hits and achieve what we want.


You can find a bit more at Query Builder API


1 comment:

  1. Wow that was strange. I just wrote an extremely long comment but after I clicked submit my
    comment didn't show up. Grrrr... well I'm not writing all that over again. Anyways, just wanted to say fantastic blog!

    ReplyDelete