🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
Guides / Managing results / Refine results / Filtering

Filters and boolean operators

The following operators, which must be in capital letters, are supported:

  • OR: must match any of the combined conditions (disjunction)
  • AND: must match all the combined conditions (conjunction)
  • NOT: negates a filter

You can also use parentheses ( and ) for grouping.

For example, if you would like your retrieved products to be either books or ebooks, not written by JK Rowling, your query would look like this:

1
2
3
index.search('', {
  filters: '(category:Book OR category:Ebook) AND NOT author:"JK Rowling"'
});

Negate combined filters

You can’t negate a group of filters, only an individual filter. For example, NOT(filter1 OR filter2) isn’t allowed.

Combine ANDs and ORs

While you may use as many ANDs and ORs as you need, be careful about how you combine them.

For performance reasons, you can’t put groups of ANDs within ORs. For example:

Combination Allowed?
x AND (y OR z) AND (a OR b) Yes
x AND y OR z AND a OR b Yes
x OR ( y AND z) OR ( a AND b) No
(a AND b) OR (c AND d) No

Your filter might fail if it reduces to an unsupported form. For example:

Original Reduced (disallowed) form
((a OR b) AND NOT c) AND NOT d (a AND NOT c AND NOT d) OR (b AND NOT c AND NOT d)

Check out the filter syntax validator to help you build complex filter statements.

Missing parentheses

To avoid confusion, use parentheses around logical statements.

If you omit them, the engine will automatically put the ORs inside parenthesis:

Original Engine’s interpretation
x AND y OR z AND a OR b x AND (y OR z) AND (a OR b)
x OR y AND z OR a AND b (x OR y) AND (z OR a) AND b

Mixed filter types in OR conditions

You can’t compare different filter types (string, numeric, tags) with an OR. For example, num=3 OR tag1 OR facet:value isn’t allowed.

Did you find this page helpful?