Guides / Managing results / Refine results / Filtering

Filters and boolean operators

Algolia supports the following operators, which must be in capital letters:

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

Use of parentheses

Use parentheses ( and ) to combine several OR conditions. These rules apply:

  • Don’t group ANDs within ORs. For example, A OR (B AND C) OR (D AND E) isn’t supported.

  • Don’t use parentheses with top-level AND combinations. For example, (A AND (B OR C)) isn’t supported. (But A AND (B OR C) is supported)

  • Don’t use parentheses to negate a group of expressions. For example, NOT (filter1 OR filter2) isn’t supported.

Use the filter syntax validator to help you verify and build filter statements.

Examples of valid combinations

The following examples are valid combinations of filter expressions:

  • A AND (B OR C)
  • (A OR B) AND C
  • A AND (B OR C) AND (D OR E)
  • A AND B AND (C OR D) AND (E OR F)
  • (A OR B) AND (C OR D) AND E

Example scenarios for filter combinations

The filters parameter gives precise control over search results with a SQL-like syntax. Use the following examples to learn how to apply different filters and combine them with boolean logic.

Basic filters

Filter type Example Filter
Numeric comparison Show products that cost $13 or more price > 12.99
Numeric range Show products with a price between $5.99 and $100 price:5.99 TO 100
Facets Show records categorized as “Book” category:Book
Tag filters Show records marked with the “published” tag _tags:published
Boolean filters Show records where the isEnabled attribute is true isEnabled:true
Filter by date Show records published between two dates Unix timestamps date: 1441745506 TO 1441755506
Negation Exclude discontinued products NOT is_discontinued:true

Combine filters

  • Find books not written by a specific author: (category:Book OR category:Ebook) AND NOT author:"JK Rowling"
  • Filter products by price range and category: price:10 TO 50 AND (category:Electronics OR category:Accessories)
  • Complex filter with several conditions: inStock:true AND (category:Phones OR category:Tablets) AND (brand:Apple OR brand:Samsung) AND price>500

Don’t mix 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.

Apps and user actions change filter expressions

If a user or app selects a filter, Algolia omits the filters that don’t apply from the expression.

For example, if the original filter expression is ((category:Electronics OR category:Home) AND NOT brand:LG) AND NOT price:100 TO 500 and the user selects the Electronics category, the expression simplifies to (category:Electronics AND NOT brand:LG) AND NOT price:100 TO 500.

Filters syntax validator

Type your filter to validate it:


      

Did you find this page helpful?