DEV: Display fuzzy site setting search results below direct matches (#23197)

DEV: Display fuzzy site setting search results below direct matches

When searching for site settings, in the results under the ALL category
all the fuzzy search results were showing first followed by any direct
matches. This change adjusts that so that fuzzy searches show below
direct matches.

Fuzzy results are now also sorted based on their gap calculation in
ascending order.
This commit is contained in:
Blake Erickson 2023-08-24 17:47:40 -06:00 committed by GitHub
parent c8a5ce9302
commit 9238a9cf42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View File

@ -22,6 +22,16 @@ export default class AdminSiteSettingsController extends Controller {
return 100;
}
sortSettings(settings) {
// Sort the site settings so that fuzzy results are at the bottom
// and ordered by their gap count asc.
return settings.sort((a, b) => {
const aWeight = a.weight === undefined ? 0 : a.weight;
const bWeight = b.weight === undefined ? 0 : b.weight;
return aWeight - bWeight;
});
}
performSearch(filter, allSiteSettings, onlyOverridden) {
let pluginFilter;
@ -56,9 +66,11 @@ export default class AdminSiteSettingsController extends Controller {
const strippedQuery = filter.replace(/[^a-z0-9]/gi, "");
let fuzzyRegex;
let fuzzyRegexGaps;
if (strippedQuery.length > 2) {
fuzzyRegex = new RegExp(strippedQuery.split("").join(".*"), "i");
fuzzyRegexGaps = new RegExp(strippedQuery.split("").join("(.*)"), "i");
}
allSiteSettings.forEach((settingsCategory) => {
@ -86,6 +98,10 @@ export default class AdminSiteSettingsController extends Controller {
strippedSetting.length <=
strippedQuery.length + fuzzySearchLimiter
) {
const gapResult = strippedSetting.match(fuzzyRegexGaps);
if (gapResult) {
item.weight = gapResult.filter((gap) => gap !== "").length;
}
fuzzyMatches.push(item);
}
}
@ -106,13 +122,15 @@ export default class AdminSiteSettingsController extends Controller {
name: I18n.t(
"admin.site_settings.categories." + settingsCategory.nameKey
),
siteSettings,
siteSettings: this.sortSettings(siteSettings),
count: siteSettings.length,
});
}
});
all.siteSettings.pushObjects(matches.slice(0, this.maxResults));
all.siteSettings = this.sortSettings(all.siteSettings);
all.hasMore = matches.length > this.maxResults;
all.count = all.hasMore ? `${this.maxResults}+` : matches.length;
all.maxResults = this.maxResults;

View File

@ -48,6 +48,13 @@ module("Unit | Controller | admin-site-settings", function (hooks) {
assert.deepEqual(results[0].siteSettings.length, 2);
// ensures hello world shows up before fuzzy hpello world
assert.deepEqual(results[0].siteSettings[0].setting, "hello world");
results = controller.performSearch("world", settings2);
assert.deepEqual(results[0].siteSettings.length, 2);
// ensures hello world shows up before fuzzy hpello world with "world" search
assert.deepEqual(results[0].siteSettings[0].setting, "hello world");
// ensures fuzzy search limiter is in place
results = controller.performSearch("digest", settings2);
assert.deepEqual(results[0].siteSettings.length, 1);
assert.deepEqual(results[0].siteSettings[0].setting, "digest_logo");