Pagination and sorting
1-based pagination with page/perPage/found, and multi-key sorting via SortItem.
Pagination
Kelp uses 1-based pagination. Pass page and perPage on any search or browse request; read found and page from the response to build pagination controls.
const res = await kelp.search('products', {
q: 'shirt',
page: 2,
perPage: 24,
});
console.log(res.found); // total matches across all pages, e.g. 312
console.log(res.page); // current page, e.g. 2
console.log(res.hits.length); // items on this page, up to perPageCalculating total pages
const totalPages = Math.ceil(res.found / perPage);Pagination params
| Param | Type | Default | Notes |
|---|---|---|---|
page | number | 1 | 1-based page number. |
perPage | number | 20 | Results per page. |
Response fields
| Field | Type | Notes |
|---|---|---|
found | number | Total matching documents across all pages. Use this to calculate totalPages. |
page | number | The page returned (mirrors what was requested). |
hits | KelpHit[] | Documents on this page - length is at most perPage. |
The React useSearch and useBrowse hooks manage pagination state for you: page, setPage, totalPages, and perPage/setPerPage are returned directly. See React.
Sorting
Sort results with the sort param - an array of SortItem objects applied in order. Each item specifies a field and direction.
interface SortItem {
field: string;
direction: 'asc' | 'desc';
}Single-field sort
await kelp.browse('products', {
sort: [{ field: 'title', direction: 'asc' }],
});Multi-key sort
Sort items are applied in order - the second item breaks ties left by the first:
await kelp.browse('products', {
sort: [
{ field: 'variantsInStockPercentage', direction: 'desc' },
{ field: 'title', direction: 'asc' },
],
});Wire body sent:
{
"sort": [
{ "field": "variantsInStockPercentage", "direction": "desc" },
{ "field": "title", "direction": "asc" }
]
}Sort with filters
Sort and filters compose freely. A common pattern for a collection page is filtering to in-stock items and sorting by stock percentage descending:
await kelp.browse('products', {
filters: [{ field: 'status', op: '=', values: ['ACTIVE'] }],
sort: [
{ field: 'variantsInStockPercentage', direction: 'desc' },
{ field: 'title', direction: 'asc' },
],
perPage: 24,
});Sortable fields are determined by your collection configuration. Sorting on a field that is not referenced in the collection config returns a 400. See Configuring sortable fields below.
Configuring sortable fields
You can sort on any catalog-sortable field that your collection configuration already references - as a searchable attribute, facet attribute, or ranking field. A sort on a field the collection never references returns a 400.
To enable sorting on a field, add it to the relevant section of your collection config. For example, to sort on trending and bestSeller, add them as ranking fields:
{
"ranking": ["trending", "bestSeller"]
}Or add them as facet attributes if you also want to filter on them:
{
"facetAttributes": ["vendor", "productType"],
"ranking": ["trending", "bestSeller"]
}Once referenced, you can sort in either direction:
// Sort by trending popularity (most popular first)
await kelp.browse('products', {
sort: [{ field: 'trending', direction: 'desc' }],
});
// Sort by all-time best sellers, tie-break by title
await kelp.browse('products', {
sort: [
{ field: 'bestSeller', direction: 'desc' },
{ field: 'title', direction: 'asc' },
],
});Common sortable catalog fields
| Field | Notes |
|---|---|
title | Alphabetical product title sort |
vendor | Alphabetical vendor sort |
minPrice | Sort by lowest variant price |
maxPrice | Sort by highest variant price |
variantsInStockPercentage | Sort by in-stock fraction (0.0 - 1.0) |
bestSeller | All-time units sold - requires order sync |
trending | Units sold in trailing 60 days - requires order sync |
createdAt | Newest/oldest first |
publishedAt | Most recently published first |
See Catalog fields reference for the full list of sortable fields.
Combining pagination and sort
const page = 3;
const perPage = 24;
const res = await kelp.search('products', {
q: 'running shoe',
sort: [{ field: 'minPrice', direction: 'asc' }],
page,
perPage,
});
const totalPages = Math.ceil(res.found / perPage);
// res.page === 3
// res.hits.length <= 24