How do I make Algolia Search guaruntee that it will return a number of results

329 views Asked by At

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?

1

There are 1 answers

0
redox On BEST ANSWER

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.

index.search(query, { removeWordsIfNoResults: 'allOptional' });

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 as OPT(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.

index.search(query, { optionalWords: query });

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 or optionalWords - if the engines returns less than 5 results, do another query