lucaong / minisearch

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

Minimum Length Prefix Match #203

Closed oasiz closed 1 year ago

oasiz commented 1 year ago

Hi, hope you're well!

This is for a search of artists/songs.

If I do a search for "R Kelly" I'm (as well as results for 'R Kelly' of course) getting results for this too:

{
    artist: "Machine Gun Kelly",
    title: "RAP DEVIL",
    album: "RAP DEVIL [Explicit]"
}

Now that's fine to match the artist name, but it's also showing "rap" as a result word match for the title and album. The result terms are ['rap', 'kelly'].

Is it possible to have a minimum length for individual term matches as in this example I think it should only match the artist name and not the title/album.

e.g Only match ahead for words with 2+ characters - or as a percentage of the matched word length.

Prefix search works very well for my use, so that cannot be turned off, but "r" matching "rap" (or any word beginning with "r") is pretty speculative - I'd like to find a way to restrict that it if possible.

Please let me know if any of that makes sense (or not!) and if there's a solution.

Thanks,

Rob

lucaong commented 1 year ago

Hi @oasiz , Yes, it’s possible: the prefix search option can be set to a function , which is called passing the search term (and other parameters), and should return a boolean, determining if prefix search is performed or not.

Suppose you want to perform prefix search only on terms that are at least 3 characters long, you can do this:

const miniSearch = new MiniSearch({
  fields: [/* … */],
  searchOptions: {
    prefix: (term) => term.length >= 3
  }
})

I hope this helps.

oasiz commented 1 year ago

Hi @lucaong, brilliant - thanks for that!

lucaong commented 1 year ago

Happy to help :)