Sunday 6 December 2015

Query Builder

  • QueryBuilder is an API used to build Queries for Query Engine.
  • The generated queries are compatible with HTML forms also.
  • Allows us to add and remove conditions
  • Allows us to copy and paste of Queries
  •  Easily extensible
  • This QueryBuilder resides at server side and accepts a query description, creates and runs an XPath query.
  • The Query Description is simply a set of predicates.
  • Predicates are nothing but conditions. For each predicate type there is an evaluator component PredicateEvaluator that knows how to handle that specific predicate for XPath, filtering and facet extraction.
  • Standard Predicates:
    •  path
    •  property
    • type
    • fulltext
    •  range
    •  daterange
    • nodename
    •  similar
    • tagid & tag
    • language
    • event
    •  Also we can have the following predicates: 
      • group of predicates
      • like brackets 
      • order by predicates

  • Example usages

     From HTTP Form Post Request:
     Session session = request.getResourceResolver().adaptTo(Session.class);
     Query query = queryBuilder.createQuery(PredicateGroup.create(request.getParameterMap()), session);
     SearchResult result = query.getResult();
     ...
     
    From key/value map:
     Map map = new HashMap();
     map.put("path", "/content");
     map.put("type", "nt:file");
     Query query = builder.createQuery(PredicateGroup.create(map), session);
     SearchResult result = query.getResult();
     ...
     
    From predicates:
     PredicateGroup group = new PredicateGroup();
     group.add(new Predicate("mypath", "path").set("path", "/content"));
     group.add(new Predicate("mytype", "type").set("type", "nt:file"));
     Query query = builder.createQuery(group, session);
     SearchResult result = query.getResult();
     ...
    


QueryBuilder Debugger:
You can test and debug the queries at this URL


Sample Application that retrieves the images from a DAM

<%@include file="/libs/foundation/global.jsp"%><%
%><%@page session="false"%>
<%@page import="com.day.cq.tagging.*, com.day.cq.wcm.api.*" %>


<%@page
import ="java.util.*,
javax.jcr.*,
org.apache.sling.api.resource.*,
org.apache.sling.api.scripting.*,
org.apache.sling.jcr.api.*,
com.day.cq.search.*,
com.day.cq.dam.api.Asset,
com.day.cq.search.result.*"
%>


<%        SlingRepository slingRep=sling.getService (SlingRepository.class);
            Session session=slingRep.loginAdministrative (null);
            //Instantiation of Map Object useful for storing the Query Description
Map<String, String> map=new HashMap<String, String>();
QueryBuilder queryBuilder;
            //Writing Query Description that goes to form a Query
map.put ("type", "dam:Asset");
map.put ("property", "jcr:content/metadata/dc:format");
map.put ("property.value", "image/jpeg");
            //resource resolving
queryBuilder=resource.getResourceResolver ().adaptTo(QueryBuilder.class);
            //creating query based on the Query Description
Query query=queryBuilder.createQuery (PredicateGroup.create(map),session);
            //Getting and storing the Results
SearchResult searchRes=query.getResult ();
String assPath=null;
            //Iterating for the no of times the requested resource found
for (Hit hit:searchRes.getHits()){
                       //Storing the path where the resource found
           String path1=hit.getPath ();
                       //Getting the resource from the path found
           Resource rs=resourceResolver.getResource (path1);
                       //Storing the resource into Asset object
          Asset asset=rs.adaptTo (Asset.class);
                      //Getting the path from the Asset object where the fetched data stored
          assPath=asset.getPath();
                      //finally rendering the asset data on the Page
             %><img src="<%=asset.getRendition ("cq5dam.thumbnail.140.100.png").getPath() %>" ></img>
<%
}
%>


No comments :

Post a Comment