Skip to content

FIX: improvements for admin search #33006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const MAX_TYPE_RESULT_COUNT_HIGH = 50;

const SEARCH_SCORES = {
labelStart: 20,
labelPartial: 15,
exactKeyword: 10,
partialKeyword: 7,
fallback: 5,
pageBonusScore: 20,
};
Expand Down Expand Up @@ -268,7 +270,9 @@ export default class AdminSearchDataSource extends Service {
}

let filteredResults = [];
const escapedFilterRegExp = escapeRegExp(filter.toLowerCase());
const escapedFilterRegExp = escapeRegExp(
filter.toLowerCase().trim().replace(/\s+/g, " ")
);

// Pointless to render heaps of settings if the filter is quite low.
const perTypeLimit =
Expand All @@ -277,9 +281,15 @@ export default class AdminSearchDataSource extends Service {
: MAX_TYPE_RESULT_COUNT_HIGH;

const labelStartRegex = new RegExp(`^${escapedFilterRegExp}`, "i");
const labelPartialRegex = new RegExp(`\\b${escapedFilterRegExp}`, "i");
const exactKeywordRegexes = escapedFilterRegExp
.split(" ")
.filter((keyword) => keyword.length > 3)
.map((keyword) => new RegExp(`(${keyword})\\b`, "i"));
const partialKeywordRegexes = escapedFilterRegExp
.split(" ")
.filter((keyword) => keyword.length > 3)
.map((keyword) => new RegExp(`\\b${keyword}`, "i"));
const fallbackRegex = new RegExp(`${escapedFilterRegExp}`, "i");

ADMIN_SEARCH_RESULT_TYPES.forEach((type) => {
Expand All @@ -289,16 +299,27 @@ export default class AdminSearchDataSource extends Service {

if (dataSourceItem.label.match(labelStartRegex)) {
dataSourceItem.score += SEARCH_SCORES.labelStart;
} else if (dataSourceItem.label.match(labelPartialRegex)) {
dataSourceItem.score += SEARCH_SCORES.labelPartial;
}
if (
exactKeywordRegexes.length > 0 &&
exactKeywordRegexes.every((regex) => {
return dataSourceItem.label.match(regex);
return dataSourceItem.keywords.match(regex);
})
) {
dataSourceItem.score = dataSourceItem.score +=
SEARCH_SCORES.exactKeyword;
} else if (
partialKeywordRegexes.length > 0 &&
partialKeywordRegexes.every((regex) => {
return dataSourceItem.keywords.match(regex);
})
) {
dataSourceItem.score = dataSourceItem.score +=
SEARCH_SCORES.partialKeyword;
}
if (dataSourceItem.keywords.match(fallbackRegex)) {
if (filter.length > 3 && dataSourceItem.keywords.match(fallbackRegex)) {
dataSourceItem.score += SEARCH_SCORES.fallback;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ module("Unit | Service | AdminSearchDataSource", function (hooks) {
{
description: "first page",
icon: "house",
keywords: "exact setting",
label: "Page about exact setting",
keywords: "exact settings",
label: "Page about whatever",
type: "page",
url: "/admin",
},
Expand All @@ -152,14 +152,14 @@ module("Unit | Service | AdminSearchDataSource", function (hooks) {
{
description: "first setting",
icon: "house",
keywords: "exact setting",
keywords: "exact settings",
label: "exact setting",
type: "setting",
url: "/admin",
},
];
let results = this.subject.search("exact setting");
assert.deepEqual(results[0].label, "Page about exact setting");
let results = this.subject.search("exact setting");
assert.deepEqual(results[0].label, "Page about whatever");
});
});

Expand Down
2 changes: 2 additions & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5373,6 +5373,7 @@ en:

logo:
title: "Logo"
header_description: "Configure the logos and icons used throughout your site"
form:
saved: "Logo settings are saved."
logo:
Expand Down Expand Up @@ -5437,6 +5438,7 @@ en:
help_text: "recommended size is at least 280 x 150 pixels. Don't use an SVG image."
fonts:
title: "Fonts"
header_description: "Configure the fonts used throughout your site, including base font, heading font, and default text size"
form:
more_fonts: "More fonts"
fewer_fonts: "Fewer fonts"
Expand Down
Loading