Include Assets and Images in Search Results

The typical code for querying all documents in the current site, containing the word hippo, looks like this:

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

The query above searches for any type of document below contentBaseBean, which contains the word ' hippo'. The contentBaseBean is fetched with getSiteContentBaseBean(), which returns the root content bean of the current site. However, this search scope does not include images and assets. You can extend the query above to also search in Assets and Image Gallery as follows:

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 that search hits in Assets or Gallery will return HippoAssetBean and HippoGalleryImageSetBean instances as result.

 

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?