krisk / Fuse

Lightweight fuzzy-search, in JavaScript
https://fusejs.io/
Apache License 2.0
18.1k stars 766 forks source link

The matches returned in a result should be ordered by most relevant #275

Closed loucadufault closed 4 years ago

loucadufault commented 5 years ago

The issue here is that when a result is returned with the option includeMatches:true, I would expect that in any one of the results, the matches of that result would be ordered by relevance, or highest fraction of matched characters over total chars. I would expect to simply access the most relevant match by doing:

  var result = fuse.search(searchQuery); 

  if (result.length > 0) {
    for(var i = 0; i < result.length; i++) { 
        var resultDict = result[i]; //for each result dict found in the result array returned by fuse.js
        var item = resultDict["item"]; //get the item of that current resultDict (id)
        ...
        var match = resultDict["matches"][0]["value"]; //this should work but it doesn't !
        ...
     } //endif
     ...
} //endfor

(the last line of code (var match = ... is what I am referring to)

Because of this I find myself having to create logic to iterate through each match, tally up the indices of matched characters in matches[i]["indices"] for each match in each result and divide by the match.length to get a matchScore. Then select the match with the highest score. This is a good 40 lines of code, especially with the complex array structure of the indices.

Would really be great if this could be done by Fuse itself, possibly as an option. The current behaviour is to sort by ["arrayIndex"], which seems like a completely arbitrary decision.

heyimalex commented 5 years ago

The matched indices don't have any association with "match score". The code for telling whether a character is matched looks basically like this:

const query = "old";
const text = "old new borrowed blue";
const queryChars = new Set([...query]);
for (let char of text) {
  if (queryChars.has(char)) {
    // it's a match
  }
}

It's useful for highlighting, but really doesn't correspond to an actual "match". According to your ranking algorithm, a search for "faxts" would give a higher score to "ssss" than "facts".

github-actions[bot] commented 4 years ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days