Delivery Tier Fluent Search API
Introduction
Goal
Create and execute search queries using the Fluent Search API in Hippo's delivery tier.
Background
As of Hippo CMS 11.1, Hippo's delivery tier (HST) provides a Fluent Java API through HstQueryBuilder to create and execute search queries. This API provides functionalities equivalent to those of the Legacy Search API but is more intuitive to use and results in better code readability. It is, therefore, the recommended search API for all new implementation projects.
When an HST query is executed, the HstQuery object is translated into a JCR XPath query, which is executed in the Hippo Repository. The Hippo Repository returns JCR Nodes as hits, that in turn are mapped to HippoBeans in the HstQueryResult object.
Examples
Below is an example of a Java HstComponent using the Fluent Search API.
More detailed documentation about using the Fluent Search API can be found on the following pages:
- Bootstrap a Query using the Fluent Search API
- Add Constraints to a Query using the Fluent Search API
MySearchComponent Java Code Snippet
Java Class MySearchComponent:
public class MySearchComponent extends BaseHstComponent { @Override public void doBeforeRender(final HstRequest request, final HstResponse response) throws HstComponentException { HstRequestContext requestContext = request.getRequestContext(); SearchInfo info = getComponentParametersInfo(request); // the scope to search below, for example /content/documents/myhippoproject HippoBean scope = requestContext.getSiteContentBaseBean(); try { // parse a free text query to remove invalid chars. The argument // 'false' means no wildcards allowed String query = getPublicRequestParameter(request, "q"); String parsedQuery = SearchInputParsingUtils.parse(query, false); int pageSize = NumberUtils.toInt(getPublicRequestParameter(request, "ps"), 10); int pageNum = NumberUtils.toInt(getPublicRequestParameter(request, "pn"), 1); // create the query to search below 'scope', return beans that are // of type BaseDocument bean or a subclass/sub-jcr-types, the // third argument, 'true', indicates whether to include subtypes HstQuery hstQuery = HstQueryBuilder.create(scope) .ofTypes(BaseDocument.class) .where(constraint(".").contains(parsedQuery)) .limit(pageSize) .offset(pageSize * (pageNum - 1)) .orderByDescending("mynamespace:date") .build(); // execute the query HstQueryResult result = hstQuery.execute(); // set the result, info and parsedQuery on the HstRequest : It is // then available in the JSP request.setAttribute("result", result); request.setAttribute("info", info); request.setAttribute("query", parsedQuery); } catch (QueryException e) { throw new HstComponentException( "Exception occured during creation or execution of HstQuery.", e); } }