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.

  1. Go to your dashboard and select your index.
  2. Click the Configuration tab.
  3. In the Facets subsection of Filtering and Faceting, click the “Add an attribute” button and select the brand attribute from the dropdown.
  4. 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.

  1. Go to your dashboard and select your index. This should take you automatically to the Browse section.
  2. Click the “Add Query Parameter” button, which is just below the search bar.
  3. Go to the Custom tab.
  4. Add your filter as JSON: { "filters": "brand:Motorola" } or { "filters": "brand:LG OR brand:Samsung" } or { "filters": "NOT brand:Apple" }.
  5. Click “Apply”.

Note that you can alternatively use facet filters. Click the “Add Query Parameter” button, go to the Filters tab and enter your targeted facet filter(s).

Did you find this page helpful?