How to conditionally set hitsPerPage?

Trying to figure out how to have a different hitsPerPage, per-page.
E.g. page 1 would have 9 hits, while the rest would have 10.

Seems like I need to do something with the helper in the searchFunction param, as that gets called with every query/page change, etc.

Help is much appreciated!

I’ve tried doing something like:

    searchFunction: function(helper) {
  // Note: `helper` is a shallow clone of the
  // AlgoliaSearchHelper instance, so methods aren't accessible.
  // console.log(helper);

  if (helper.state.page === 0) {
    helper.state.hitsPerPage = 2;
  } else {
    helper.state.hitsPerPage = 11;
    helper.state.offset = 9;
  }


  helper.search();
},

This limits the first page to 2 and the rest to 11, as I want. However, the second page is then off by 9, as it thinks the first page displayed 11. Setting the offset as I am doing doesn’t seem to have an effect.

Hi @timkelty, offset and length are meant to be used as hitsPerPage and page are meant to be used together. You cannot mix offset with hitsPerPage.

to achieve what you want: “Allow me to programmatically fetch different set of results”:

  • offset
  • length

As described here: https://www.algolia.com/doc/rest-api/search/#offset

You will need to do a specific pagination widget that will use those instead of hitsPerPage/page combo. That way you won’t miss any result.

Let me know how it goes.

But what you could also be doing is always ask for 11 hits then just hide

Thanks for the clarification @vvo.
Makes sense that I would need to roll my own pagination.

In my case I don’t think I can just hide the unwanted results, as then there would be hits that would never be shown (if I’m following you correctly). E.g. if page 1 hid hits 3-11, page 2 would show hits 12-33, and hits 3-11 would never be shown.

Indeed so going with offset/length should be the way to do it.