Using Algolia Faceting to Build Instant Refined Search Filters
Learn how to declare faceted attributes, apply facetFilters, and understand the impact on record size and query latency when adding brand‑ and price‑based filters to an Algolia index.
04 Dec 2025, 12:56 UTC

Problem: Needing instant filter UI without extra backend calls
When building a product catalog, users expect to narrow results by brand, category, or price range as they type. Implementing these filters usually requires additional queries to your own services to compute counts, which adds latency and complexity. Algolia’s faceting feature moves that work into the search engine itself, returning facet counts alongside matching records so the UI can update instantly.
Thesis: By declaring attributes as faceted and using facetFilters in your query, you get real‑time filter counts with minimal extra work, provided you keep an eye on record size and cardinality.
1. What faceting does in Algolia
Faceting tells Algolia to treat certain record attributes as dimensions for counting. For each faceted attribute, the search response includes a facetCounts object that shows how many matching records have each distinct value (or, for numeric attributes, how many fall into each bucket). This eliminates the need for a separate aggregation query.
2. Declaring faceted attributes
You can set faceting either through the Algolia dashboard or via the Settings API. The dashboard path is Index → Settings → Faceting. To do it programmatically, you need an Admin API key (which grants write access to index settings).
Example using the official Node.js client (run in a trusted backend or CI environment):
const algoliasearch = require('algoliasearch');
// Replace with your actual values
const client = algoliasearch('YOUR_APP_ID', 'YOUR_ADMIN_API_KEY');
const index = client.initIndex('products');
// Define which attributes should be used for faceting
const settings = {
attributesForFaceting: ['brand', 'price']
};
index.setSettings(settings).then(() => {
console.log('Faceting settings updated');
}).catch(err => {
console.error('Failed to update settings:', err);
});
Where to run: any environment that can securely store your Admin API key (e.g., a server‑side script, a CI pipeline, or an admin tool). Required permissions: the key must have the addObject and editSettings ACLs. Risks: if you accidentally overwrite other settings, you may lose custom ranking or typo tolerance; always retrieve the current settings first if you are unsure.
3. Using facetFilters in a search query
Once the attributes are declared, you can refine searches by passing a facetFilters array. Each entry is a string in the format "attribute:value"; for numeric ranges you use the Algolia numeric syntax "attribute:min TO max".
Example query (again with the Node.js client, using a search‑only key):
const searchIndex = client.initIndex('products');
searchIndex.search({
query: 'mobile phone',
facetFilters: ['brand:Apple', 'price:0 TO 500']
}).then(({ hits, facetCounts }) => {
console.log('Hits:', hits.length);
console.log('Facet counts:', facetCounts);
}).catch(err => {
console.error('Search error:', err);
});
What to expect in the response:
hits– the product records that match "mobile phone" and are Apple brand with a price between 0 and 500.facetCounts– an object containing two keys,brandandprice. Underbrandyou’ll see a count for each brand present in the filtered set (e.g., {"Apple": 23, "Samsung": 5}); underpriceyou’ll see bucket counts based on the numeric faceting granularity Algolia computes automatically (you can customize bucket size withnumericAttributesForFacetingif needed).
You can verify the configuration by opening the Algolia dashboard, navigating to the index, and checking the Faceting tab – the attributes you set should appear there.
4. Trade‑offs and limitations
While faceting is powerful, it adds overhead:
- Record size increase – each faceted attribute is stored in the record for fast lookup. Algolia enforces a 10 KB record limit. Adding many faceted attributes, especially those with high cardinality (thousands of distinct values), can push records over this limit and cause indexing errors.
- Indexing time – more faceted attributes mean more work during indexing, which can slow down batch updates.
- Approximate counts – when you use
distinctor custom ranking, facet counts may be approximate. Enabling exhaustive faceting (exhaustiveFacets:true) guarantees exact counts but can increase query latency, particularly on large indexes.
Practical way to check the impact:
- After uploading a representative sample of records, view the index statistics in the dashboard (Index → Usage → Records). Note the average record size.
- Run a typical search with facetFilters and inspect the response time (the dashboard shows latency per query).
- If you suspect size issues, temporarily remove a faceted attribute, reindex, and compare the record size and indexing duration.
Actionable closing
Start by identifying the few attributes that truly drive your filter UI (e.g., brand, category, price). Declare them as faceted via the API or dashboard, keep an eye on record size, and test with realistic data volumes. Use facetFilters in your search calls to obtain instant filter counts without extra backend work. If you need guaranteed exact counts, enable exhaustive faceting and measure the latency impact; otherwise, rely on Algolia’s approximate counts for a snappy experience.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.