I need Algolia to always return me 5 results from a full text search even if the query text itself bears little or no relevance to the actual returned results. Before someone suggests it, I have already tried to set the removeWordsIfNoResults
option to all of it's possible modes and this still doesn't guarantee that I get my 5 results.
The purpose of this is to create a 'relevant entities' sidebar where the name of the current entity is used to search for other entities.
Any suggestions?
Using the
removeWordsIfNoResults=allOptional
query parameter is indeed a good way to go -> because all query words are required to match an object by default, fallbacking to "optional" is a good way to still retrieve results if one you the query words (or the combination of words) doesn't match anything.Another solution is to always consider all query words as optional (not only as a fallback); to make sure the query
foo bar baz
is interpreted asOPT(foo) AND OPT(bar) AND OPT(baz)
<=>foo OR bar OR baz
. The difference is that this query will retrieve more results than the previous one because 1 single matching word will be enough to retrieve the object.That being said, there is no way to force the engine to retrieve "at least" 5 results. What I would recommend is to have a small frontend logic: - do the query with
removeWordsIfNoResults
oroptionalWords
- if the engines returns less than 5 results, do another query