This article covers a Hippo CMS version 7.7. There's an updated version available that covers our most recent release.

HST-2 Search 

The HST-2 has a Java API to create searches with. When an HST query is executed, the HstQuery object is translated into a JCR XPath query which queries the Hippo Repository. The Hippo Repository returns JCR Nodes as hits, that in turn are mapped to HippoBeans in the HstQueryResult object.

Below there are a Java HstComponent snippet example, a JSP snippet and a schematic (not exact!) drawing of all involved components combining some HST query classes and the Hippo Repository interaction. 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 {

        SearchInfo info = getParametersInfo(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 = getQueryManager(request).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 * (crPage - 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);
        }
    }

}

JSP code snippet to iterate through HippoBeans on the result

JSP Snippet example using HstQueryResult:

<%@ include file="/WEB-INF/jspf/htmlTags.jspf" %>
<%--@elvariable id="crPage" type="java.lang.Integer"--%>
<%--@elvariable id="info" type="org.example.componentsinfo.GeneralListInfo"--%>
<%--@elvariable id="query" type="java.lang.String"--%>
<%--@elvariable id="result" type="org.hippoecm.hst.content.beans.query.HstQueryResult"--%>

<h2>
  <c:out value="${info.title}"/> for '<c:out value="${query}"/>' : ${result.totalSize} results
</h2>
<ul>
  <%-- Iterate through the hippoBeans on the result --%>
  <c:forEach var="item" items="${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>

Global overview of involved components

 

http://www.onehippo.org/binaries/ninecolumn/content/gallery/connect/library/search-new.png

Detailed description of parts involved

CMS 7 HST-2 Search pages