This article covers a Bloomreach Experience Manager version 12. There's an updated version available that covers our most recent release.

Delivery Tier Legacy Search API

As of Bloomreach Experience Manager 11.1, we recommend using the Fluent Search API which is more intuitive to use and results in better code readability. However, future releases of Bloomreach Experience Manager will continue to support the Legacy Search API.

Introduction

Goal

Create and execute search queries using the Legacy Search API in Hippo's delivery tier.

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 is an example of a Java HstComponent and a schematic (not exact!) drawing of how all involved components interact.

More detailed documentation about using the Legacy Search API can be found on the following pages:

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 = context.getSiteContentBaseBean();
        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);
        }
    }

}
See Render a Search Query Result for example JSP and Freemarker templates for the above example.

Overview of Involved Components

 

Did you find this page helpful?
How could this documentation serve you better?
On this page
    Did you find this page helpful?
    How could this documentation serve you better?