Instantsearch: How to combine the hits of 2 queries and maintain proper pagination/totals

Cross-posting this here from Stack Exchange, as I haven’t gotten much traction there: http://stackoverflow.com/questions/41166479/complex-featured-product-model-using-query-time-ranking-optional-filters

I have a products index that displays filtered results on category pages.

For a given category, any amount of products may be flagged as featured, meaning it displays first.
When products are displayed for a category, only one featured product should show at a time (at random from the available products flagged as featured)
Additionally, a product should not be flagged as featured if it has date range fields and the current date is not within the range
So, my index might look something like: https://gist.github.com/1a0327d8a321dc6627e197b94f4209c9

It seems like I need to perform 2 queries, but I don’t know how to combine them without throwing off pagination and totals.

Any guidance is greatly appreciated!

1 Like

Thanks for posting over here @timkelty. It’s end-of-day Friday SF time and I don’t have insight on this atm. But rest assured we’ll get some :eyes: on it next week.

Sounds good @dzello, thanks.

I made a related post here, which is a piece of the puzzle: How to conditionally set hitsPerPage?

1 Like

Hi @timkelty I am not sure there will be an automated easy way to do that. Usually showing featured product on top can be achieved by doing a multi index search like this: https://jsfiddle.net/j9nwpz34/

Using this and forwarding some helper parameters you should be able to achieve what you want. Let us know how it goes.

@vvo That should work, yes - although I’ll likely have to use a custom pagination widget then.

Before I go that route, I want to confirm: since these results are already in the returned query results, all I really want to do is pluck them out of the results and place them first. Am I correct in saying there isn’t really a way to do that, because of how Algolia handles ranking?

Indeed that’s the way Algolia handles it. We yet have no ways to achieve easily what you want (but the enterprise optionalFacetFilters). The engine was built with text relevance in mind and we are iterating on adjusting the feature set to better allow what you are trying to do (injecting one popular result)

In the meantime you will have to use the custom pagination and multi instantsearch instances (completely OK performance wise still).

Thanks for reaching out!

@vvo Thanks for the explanation!