The generated API clients are a work in progress, you can also find our stable clients on the Algolia documentation.

Skip to main content

Filtering your search

Filtering is primarily used in the context of front-end search. We call this faceting, where filters are displayed on the search UI as clickable items, allowing users to select one or more filters. This enables a more refined, drilled-down search experience.

How to Filter Your Data

1. Define attributes that need to be filterable (at indexing time)

Initially, filter attributes must be defined as facets, using the attributesForFaceting parameter. This can be done using the setSettings method.

import { searchClient } from '@algolia/client-search';

const client = searchClient('YOUR_APP_ID', 'YOUR_API_KEY');

const response = await client.setSettings({
indexName: '<YOUR_INDEX_NAME>',
indexSettings: {
attributesForFaceting: [
'actor',
'filterOnly(category)',
'searchable(publisher)',
],
},
});

// use typed response
console.log(response);

await client.waitForTask({ indexName: '<YOUR_INDEX_NAME>', taskID: response.taskID });

2. Filter by Attributes (at query time)

The actual filtering of records is performed at query time, not at indexing time. For this, you need to use the filters parameter in your search code.

Filtering by string using the filters field

// Only "Scarlett Johansson" actor
const response = await client.search({
requests: [
{
indexName: '<YOUR_INDEX_NAME>',
query: '<YOUR_QUERY>',
filters: 'actor:Scarlett Johansson',
},
],
});

// use typed response
console.log(response);

// Only "Tom Cruise" or "Scarlett Johansson" actor
const response = await client.search({
requests: [
{
indexName: '<YOUR_INDEX_NAME>',
query: '<YOUR_QUERY>',
filters: 'actor:Tom Cruise OR actor:Scarlett Johansson',
},
],
});

// use typed response
console.log(response);

// Everything but "Nicolas Cage" actor
const response = await client.search({
requests: [
{
indexName: '<YOUR_INDEX_NAME>',
query: '<YOUR_QUERY>',
filters: 'NOT actor:Nicolas Cage',
},
],
});

// use typed response
console.log(response);