lucaong / minisearch

Tiny and powerful JavaScript full-text search engine for browser and Node
https://lucaong.github.io/minisearch/
MIT License
4.81k stars 137 forks source link

Max nubmer of results #148

Closed kozi closed 2 years ago

kozi commented 2 years ago

Is there a way to specify a maximum number of hits? I can of course truncate the result set but the search algorithm could possibly stop earlier if N hits were found, right?

lucaong commented 2 years ago

Hi @kozi , that's a good question, and a common need in search applications.

The reason why such option is not provided inside MiniSearch is that the algorithm cannot really do that more efficiently than collecting all results first, then truncating the list to the first N. That's because the relevance score can only be calculated at runtime for a given search, so the algorithm must first retrieve and score all matching results, then sort them by score, and only after it could truncate the list. In other words, the relevance score of a document depends on the specific search, so it is not possible to retrieve documents from the index already in order of decreasing score.

In sum, a limit option limiting the results to the top N could be implemented, but MiniSearch would do that by truncating the result set, no differently than how it can be done in user code.

lucaong commented 2 years ago

Hi @kozi , I will close the issue for now, as I think my reply answers it, but feel free to comment further if necessary, and in case I can reopen it.