Faceted Navigation with Free Text Search and HstQueries
This page describes how to use faceted navigation in combination with free text search and HstQueries in Bloomreach Content.
Before proceeding, ensure you have configured a faceted navigation node as described in Faceted Navigation Configuration. With HST2, you can access a faceted navigation bean directly. The matched sitemap item should contain a relativecontentpath that points to your faceted navigation node.
Faceted Navigation Without Free Text Search or HstQueries
To access the faceted navigation bean in an HstComponent, use the following approach:
HippoBean currentBean = this.getContentBean(request);
The currentBean may represent either a result set or a document within the result set. To ensure you are working with a faceted navigation bean, use the following logic:
if(currentBean instanceof HippoDocumentBean && currentBean.getParentBean() instanceof HippoResultSetBean) { // Retrieve the faceted navigation bean currentBean = currentBean.getParentBean().getParentBean(); }else if(currentBean instanceof HippoResultSetBean) { // Retrieve the faceted navigation bean currentBean = currentBean.getParentBean(); } if(currentBean instanceof HippoFacetNavigation || currentBean instanceof HippoFacetChildNavigationBean) { request.setAttribute("facetnav", currentBean); if(currentBean instanceof HippoFacetChildNavigationBean) { request.setAttribute("childNav", "true"); } }
In your JSP, you can render facet categories and counts as follows:
colors (3466)
grey (235)
red (346)
yellow (1245)
etc
For additional examples, refer to the testsuite code.
To display the result set in a component, use the following pattern:
HippoBean currentBean = this.getContentBean(request); if(currentBean instanceof HippoFacetChildNavigationBean) { HippoFacetChildNavigationBean facetNav = (HippoFacetChildNavigationBean) currentBean; List<ProductBean> resultset = new ArrayList<ProductBean>(); HippoDocumentIterator<ProductBean> it = facetNav.getResultSet() .getDocumentIterator(ProductBean.class); int skip = 0; it.skip(skip); while(it.hasNext() && it.getPosition() < 10 + (skip - 1)) { // getPosition increases on it.next() call; skip - 1 is intentional resultset.add(it.next()); } request.setAttribute("resultset", resultset); }
Combining Free Text Search or HstQueries with Faceted Navigation
Important:
When using free text search or HstQueries with faceted navigation, ensure that every component referencing the faceted navigation injects the exact same free text search or HstQuery instance.
To simplify this integration, use the utility methods in org.hippoecm.hst.util.ContentBeanUtils. For example, in your result set component:
HstQuery query = getHstQuery(request); HippoFacetNavigationBean facetNav = ContentBeanUtils.getFacetNavigationBean(query); List<ProductBean> resultset = new ArrayList<ProductBean>(); request.setAttribute("resultset", resultset); request.setAttribute("facetNavigation", facetNav); HippoResultSetBean resultSetBean = ((HippoFacetNavigationBean)facetNav) .getResultSet(); if(resultSetBean == null) { return; } HippoDocumentIterator<ProductBean> it = resultSetBean.getDocumentIterator(ProductBean.class); int skip = 0; it.skip(skip); while(it.hasNext() && it.getPosition() < 10 + (skip - 1)) { // getPosition increases on it.next() call; skip - 1 is intentional resultset.add(it.next()); }
To display a document in context (see Context Aware, Canonical, Preferred and navigationStateful URLs), use:
HstQuery query = getHstQuery(request); ProductBean product = ContentBeanUtils .getFacetedNavigationResultDocument( query, ProductBean.class); request.setAttribute("document", product);
Note:
The query object (String or HstQuery) passed to getFacetedNavigationResultDocument must be the same as the one used to create the faceted navigation tree. Fetching a document without the correct query may return a result outside the intended result set (for example, if the result set is limited to 100 documents).
The ContentBeanUtils class provides the following utility methods. You can pass a query as a String (for free text search, equivalent to jcr:contains(.,'query') in XPath) or as an HstQuery.
getFacetNavigationBean(HstQuery q); getFacetNavigationBean(HstQuery query, String relPath); getFacetNavigationBean(String query); getFacetNavigationBean(String query, String relPath); getFacetedNavigationResultDocument(HstQuery query, Class<T> bean); getFacetedNavigationResultDocument(HstQuery query, String relPath , Class<T> bean); getFacetedNavigationResultDocument(String query , Class<T> bean); getFacetedNavigationResultDocument(String query, String relPath, Class<T> bean);
To generate links for documents in a result set, use the navigationStateful attribute. For details, see Context Aware, Canonical, Preferred and navigationStateful URLs.
JSP Example
<c:forEach var="result" items="${requestScope.resultset}"> <hst:link var="link" hippobean="${result}" navigationStateful="true" /> <a href="${link}">${result.title}</a> </c:forEach>
Freemarker Example
<#list resultset as result> <@hst.link var="link" hippobean=result navigationStateful="true" /> <a href="${link}">${result.title}</a> </#list>