Algolia search with a certain category first

不问归期 提交于 2019-12-11 08:44:04

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/40920245/algolia-search-with-a-certain-category-first

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!