Filter By Boolean
On this page
Algolia allows you to filter results by boolean attributes. Imagine you have an e-commerce website, and you want users to search through available products. If you have a boolean attribute called is_available
, you can exclude all records where is_available
is false
. To do this, you must first declare is_available
as an attribute for faceting.
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.
1
2
3
$results = $index->search('query', [
'filters' => 'is_available:true'
]);
The engine considers booleans as integers: false
as 0
and true
as 1
. That means you can apply numeric filters for boolean attributes. With the preceding example dataset, you can apply the filter is_available=0
for the same effect as is_available:false
, and is_available=1
for is_available:true
. If using numeric filters, you don’t need to declare the attribute in attributesForFaceting
.