Guides / Managing results / Refine results / Filtering

Filter By Boolean

Algolia allows you to filter results by boolean attributes. Imagine you have an e-commerce website, and you want user to search only through available products. With filtering, you can create an is_available attribute and exclude all records with is_available = false.

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
[
  {
    "name": "Apple iPhone 5s",
    "is_available": true
  },
  {
    "name": "Apple iPhone 7",
    "is_available": true
  },
  {
    "name": "Apple iPhone X",
    "is_available": false
  },
  ...
]

Applying a boolean filter

If you want users to see only the available products, do the following.

In the engine, booleans are considered as integers so we apply a numeric filter with a value equal to 0 or 1.

1
2
3
$results = $index->search('query', [
  'filters' => 'is_available=1'
]);

Did you find this page helpful?