Price & range filtering
Filter by numeric ranges such as price using the range operator and individual bound operators.
Price sliders and range pickers produce two optional values - a minimum and a maximum. Kelp handles this with the range operator (when both bounds are set) or individual >= / <= operators (when only one bound is set).
Strategy
| Bounds provided | Operator to use | Clauses emitted |
|---|---|---|
| Both min and max | range | One clause: { op: 'range', values: [min, max] } |
| Min only | >= | One clause: { op: '>=', values: [min] } |
| Max only | <= | One clause: { op: '<=', values: [max] } |
| Neither | - | No clauses (omit from filters) |
This keeps the filter array minimal and avoids sending redundant clauses.
Implementation
import type { KelpFilter } from '@kelpclick/client';
/**
* Build price-range filter clauses from optional min/max bounds.
* Returns an empty array when both bounds are absent or invalid.
*/
function toPriceFilters(
priceField: string,
min?: number,
max?: number,
): KelpFilter[] {
const validMin = Number.isFinite(min) ? min : undefined;
const validMax = Number.isFinite(max) ? max : undefined;
// Inverted range (bad URL param) - ignore silently.
if (validMin !== undefined && validMax !== undefined && validMin > validMax) {
return [];
}
// Both bounds: use a single range clause.
if (validMin !== undefined && validMax !== undefined) {
return [
{
field: priceField,
op: 'range',
values: [String(validMin), String(validMax)],
},
];
}
const out: KelpFilter[] = [];
if (validMin !== undefined) {
out.push({ field: priceField, op: '>=', values: [String(validMin)] });
}
if (validMax !== undefined) {
out.push({ field: priceField, op: '<=', values: [String(validMax)] });
}
return out;
}Values are always passed as strings in KelpFilter.values. Numeric conversion (String(validMin)) happens before building the clause.
Usage
import { createKelpClient } from '@kelpclick/client';
const kelp = createKelpClient({
searchUrl: 'https://{TENANT_ID}-search.kelp.click',
analyticsUrl: 'https://{TENANT_ID}-events.kelp.click',
apiKey: 'kelp_...',
});
// User has set $50 - $150 price range
const priceFilters = toPriceFilters('price', 50, 150);
// [{ field: 'price', op: 'range', values: ['50', '150'] }]
// User has set min $75 only (no max)
const priceFilters2 = toPriceFilters('price', 75, undefined);
// [{ field: 'price', op: '>=', values: ['75'] }]
const res = await kelp.browse('products', {
filters: [
{ field: 'status', op: '=', values: ['ACTIVE'] },
...priceFilters,
],
sort: [{ field: 'price', direction: 'asc' }],
perPage: 24,
});Handling URL parameters
Price bounds often come from URL search params. Parse and validate them before building filter clauses:
// In a loader or component reading URL params
function parsePriceBounds(searchParams: URLSearchParams) {
const min = parseFloat(searchParams.get('priceMin') ?? '');
const max = parseFloat(searchParams.get('priceMax') ?? '');
return {
min: Number.isFinite(min) && min >= 0 ? min : undefined,
max: Number.isFinite(max) && max >= 0 ? max : undefined,
};
}
const { min, max } = parsePriceBounds(new URL(request.url).searchParams);
const priceFilters = toPriceFilters('price', min, max);Rating and other numeric ranges
The same pattern works for any numeric field, not just price:
// Rating 4.0 to 5.0
const ratingFilter: KelpFilter = {
field: 'rating',
op: 'range',
values: ['4.0', '5.0'],
};
// In-stock only (inventory total > 0)
const inStockFilter: KelpFilter = {
field: 'variantInventoryTotal',
op: '>',
values: ['0'],
};See Combining filters for how to compose price, in-stock, and facet clauses into a single request.