fix(scriptindex): Improve script search: use ObjectSorter with weighted results prioritizing slug and name
This commit is contained in:
@@ -272,29 +272,35 @@ export class ScriptIndex {
|
||||
scripts = scripts.filter((script) => script.type === typeFilter);
|
||||
}
|
||||
|
||||
// Use smartfuzzy for ranking
|
||||
const fuzzyMatcher = new plugins.smartfuzzy.FuzzyMatcher();
|
||||
// Use ObjectSorter for fuzzy searching across multiple fields
|
||||
const sorter = new plugins.smartfuzzy.ObjectSorter(scripts);
|
||||
|
||||
const scoredResults = scripts.map((script) => {
|
||||
// Calculate match scores for each field
|
||||
const nameScore = script.name ? fuzzyMatcher.match(query, script.name) : 0;
|
||||
const slugScore = script.slug ? fuzzyMatcher.match(query, script.slug) : 0;
|
||||
const descScore = script.description ? fuzzyMatcher.match(query, script.description) : 0;
|
||||
// Search across slug, name, and description
|
||||
const results = sorter.sort(query, ['slug', 'name', 'description']);
|
||||
|
||||
// Prioritize: slug > name > description
|
||||
const totalScore = slugScore * 3 + nameScore * 2 + descScore;
|
||||
// Post-process to weight results by which field matched
|
||||
const weightedResults = results.map((result) => {
|
||||
let weight = 1;
|
||||
|
||||
// Boost score if match was in slug (highest priority)
|
||||
if (result.matches?.some((m) => m.key === 'slug')) {
|
||||
weight = 3;
|
||||
}
|
||||
// Boost if match was in name (medium priority)
|
||||
else if (result.matches?.some((m) => m.key === 'name')) {
|
||||
weight = 2;
|
||||
}
|
||||
|
||||
return {
|
||||
script,
|
||||
score: totalScore,
|
||||
...result,
|
||||
adjustedScore: (result.score || 0) / weight,
|
||||
};
|
||||
});
|
||||
|
||||
// Filter out non-matches and sort by score
|
||||
return scoredResults
|
||||
.filter((result) => result.score > 0)
|
||||
.sort((a, b) => b.score - a.score)
|
||||
.map((result) => result.script);
|
||||
// Sort by adjusted score and return just the script objects
|
||||
return weightedResults
|
||||
.sort((a, b) => (a.adjustedScore || 0) - (b.adjustedScore || 0))
|
||||
.map((result) => result.item);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user