I'm trying to search Algolia and have results for a certain category show up before all other categories. Here's an example:
Data in Algolia
{ name: Harry Potter, category: book},
{ name: The Avengers, category: movie},
{ name: Pottery, category: movie}
Problem
Let's say the normal Algolia algorithm has Harry Potter
way more relevant than the movie Pottery
, so normally if you searched pot
then Harry Potter
would show up ahead of Pottery
.
I want to pass Algolia the search term pot
and the category movie
and then have Pottery
show up ahead of everything else. It needs to be dynamic, i.e. I should be able to search pot
with category book
and get Harry Potter
first.
Is there a way to do this with Algolia?
There is actually a nice way to implement that behavior using "optional" facet filters (a soon to be released advanced feature - as of 2016/12/01).
An "Optional Facet Filter" is a facet filter that doesn't need to match to retrieve a result but that will - by default - make sure the hits that have the facet value are retrieved first (thanks to the filters
criterion of Algolia's tie-breaking ranking formula).
This is exactly what you want: on every single page where you want some results sharing a category
value to be retrieved first; just query the Algolia index with the category:value
optional facet filter.
- make sure your
category
attribute is part of your attributesForFacet
index setting
- at query time, query the index with
index.search('', { optionalFacetFilters: ["category:book"])
You can read more on this (beta) documentation page.