weighted search on multiple keys #590
|
I wondered if it's possible using Fuse to search multiple values and keys? I would like to sort this list of jobs to find the closest one for a search. And if the user was searching for a job with: It would show the "Journalist" object as it's the closest to all weighted keys. I'm just not quite sure how to do this in general and wondered If I could use Fuse for this? |
Replies: 2 comments
|
You could use extended search. Perhaps you may also want to pair it with logical query operations |
|
Looking at this again, my earlier suggestion wasn't great for this use case — this is really a numeric distance/nearest-neighbor problem, not a text search one. Fuse operates on string matching, so it's not the right tool for comparing numeric attribute vectors. For this kind of thing, you'd want to compute something like Euclidean distance between the query object and each job, then sort by that. A simple approach: const query = { people: 8, physical: 1, mental: 7, location: 6, money: 5, value: 4 }
const keys = Object.keys(query)
const sorted = jobs
.map(job => ({
item: job,
distance: Math.sqrt(keys.reduce((sum, k) => sum + (job[k] - query[k]) ** 2, 0))
}))
.sort((a, b) => a.distance - b.distance) |
Looking at this again, my earlier suggestion wasn't great for this use case — this is really a numeric distance/nearest-neighbor problem, not a text search one. Fuse operates on string matching, so it's not the right tool for comparing numeric attribute vectors.
For this kind of thing, you'd want to compute something like Euclidean distance between the query object and each job, then sort by that. A simple approach: