I used Fuse.js to implement a search system to my discord bot. However when I give the search string "Paper Beauty (Awakened)", It returns another Item that is similar to it as the first element in the array.
Results:
[
{
item: 'Paper Beauty (Winter)',
refIndex: 77,
score: 0.01857804455091699
},
{
item: 'Paper Beauty (Awakened)',
refIndex: 135,
score: 0.01857804455091699
},
{ item: 'Patternine', refIndex: 122, score: 0.4 },
{ item: 'Bright Reaper', refIndex: 31, score: 0.40657505381748826 },
{ item: 'Spade', refIndex: 112, score: 0.41000000000000003 },
{ item: 'Puppet', refIndex: 61, score: 0.42000000000000004 },
{ item: 'Expert Sorcerer', refIndex: 8, score: 0.5231863610884103 },
{ item: 'Water Goddess', refIndex: 13, score: 0.5231863610884103 },
{ item: 'Water Bender', refIndex: 41, score: 0.5231863610884103 },
{ item: 'Super Firework', refIndex: 176, score: 0.5231863610884103 },
{ item: 'Spider Boss', refIndex: 44, score: 0.5324001715007329 }
]
Code:
const Fuse = require("fuse.js");
const { trendSystem, demandSystem } = require("../../util/functions");
const names = require("../../names.json");
module.exports = {
name: "stats",
category: "trading",
devOnly: false,
run: async ({ client, message, args }) => {
const options = {
includeScore: true,
};
const searcher = new Fuse(names, options);
const results = searcher.search(args[0]).slice(0, 11)
console.log(results)
}
}
The scoring is done on the basis of three factors:
#Distance, Threshold, and Location The calculation for something to be considered a match (whether fuzzy or exact) takes into account how far the pattern is from the expected location, within a threshold.
To illustrate, consider the following options:
With the above options, for something to be considered a match, it would have to be within (threshold) 0.6 x (distance) 100 = 60 characters away from the expected location 0.
In your case it might be happening too:
For example, consider the string "Fuse.js is a powerful, lightweight fuzzy-search library, with zero dependencies". Searching for the pattern "zero" would not match anything, even though it occurs in the string. The reason is that with the above defaults, for it to be considered a match it would have to be within 60 characters away from the expected location 0. However, "zero" appears at index 62.
Note: you need to tweak the configuration in order to get the correct results.