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 that queryBuilder 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 Detail

      • createQuery

        Query createQuery​(Session session)
        Creates an "empty" query, ie. a query without any constraints. Use Query.getPredicates() to get the root predicate group in order to add predicates via PredicateGroup.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 group
        session - 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 an nt:file node that contains the query as a text file (ie. in the jcr: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 reader PredicateConverter.createPredicates(java.util.Map).

        To store a query, use storeQuery(Query, String, boolean, Session).

        Parameters:
        path - location of the saved query
        session - 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 failed
        RepositoryException - 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 an nt: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 from PredicateConverter.createMap(PredicateGroup). If the full path to that property does not exist yet, it will be created using nt:unstructured nodes.

        To load this query again, use loadQuery(String, Session).

        Parameters:
        query - the query to save
        path - target location
        createFile - if true, an nt:file will be created under that path with the query as file contents; otherwise, if the value is false, a simple string property will be created at the given location
        session - the JCR session to access the repository
        Throws:
        java.io.IOException - if writing to the binary stream failed
        RepositoryException - 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