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

2 HstQuery Filter

After you have bootstrapped the HstQuery, see HstQuery bootstrapping, you can apply filters. Not every query requires the use of filters. For example, when querying for the latest 10 news documents inside folder X, not filter is needed:

// the scope, aka the folder, to search news items below
HippoBean scope = request.getRequestContext().getSiteContentBaseBean().getBean(relativePathToFolderX);
try {
    HstQuery hstQuery =
        request.getRequestContext().getQueryManager().createQuery(scope, NewsDocument.class, true);
    hstQuery.setLimit(10);
    hstQuery.addOrderByDescending("example:date");
    HstQueryResult result = hstQuery.execute();
} catch (QueryException e) {
    throw new HstComponentException(
          "Exception occured during creation or execution of HstQuery.", e);
}

However, there are cases where you need to apply a filter. For example, when querying for the latest 10 news documents that contain some word in the entire document, or some word in the value of a property  such as " example:title", you need to apply a filter to the hstQuery. A filter specifies constraints like "Give me only documents where the lastModifiedBy user equals X" or "Give me only documents that contain word W".

First of all, creating a Filter can be done from an HstQuery:

String query = ....
// create a new Filter. The HstQuery behaves like a Filter factory.
Filter filter = hstQuery.createFilter();
// apply the filter to the hstQuery
hstQuery.setFilter(filter);
// add to the filter a 'query' constraint. "." means to search in the
// entire document
filter.addContains(".", query);

Now, hstQuery.execute() will only return documents that match the free text search query.

Note that supplying "." for the first argument of HstQuery#addContains() and HstQuery#addNotContains() means that the entire document is to be searched, including all child nodes and all properties.

By default, the repository index takes  all text of a document into account - all of its properties and all the properties of all descendant nodes. Like that, searching on "." means searching in the entire document.

You can also search in just a single property, for example

Search for documents the title of which matches the query string:

// only return hits that match the query in the title
filter.addContains("example:title", query);

or in multiple properties, for example

Search for documents that match query in title AND in summary:

// only return hits that match the query in the title AND in the summary
filter.addContains("example:title", query);
filter.addContains("example:summary", query);

Whe you specify multiple constraints for a single Filter, they get AND-ed. The code snippet above therefore only returns hits for documents that match the query in the property " example:title" AND the property " example:summary". See Nesting HstQuery Filters on how to OR constraints.

Multiple constraints to a single Filter are AND-ed.

The previous examples all searched in a property of a document. You can also search in a property of a descendant node (i.e. compound type field) of a document:

filter.addContains("example:address/example:street", query);

As an alternative, you can also specify the first argument as example:address/ @example:street, adding "@" to mark example:street as a property. Both versions are equivalent.

Summarizing, we added a free text search (using  addContains()) on:

  1. a document

  2. a property of a document

  3. a property of a child/descendant node of the document

Next to addContains, more constraints can be applied to a Filter. The opposite of addContains() is addNotContains(). These are the only two constraints that involve free text search. The other filter constraints pertain to equality or comparison to a given value.

For more details, see the javadoc of the Filter interface.

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?