Include Assets and Images in Search Results
By default, a query for documents in the current site that contain the word hippo uses the following code:
final HstRequestContext context = request.getRequestContext(); final HippoBean contentBaseBean = context.getSiteContentBaseBean(); final HstQuery query = HstQueryBuilder.create(contentBaseBean) .ofTypes(BaseDocument.class) .limit(10) .where(constraint(".").contains("hippo")) .build(); final HstQueryResult result = query.execute(); request.setAttribute("result", result);
This query searches for all document types under the contentBaseBean node that contain the term hippo. The getSiteContentBaseBean() method retrieves the root content bean for the current site. However, this approach does not include images or assets in the search scope.
To include assets and images in your search results, extend the query as shown below:
final HstRequestContext context = request.getRequestContext(); final HippoBean contentBaseBean = context.getSiteContentBaseBean(); final HippoBean assetsBaseBean = getAssetBaseBean(request); final HippoBean galleryBaseBean = getGalleryBaseBean(request); final HstQuery query = HstQueryBuilder.create(contentBaseBean, assetsBaseBean, galleryBaseBean) .ofTypes(HippoBean.class) .limit(10) .where(constraint(".").contains("hippo")) .build(); final HstQueryResult result = query.execute(); request.setAttribute("result", result);
Note: When the search matches items in the Assets or Gallery sections, the results will include instances of
HippoAssetBeanandHippoGalleryImageSetBean.