Search & browse

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 perPage

Calculating total pages

const totalPages = Math.ceil(res.found / perPage);

Pagination params

ParamTypeDefaultNotes
pagenumber11-based page number.
perPagenumber20Results per page.

Response fields

FieldTypeNotes
foundnumberTotal matching documents across all pages. Use this to calculate totalPages.
pagenumberThe page returned (mirrors what was requested).
hitsKelpHit[]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

FieldNotes
titleAlphabetical product title sort
vendorAlphabetical vendor sort
minPriceSort by lowest variant price
maxPriceSort by highest variant price
variantsInStockPercentageSort by in-stock fraction (0.0 - 1.0)
bestSellerAll-time units sold - requires order sync
trendingUnits sold in trailing 60 days - requires order sync
createdAtNewest/oldest first
publishedAtMost 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

On this page