Filters and Boolean Operators
On this page
The following operators, which must be specified in CAPS, are supported:
- OR: must match any of the combined conditions (disjunction)
- AND: must match all of the combined conditions (conjunction)
- NOT: negates a filter
Finally, parentheses, (
and )
, can be used for grouping.
Putting all that together, if you would like your retrieved products to be either books or ebooks, but not by the author JK Rowling, your query would look like this:
1
2
3
index.search({
filters: '(category:Book OR category:Ebook) AND NOT author:"JK Rowling"'
});
Caveats
Negating Combined Filters
You cannot negate a group of filters, only an individual filter. For example, NOT(filter1 OR filter2)
is not allowed.
Combining ANDs and ORs
While you may use as many ANDs and ORs as you need, you’ll need to be careful about how you combine them.
For performance reasons, we do not allow you to put groups of ANDs within ORs. Here are some considerations to take into account:
- We allow ( x AND (y OR z) AND (a OR b) )
- We allow ( x AND y OR z AND a OR b )
- We don’t allow
( x OR ( y AND z) OR ( a AND b) ) - We don’t allow
(a AND b) OR (c AND d)
Keep in mind that your particular filter might not look exactly like the format we don’t allow, and yet it still might fail. This is because it might be reducible to (or be transformed into) the unsupported form.
For example:
- (((a OR b) AND NOT c) AND NOT d) becomes (a AND NOT c AND NOT d) OR (b AND NOT c AND NOT d)
Check out our filter tool to help you build complex filter statements.
Missing parentheses
If you omit parentheses, the engine will automatically put the ORs inside parenthesis:
- ( x AND y OR z AND a OR b ) becomes ( x AND (y OR z) AND (a OR b) )
- ( x OR y AND z OR a AND b ) becomes ( (x OR y) AND (z OR a) AND b )
Given the potential for confusion, we encourage you to use parenthesis.
Mixing type of filters in OR conditions
As noted above, there are 3 categories of filters (string, numeric, tags).
You cannot mix these different filter categories inside an OR.
For example, num=3 OR tag1 OR facet:value
is not allowed.