Filtering & facets overview
Narrow results with typed filter clauses and request facet counts alongside every search or browse call.
Filters and facets work together: filters restrict which documents Kelp returns, and facets count how many documents match each field value so you can render refinement UI (checkboxes, sliders, swatches).
Both are first-class on every search and browse request.
The KelpFilter shape
A filter clause is a plain object with three fields:
interface KelpFilter {
field: string; // index field name (must be a configured facet attribute)
op?: FilterOp; // operator - defaults to '=' when omitted
values: string[]; // one or more string-encoded values
}FilterOp is one of: '=', '!=', '>', '>=', '<', '<=', 'range'. See Operators for the full table.
Only fields configured as facet attributes on your collection can be filtered. Filtering an unconfigured field returns a 400. The same set of fields is also available for facet counts.
Shopify options fields
Product options (Color, Size, etc.) are facetable using dot notation. Reference a specific option by name to project its values as a dedicated field:
// Facet and filter on a single option
await kelp.search('products', {
facets: ['options.Color', 'options.Size'],
filters: [{ field: 'options.Color', op: '=', values: ['Red', 'Blue'] }],
q: 'shirt',
});
// Filter on the whole options array (prefixed form "Color:Red")
await kelp.search('products', {
filters: [{ field: 'options', op: '=', values: ['Color:Red'] }],
q: 'shirt',
});options.Color projects only the color values (["Red","Blue"]), while the root options array contains all option values in key:value form. The dot notation works the same way for keyedTags and metafields. See Catalog fields reference for the full list of available fields and how to configure them.
Attaching filters to a request
Pass a filters array to search or browse:
import { createKelpClient } from '@kelpclick/client';
const kelp = createKelpClient({
searchUrl: 'https://{TENANT_ID}-search.kelp.click',
analyticsUrl: 'https://{TENANT_ID}-events.kelp.click',
apiKey: 'kelp_...',
});
// search with filters
const res = await kelp.search('products', {
q: 'running shoe',
filters: [
{ field: 'vendor', op: '=', values: ['globex', 'initech'] }, // vendor in (globex, initech)
{ field: 'status', op: '!=', values: ['discontinued'] },
],
});
// browse (no q) with filters - for collection/category pages
const browse = await kelp.browse('products', {
filters: [
{ field: 'status', op: '=', values: ['ACTIVE'] },
],
sort: [{ field: 'variantsInStockPercentage', direction: 'desc' }],
perPage: 24,
});The SDK sends filters as { "filters": [ ... ] } in the request body.
Requesting facet counts
Add a facets array to receive KelpFacetCount[] alongside your hits. Omit it to receive counts for every configured facet attribute on the collection.
const res = await kelp.search('products', {
q: 'shirt',
facets: ['vendor', 'productCategory', 'collections'],
perPage: 20,
});Reading res.facetCounts
Each entry in facetCounts maps a field to its value-level counts:
interface KelpFacetCount {
field: string;
counts: { value: string; count: number }[];
}Example response slice:
res.facetCounts;
// [
// {
// field: 'vendor',
// counts: [
// { value: 'globex', count: 142 },
// { value: 'initech', count: 87 },
// ],
// },
// {
// field: 'productCategory',
// counts: [
// { value: 'footwear', count: 98 },
// { value: 'accessories', count: 56 },
// ],
// },
// ]Use these counts to render a facet list and show how many results each selection would return - counts reflect the current set of active filters, so they stay accurate as the user refines.
Full example
const res = await kelp.search('products', {
q: 'jacket',
facets: ['vendor', 'productCategory', 'status'],
filters: [
{ field: 'status', op: '=', values: ['ACTIVE'] },
{ field: 'vendor', op: '=', values: ['globex'] },
],
perPage: 24,
});
// hits
for (const { document } of res.hits) {
console.log(document.title);
}
// facet UI
for (const facet of res.facetCounts) {
console.log(facet.field, facet.counts);
}What's next
- Operators - full operator reference with a table and examples
- Price & range filtering - min/max bounds and the
rangeoperator - Combining filters - composing facet, price, collection, and exclusion clauses