Skip to content

A platform for community discussion. Free, open, simple.

DEV: Improve site setting search ranking (#32461)

Ted Johanssonb056f9538b30
diff --git a/app/assets/javascripts/admin/addon/lib/site-setting-filter.js b/app/assets/javascripts/admin/addon/lib/site-setting-filter.js
index b1a9d3fe3b8..769e370a9fb 100644
--- a/app/assets/javascripts/admin/addon/lib/site-setting-filter.js
+++ b/app/assets/javascripts/admin/addon/lib/site-setting-filter.js
@@ -1,5 +1,6 @@
import { bind } from "discourse/lib/decorators";
import { i18n } from "discourse-i18n";
+import SiteSettingMatcher from "admin/lib/site-setting-matcher";
export default class SiteSettingFilter {
constructor(siteSettings) {
@@ -53,62 +54,55 @@ export default class SiteSettingFilter {
matchesGroupedByCategory.push(all);
}
- 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");
- }
-
this.siteSettings.forEach((settingsCategory) => {
let fuzzyMatches = [];
- const siteSettings = settingsCategory.siteSettings.filter((item) => {
- if (opts.onlyOverridden && !item.get("overridden")) {
- return false;
- }
- if (pluginFilter && item.plugin !== pluginFilter) {
- return false;
- }
- if (filter) {
- const setting = item.get("setting").toLowerCase();
- let filterResult =
- setting.includes(filter) ||
- setting.replace(/_/g, " ").includes(filter) ||
- item.get("description").toLowerCase().includes(filter) ||
- (item.get("keywords") || []).any((keyword) =>
- keyword
- .replace(/_/g, " ")
- .toLowerCase()
- .includes(filter.replace(/_/g, " "))
- ) ||
- (item.get("value") || "").toString().toLowerCase().includes(filter);
- if (!filterResult && fuzzyRegex && fuzzyRegex.test(setting)) {
- // Tightens up fuzzy search results a bit.
- const fuzzySearchLimiter = 25;
- const strippedSetting = setting.replace(/[^a-z0-9]/gi, "");
- if (
- strippedSetting.length <=
- strippedQuery.length + fuzzySearchLimiter
- ) {
- const gapResult = strippedSetting.match(fuzzyRegexGaps);
- if (gapResult) {
- item.weight = gapResult.filter((gap) => gap !== "").length;
- }
- fuzzyMatches.push(item);
- }
+ const siteSettings = settingsCategory.siteSettings.filter(
+ (siteSetting) => {
+ siteSetting.weight = 0;
+
+ if (opts.onlyOverridden && !siteSetting.get("overridden")) {
+ return false;
}
- return filterResult;
- } else {
- return true;
- }
- });
- if (fuzzyMatches.length > 0) {
- siteSettings.pushObjects(fuzzyMatches);
- }
+ if (pluginFilter && siteSetting.plugin !== pluginFilter) {
+ return false;
+ }
+
+ if (!filter) {
+ return true;
+ }
+
+ const matcher = new SiteSettingMatcher(filter, siteSetting);
+
+ if (matcher.isNameMatch) {
+ siteSetting.weight = 10;
+ return true;
+ }
+
+ if (matcher.isKeywordMatch) {
+ siteSetting.weight = 5;
+ return true;
+ }
+
+ if (matcher.isDescriptionMatch) {
+ return true;
+ }
+
+ if (matcher.isValueMatch) {
+ return true;
+ }
+
+ if (matcher.isFuzzyNameMatch) {
+ siteSetting.weight += matcher.matchStrength;
+ fuzzyMatches.push(siteSetting);
+
+ return true;
+ }
+
+ return false;
+ }
+ );
if (siteSettings.length > 0) {
matches.pushObjects(siteSettings);
@@ -138,9 +132,9 @@ export default class SiteSettingFilter {
@bind
sortSettings(settings) {
// Sort the site settings so that fuzzy results are at the bottom
- // and ordered by their gap count asc.
+ // and ordered by their match strength.
return settings.sort((a, b) => {
- return (a.weight || 0) - (b.weight || 0);
+ return (b.weight || 0) - (a.weight || 0);
});
}
}
diff --git a/app/assets/javascripts/admin/addon/lib/site-setting-matcher.js b/app/assets/javascripts/admin/addon/lib/site-setting-matcher.js
new file mode 100644
index 00000000000..8d355371a10
--- /dev/null
+++ b/app/assets/javascripts/admin/addon/lib/site-setting-matcher.js
@@ -0,0 +1,75 @@
+export default class SiteSettingMatcher {
+ constructor(filter, siteSetting) {
+ this.filter = filter;
+ this.siteSetting = siteSetting;
+ this.strippedQuery = filter.replace(/[^a-z0-9]/gi, "");
+ this.fuzzyRegex = new RegExp(this.strippedQuery.split("").join(".*"), "i");
+ this.fuzzyRegexGaps = new RegExp(
+ ".*" + this.strippedQuery.split("").join("(.*)"),
+ "i"
+ );
+ this.matchStrength = 0;
+ }
+
+ get isNameMatch() {
+ const name = this.siteSetting.setting.toLowerCase();
+
+ return (
+ name.includes(this.filter) ||
+ name.replace(/_/g, " ").includes(this.filter)
+ );
+ }
+
+ get isKeywordMatch() {
+ return (this.siteSetting.keywords || []).any((keyword) =>
+ keyword
+ .replace(/_/g, " ")
+ .toLowerCase()
+ .includes(this.filter.replace(/_/g, " "))
+ );
+ }
+
+ get isDescriptionMatch() {
+ return this.siteSetting.description.toLowerCase().includes(this.filter);
+ }
+
+ get isValueMatch() {
+ return (this.siteSetting.value || "")
+ .toString()
+ .toLowerCase()
+ .includes(this.filter);
+ }
+
+ get isFuzzyNameMatch() {
+ const name = this.siteSetting.setting.toLowerCase();
+
+ if (this.strippedQuery.length < 3) {
+ return false;
+ }
+
+ if (!this.fuzzyRegex.test(name)) {
+ return false;
+ }
+
+ const fuzzySearchLimiter = 25;
+ const strippedSetting = name.replace(/[^a-z0-9]/gi, "");
+
+ if (
+ strippedSetting.length >
+ this.strippedQuery.length + fuzzySearchLimiter
+ ) {
+ return false;
+ }
+
+ const gapResult = strippedSetting.match(this.fuzzyRegexGaps);
+
+ if (gapResult) {
+ // Discard empty gaps and disregard the full string match.
+ const numberOfGaps = gapResult.filter((gap) => gap !== "").length - 1;
+
+ this.matchStrength -= numberOfGaps;
+ }
+
+ return true;
+ }
+}