Delivery Tier Search API 

Since 11.1 we support Fluent Search API which is preferred since much easier to read and maintain in case of multiple search constraints. 

Introduction

Goal

Create and execute search queries and render the results.

Background

Hippo's delivery tier (HST) provides a Java API to create and execute searches. 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, there are examples for a Java HstComponent snippet, JSP and Freemarker snippets and a schematic (not exact!) drawing of how all involved components interact. In the child pages, we explain all steps in more detail.

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 context = request.getRequestContext();
        SearchInfo info = getComponentParametersInfo(request);
        // the scope to search below, for example /content/documents/example
        HippoBean scope = getSiteContentBaseBean(request);
        try {
            // 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 =
               context.getQueryManager().createQuery(scope,
                                                     BaseDocument.class, true);
            // always set a limit! The limit is normally the pageSize,
            // for example 10
            hstQuery.setLimit(pageSize);
            // the offset : Assume requested page is 3. Then the offset is 20
            hstQuery.setOffset(pageSize * (currentPage - 1));
            // we assume ordering on property "mynamespace:date" descending
            hstQuery.addOrderByDescending("mynamespace:date");

            // parse a free text query to remove invalid chars. The argument
            // 'false' means no wildcards allowed
            String parsedQuery = SearchInputParsingUtils.parse(query, false);

            if (StringUtils.isNotEmpty(parsedQuery)) {
                // create a filter
                Filter f = hstQuery.createFilter();
                // add a constraint: Only Documents that contain the terms
                // in parsedQuery will be a hit
                // Note that "." means: Search in the entire Document
                // including all descendant jcr nodes
                f.addContains(".", parsedQuery);
                // apply the filter
                hstQuery.setFilter(f);
            }

            // 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);
        }
    }

}

Template Code Snippets to Iterate through HippoBeans in the Result

JSP Snippet example using HstQueryResult:

<%@ include file="/WEB-INF/jspf/htmlTags.jspf" %>
<h2>
  <c:out value="${requestScope.info.title}"/> for '<c:out value="${requestScope.query}"/>' :
                                             ${requestScope.result.totalSize} results
</h2>
<ul>
  <%-- Iterate through the hippoBeans on the result --%>
  <c:forEach var="item" items="${requestScope.result.hippoBeans}">
    <hst:link var="link" hippobean="${item}"/>
    <li class="overview-item">
      <hst:cmseditlink hippobean="${item}"/>
      <a href="${link}">${item.title}</a>
      <div>
        <c:if test="${hst:isReadable(item, 'date.time')}">
          <p><fmt:formatDate value="${item.date.time}"
                             type="Date" pattern="MMMM d, yyyy h:mm a"/></p>
        </c:if>
        <p>${item.summary}</p>
      </div>
    </li>
  </c:forEach>
</ul>

Freemarker snippet:

<#include "/WEB-INF/freemarker/include/imports.ftl">
<h2>
  ${info.title?html} for '${query?html}': ${result.totalSize} results
</h2>
<ul>
  <#-- Iterate through the hippoBeans on the result -->
  <#if result?? && result.hippoBeans?has_content>
    <#list result.hippoBeans as item>
      <@hst.link var="link" hippobean=item />
      <li class="overview-item">
        <@hst.cmseditlink hippobean=item/>
        <a href="${link}">${item.title?html}</a>
        <div>
          <#if item.date?? && item.date.time??>
            <p><@fmt.formatDate value=item.date.time type="Date" pattern="MMMM d, yyyy h:mm a"/></p>
          </#if>
          <p>${item.summary?html}</p>
        </div>
      </li>
    </#list>
  </#if>
</ul>

Global Overview of Involved Components

//onehippo-prod.global.ssl.fastly.net/binaries/ninecolumn/content/gallery/connect/library/search-new.png