Package com.day.cq.search
Interface QueryBuilder
-
public interface QueryBuilder
QueryBuilder
is a service for buildingQueries
searching the Java Content Repository and which are easily extensible. The individual constraints of aQuery
are expressed asPredicates
, which are pluggable through OSGi mechanisms, ie. one can easily define a newPredicateEvaluator
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 provideFacets
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:
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(); ...
- Since:
- 5.2
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
clearFacetCache()
Invalidates the facet cache, removing all currently cached facets and forcing a new facet extraction on future queries.Query
createQuery(PredicateGroup predicates, Session session)
Creates a query with the given predicate group as root group.Query
createQuery(Session session)
Creates an "empty" query, ie.Query
loadQuery(java.lang.String path, Session session)
Loads a saved query from the given repository path.void
storeQuery(Query query, java.lang.String path, boolean createFile, Session session)
Saves a query at the given repository path.
-
-
-
Method Detail
-
createQuery
Query createQuery(Session session)
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
Query createQuery(PredicateGroup predicates, Session session)
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
Query loadQuery(java.lang.String path, Session session) throws RepositoryException, java.io.IOException
Loads a saved query from the given repository path. The path can either point to a String property or to annt:file
node that contains the query as a text file (ie. in thejcr:content/jcr:data
property). 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
null
if the query could not be found - Throws:
java.io.IOException
- if reading from the binary stream failedRepositoryException
- if something went wrong in the repository- Since:
- 5.3
-
storeQuery
void storeQuery(Query query, java.lang.String path, boolean createFile, Session session) throws RepositoryException, java.io.IOException
Saves a query at the given repository path. This will either create a simple string property or annt:file
containing 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:unstructured
nodes.To load this query again, use
loadQuery(String, Session)
.- Parameters:
query
- the query to savepath
- target locationcreateFile
- iftrue
, annt:file
will 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:
java.io.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
-
-