How To Include Assets and Images in Search Results - BloomReach Experience - Open Source CMS

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

26-11-2015

How To 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:

try {
    final HstRequestContext context = request.getRequestContext(); 
    final HippoBean scope = context.getSiteContentBaseBean();
    final HstQuery query = context.getQueryManager().createQuery(scope);
    final Filter filter = query.createFilter();
    filter.addContains(".", "hippo");
    query.setFilter(filter);
    query.setLimit(10);
    final HstQueryResult result = query.execute();
    request.setAttribute("result", result);
} catch (QueryException e) {
}

The query above searches for any type of document below scope, which contains the word ' hippo'. The scope 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 or Gallery as follows:

 try {
    final HstRequestContext context = request.getRequestContext();
    final HippoBean scope = context.getSiteContentBaseBean();
    final HstQuery query = context.getQueryManager().createQuery(scope);

    final List<HippoBean> extraScopes = new ArrayList<HippoBean>();
    extraScopes.add(getAssetBaseBean(request));
    extraScopes.add(getGalleryBaseBean(request));
    query.addScopes(extraScopes);

    final Filter filter = query.createFilter();
    filter.addContains(".", "hippo");
    query.setFilter(filter);
    query.setLimit(10);
    final HstQueryResult result = query.execute();
    request.setAttribute("result", result);
} catch (QueryException e) {
} 
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?