3 Nesting HstQuery Filters
So far, we have seen how to bootstrap aHstQuery, and how to apply aFilter to it. AtHstQuery Filter, it is also mentioned that, when adding constraints to a filter, all constraints areAND-ed.
Next to that, it is also possible to haveOR-ed constraints. This can be done by OR-ing entire (sub)filters. A filter can be OR-ed or AND-ed with another filter. For example:
OR-ing two filters:
// the main filterFilter filter = hstQuery.createFilter();// some constraint on the main filterfilter.addContains(".", query);// create subFilter 1 with some constraintFilter subFilter1 = hstQuery.createFilter();subFilter1.addEqualTo("hippostdpubwf:lastModifiedBy", "admin");// create subFilter 2 with some constraintFilter subFilter2 = hstQuery.createFilter();subFilter2.addEqualTo("hippostdpubwf:createdBy", "admin");// add subFilter1 and subFilter 2 as OR-ed filtersfilter.addOrFilter(subFilter1);filter.addOrFilter(subFilter2);// alternative way to add subFilter in one statement// filter.addOrFilter(subFilter1).addOrFilter(subFilter2);// set the main filter on the requesthstQuery.setFilter(filter);The code snippet above creates a query that returns only documents that:
contain 'query' AND
have ahippostdpubwf:lastModifiedBy = 'admin'ORhippostdpubwf:createdBy = 'admin' property.
To change above code snippet to apply AND logic, usefilter.addAndFilter() instead:
// add subFilter1 and subFilter 2 as AND-ed filtersfilter.addAndFilter(subFilter1);filter.addAndFilter(subFilter2);
With this code, only documents that havehippostdpubwf:lastModifiedBy = 'admin'ANDhippostdpubwf:createdBy = 'admin' will be returned.
There is an important thing to keep in mind when adding subfilters to a filter: To a single filter, you must only addOR-ed or only addAND-ed subfilters. The rationale behind this, is that we do not use brackets or precedence for child filters. Also, a more technical reason, Filter in the end in the repository are translated to Lucene BooleanQuery's, which do not have the notion of AND or OR, but only of MUST, MUST_NOT and SHOULD. For you, it is enough to remember:
To a single filter only add OR-ed or only add AND-ed subfilters
Therefore, the following code snippet isnot correct, because AND-ing and OR-ing subfilters must not be combined in one filter:
// Wrong: Not allowed AND-ing and OR-ingfilter.addAndFilter(subFilter1);filter.addOrFilter(subFilter2);
It is, however,correct if a filter has its nested subfilters OR-ed, while it is itself AND-ed with another filter, like in the example below:
// add an AND subFilter1 to the mainFiltermainFilter.addAndFilter(subFilter1);// add two OR filters to subFilter2subFilter2.addOrFilter(subsub1);subFilter2.addOrFilter(subsub2);// add an AND subFilter2 to the mainFiltermainFilter.addAndFilter(subFilter2);
The above snippet creates a query wheresubFilter1ANDsubFilter2 constraints must be met. ForsubFilter2,subsub1ORsubsub2 must be met.