Algolia lets you filter on various types of attributes, including strings. For example, imagine you have an e-commerce website that sells smartphones, and you want to filter search results on a specific brand. With filters, you could allow users to refine results by selecting one or several brands or even exclude a specific brand.

Dataset Example

Let’s say we have an index called products that looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[
  {
    "name": "iPhone XR Black 64GB",
    "description": "iPhone XR features the most advanced LCD in a smartphone—a 6.1-inch Liquid Retina display with industry-leading color accuracy and an innovative backlight design that allows the screen to stretch into the corners.",
    "brand": "Apple",
    "price": 749.99
  },
  {
    "name": "iPhone XS Gold 64GB",
    "description": "The iPhone XS smartphone features a 5.8-inch Super Retina display with custom-built OLED panels for an HDR display that provides the industry’s best color accuracy, true blacks, and remarkable brightness.",
    "brand": "Apple",
    "price": 999.99
  },
  {
    "name": "Galaxy Note9 Ocean Blue 128GB",
    "description": "The new super powerful Note. Galaxy Note9 has the largest amount of storage offered in a Samsung smartphone.",
    "brand": "Samsung",
    "price": 900.00
  },
  {
    "name": "G7 ThinQ™ Platinum Gray 64GB",
    "description": "LG’S greatest smartphpne yet. Advanced AI multimedia phone!",
    "brand": "LG",
    "price": 600.00
  },
  {
    "name": "Moto E5 Play 16GB",
    "description": "Keep your smartphone protected with a water-repellent coating. Make room for more photos, songs, and videos with expandable storage.",
    "brand": "Motorola",
    "price": 150.00
  },
  ...
]

With the above data structure, we can filter on the brand attribute in a variety of ways:

  • We could only show “Motorola” smartphones
  • We could only show “LG” or “Samsung” smartphones
  • We could show everything but “Apple” smartphones

However, before we can do that, we first need to set brand as attributes for faceting.

Configuring attributesForFaceting

Using the API

First, you need to set brand as attributesForFaceting. This happens at indexing time.

Note that if you only need the filtering feature, you can take advantage of the filterOnly modifier, which reduces the index size and improves the speed of the search.

1
2
3
4
5
$index->setSettings([
  'attributesForFaceting' => [
    "brand" // or "filterOnly(brand)" for filtering purposes only
  ]
]);

Using the Dashboard

You can set your attribute for faceting in your Algolia dashboard.

  • Go to your dashboard and select your index.
  • Click the Display tab.
  • In the Faceting section, click the Add an attribute button and select the “brand” attribute in the Attributes for faceting dropdown.
  • Don’t forget to save your changes.

Applying a string filter

Using the API

Now, you can apply your filters. Note that you can only filter results at query time, not at indexing time. For this, you need to use the filters parameter in your search code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Only “Motorola” smartphones
$results = $index->search('smartphone', [
  'filters' => 'brand:Motorola'
]);

// Only “LG” or “Samsung” smartphones
$results2 = $index->search('smartphone', [
  'filters' => 'brand:LG OR brand:Samsung'
]);

// Everything but “Apple” smartphones
$results3 = $index->search('smartphone', [
  'filters' => 'NOT brand:Apple'
]);

Using the Dashboard

Regarding filters, you can’t set them directly in the dashboard since you can only filter at query time. Yet, you can test for specific filters in the dashboard before using them in your search code.

  • Go to your dashboard and select your index.
  • Click the Add Query Param button.
  • Click the Custom tab.
  • Add your filter as JSON: { "filters": "brand:Motorola" } or { "filters": "brand:LG OR brand:Samsung" } or { "filters": "NOT brand:Apple" }.
  • Click Apply.

Note that you can also use facet filters to do the same thing. Click the Add Query Param button, go to the Filters tab and select the right facet filter(s) in the Facet filters dropdown.

Did you find this page helpful?