Delivery Tier Fluent Search API
Info: Available since Hippo CMS 11.1.0.
Overview
The Delivery Tier Fluent Search API allows you to build and execute search queries in the delivery tier of Bloomreach Content using a fluent Java interface.
Purpose
Use the Fluent Search API to construct and run search queries within the delivery tier. This API is designed to be more intuitive and readable than the Legacy Search API, and is recommended for all new projects.
Architecture
The delivery tier (HST) exposes the HstQueryBuilder API for constructing search queries. When you execute an HST query, the HstQuery object is converted into a JCR XPath query and executed in the Hippo Repository. The repository returns JCR Nodes as results, which are then mapped to HippoBeans in the HstQueryResult object.
Example Usage
The following example shows how to use the Fluent Search API in a Java delivery tier component.
For more detailed guidance, see:
- Bootstrap a Query using the Fluent Search API
- Add Constraints to a Query using the Fluent Search API
Example: MySearchComponent Java Class
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); // Define the search scope, for example /content/documents/myproject HippoBean scope = requestContext.getSiteContentBaseBean(); try { // Parse the free text query, removing invalid characters. // The 'false' argument disables wildcards. 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); // Build the query: // - Search below 'scope' // - Return beans of type BaseDocument or its subtypes // - Apply the parsed query as a constraint // - Set pagination and sorting 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(); // Make the result, info, and parsedQuery available to the view request.setAttribute("result", result); request.setAttribute("info", info); request.setAttribute("query", parsedQuery); } catch (QueryException e) { throw new HstComponentException( "Exception occurred during creation or execution of HstQuery.", e); } }
Hint: For JSP and Freemarker template examples that display search results, see Render a Search Query Result.