3 Nesting HstQuery Filters
This page explains how to combine multiple Filter constraints in an HstQuery using nested filters. By default, when you add multiple constraints to a filter, they are combined using AND logic. See HstQuery Filter for details.
You can also combine constraints using OR logic by nesting filters. You can add subfilters to a filter and specify whether they should be combined with AND or OR logic.
Combining Filters with OR
To combine filters with OR logic, use the addOrFilter() method. The following example demonstrates how to create a filter that matches documents containing a specific query and where either the hippostdpubwf:lastModifiedBy or hippostdpubwf:createdBy property is set to 'admin':
// the main filter Filter filter = hstQuery.createFilter(); // add a constraint to the main filter filter.addContains(".", query); // create subFilter 1 with a constraint Filter subFilter1 = hstQuery.createFilter(); subFilter1.addEqualTo("hippostdpubwf:lastModifiedBy", "admin"); // create subFilter 2 with a constraint Filter subFilter2 = hstQuery.createFilter(); subFilter2.addEqualTo("hippostdpubwf:createdBy", "admin"); // add subFilter1 and subFilter2 as OR-ed filters filter.addOrFilter(subFilter1); filter.addOrFilter(subFilter2); // alternative way to add subFilters in one statement // filter.addOrFilter(subFilter1).addOrFilter(subFilter2); // set the main filter on the query hstQuery.setFilter(filter);
This code returns documents that:
- Contain the specified
queryAND - Have either
hippostdpubwf:lastModifiedBy = 'admin'ORhippostdpubwf:createdBy = 'admin'.
Combining Filters with AND
To combine filters with AND logic, use the addAndFilter() method. For example:
// add subFilter1 and subFilter2 as AND-ed filters filter.addAndFilter(subFilter1); filter.addAndFilter(subFilter2);
This code returns only documents where both hippostdpubwf:lastModifiedBy = 'admin' AND hippostdpubwf:createdBy = 'admin'.
Info:
You can add subfilters to a main filter even if the main filter does not have direct constraints. Subfilters can also contain their own nested subfilters, forming a composite structure.
Rules for Nesting Subfilters
When adding subfilters to a filter, you must choose a single logical operator for all subfilters: either all OR-ed or all AND-ed. Mixing AND and OR subfilters within the same filter is not supported. This restriction exists because the underlying implementation does not support operator precedence or grouping with brackets. Internally, filters are translated to Lucene BooleanQuery objects, which use MUST, MUST_NOT, and SHOULD, but not explicit AND/OR precedence.
Warning:
Do not mix AND-ed and OR-ed subfilters in a single filter.
Add only OR-ed or only AND-ed subfilters to a given filter.
The following example is not valid because it mixes AND and OR subfilters in the same filter:
// Wrong: Mixing AND-ed and OR-ed subfilters is not allowed filter.addAndFilter(subFilter1); filter.addOrFilter(subFilter2);
Combining Nested Filters
You can nest filters to build more complex queries. For example, you can AND two subfilters at the top level, while one of those subfilters contains OR-ed subfilters:
// add an AND subFilter1 to the mainFilter mainFilter.addAndFilter(subFilter1); // add two OR filters to subFilter2 subFilter2.addOrFilter(subsub1); subFilter2.addOrFilter(subsub2); // add an AND subFilter2 to the mainFilter mainFilter.addAndFilter(subFilter2);
This structure creates a query where both subFilter1 AND subFilter2 must match. For subFilter2, either subsub1 OR subsub2 must match.