Package com.day.cq.search
Interface QueryBuilder
public interface QueryBuilder
QueryBuilder is a service for building Queries
searching the Java Content Repository and which are easily extensible. The
individual constraints of a Query are expressed as Predicates, which are pluggable through OSGi mechanisms, ie. one can easily
define a new PredicateEvaluator that maps some higher-level search constraint
to an explicit JCR XPath expression, can filter the search result for
constraints that cannot be expressed via XPath and automatically provide
Facets based on the search results.
This service allows to create queries with several convenience methods, for example directly from a request using a fixed convention for the structure of request parameters that make up the predicates. In addition, it allows to store queries in the repository and load them again.
Example usages
All examples assume thatqueryBuilder is available as an OSGi
SCR reference.
- 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:
Mapinvalid input: '<'String, String> map = new HashMapinvalid input: '<'String, String>(); 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(); ...
- Since:
- 5.2
-
Method Summary
Modifier and TypeMethodDescriptionvoidInvalidates the facet cache, removing all currently cached facets and forcing a new facet extraction on future queries.createQuery(PredicateGroup predicates, Session session) Creates a query with the given predicate group as root group.createQuery(Session session) Creates an "empty" query, ie.Loads a saved query from the given repository path.voidstoreQuery(Query query, String path, boolean createFile, Session session) Saves a query at the given repository path.
-
Method Details
-
createQuery
Creates an "empty" query, ie. a query without any constraints. UseQuery.getPredicates()to get the root predicate group in order to add predicates viaPredicateGroup.add(Predicate).- Parameters:
session- the JCR session for the query- Returns:
- a query without any predicates
-
createQuery
Creates a query with the given predicate group as root group.- Parameters:
predicates- a predicate group used as the root groupsession- the JCR session for the query- Returns:
- a query to execute
-
loadQuery
Loads a saved query from the given repository path. The path can either point to a String property or to annt:filenode that contains the query as a text file (ie. in thejcr:content/jcr:dataproperty). In both cases, the query definition is expected as a multi-line string in the java properties file format, based on the map readerPredicateConverter.createPredicates(java.util.Map).To store a query, use
storeQuery(Query, String, boolean, Session).- Parameters:
path- location of the saved querysession- the JCR session to access the repository- Returns:
- a query loaded from the repository path, ready to execute, or
nullif the query could not be found - Throws:
IOException- if reading from the binary stream failedRepositoryException- if something went wrong in the repository- Since:
- 5.3
-
storeQuery
void storeQuery(Query query, String path, boolean createFile, Session session) throws RepositoryException, IOException Saves a query at the given repository path. This will either create a simple string property or annt:filecontaining the query as a text file, depending on the 'createFile' flag. In both cases, the query definition will be stored as a multi-line string in the java properties file format, based on the map created fromPredicateConverter.createMap(PredicateGroup). If the full path to that property does not exist yet, it will be created usingnt:unstructurednodes.To load this query again, use
loadQuery(String, Session).- Parameters:
query- the query to savepath- target locationcreateFile- iftrue, annt:filewill be created under that path with the query as file contents; otherwise, if the value isfalse, a simple string property will be created at the given locationsession- the JCR session to access the repository- Throws:
IOException- if writing to the binary stream failedRepositoryException- if something went wrong in the repository- Since:
- 5.3
-
clearFacetCache
void clearFacetCache()Invalidates the facet cache, removing all currently cached facets and forcing a new facet extraction on future queries.- Since:
- 6.0
-