A platform for community discussion. Free, open, simple.
FIX: improvements for admin search (#32574)
Krzysztof Kotlarekbff638888c4b
diff --git a/app/assets/javascripts/admin/addon/components/admin-search.gjs b/app/assets/javascripts/admin/addon/components/admin-search.gjsindex 4ac4335cdb6..60eb1ebdfc0 100644--- a/app/assets/javascripts/admin/addon/components/admin-search.gjs+++ b/app/assets/javascripts/admin/addon/components/admin-search.gjs@@ -62,10 +62,6 @@ export default class AdminSearch extends Component {);}- get showLoadingSpinner() {- return !this.adminSearchDataSource.isLoaded || this.loading;- }-get noResultsDescription() {return i18n("admin.search.no_results", {filter: escapeExpression(this.filter),@@ -215,7 +211,7 @@ export default class AdminSearch extends Component {{{/if}}<div class="admin-search__results">- <ConditionalLoadingSpinner @condition={{this.showLoadingSpinner}}>+ <ConditionalLoadingSpinner @condition={{this.loading}}>{{#each this.searchResults as |result|}}<div class="admin-search__result" data-result-type={{result.type}}><adiff --git a/app/assets/javascripts/admin/addon/services/admin-search-data-source.js b/app/assets/javascripts/admin/addon/services/admin-search-data-source.jsindex e56134c1fd5..85f66e83333 100644--- a/app/assets/javascripts/admin/addon/services/admin-search-data-source.js+++ b/app/assets/javascripts/admin/addon/services/admin-search-data-source.js@@ -15,6 +15,12 @@ const MIN_FILTER_LENGTH = 2;const MAX_TYPE_RESULT_COUNT_LOW = 15;const MAX_TYPE_RESULT_COUNT_HIGH = 50;+const SEARCH_SCORES = {+ labelStart: 20,+ exactKeyword: 10,+ fallback: 5,+};+function labelOrText(obj, fallback = "") {return obj.text || (obj.label ? i18n(obj.label) : fallback);}@@ -262,7 +268,7 @@ export default class AdminSearchDataSource extends Service {opts.types = opts.types || ADMIN_SEARCH_RESULT_TYPES;- const filteredResults = [];+ let filteredResults = [];const escapedFilterRegExp = escapeRegExp(filter.toLowerCase());// Pointless to render heaps of settings if the filter is quite low.@@ -271,22 +277,41 @@ export default class AdminSearchDataSource extends Service {? MAX_TYPE_RESULT_COUNT_LOW: MAX_TYPE_RESULT_COUNT_HIGH;+ const labelStartRegex = new RegExp(`^${escapedFilterRegExp}`, "i");+ const exactKeywordRegexes = escapedFilterRegExp+ .split(" ")+ .map((keyword) => new RegExp(`(${keyword})\\b`, "i"));+ const fallbackRegex = new RegExp(`${escapedFilterRegExp}`, "i");+opts.types.forEach((type) => {- let typeItemCount = 0;+ const typeResults = [];this[`${type}DataSourceItems`].forEach((dataSourceItem) => {- // TODO (martin) There is likely a much better way of doing this matching- // that will support fuzzy searches, for now let's go with the most basic thing.+ dataSourceItem.score = 0;++ if (dataSourceItem.label.match(labelStartRegex)) {+ dataSourceItem.score += SEARCH_SCORES.labelStart;+ }if (- dataSourceItem.keywords.match(escapedFilterRegExp) &&- typeItemCount <= perTypeLimit+ exactKeywordRegexes.every((regex) => {+ return dataSourceItem.label.match(regex);+ })) {- filteredResults.push(dataSourceItem);- typeItemCount++;+ dataSourceItem.score = dataSourceItem.score +=+ SEARCH_SCORES.exactKeyword;+ }+ if (dataSourceItem.keywords.match(fallbackRegex)) {+ dataSourceItem.score += SEARCH_SCORES.fallback;+ }++ if (dataSourceItem.score > 0) {+ typeResults.push(dataSourceItem);}});+ filteredResults = filteredResults.concat(+ typeResults.sort((a, b) => b.score - a.score).slice(0, perTypeLimit)+ );});-- return filteredResults;+ return filteredResults.sort((a, b) => b.score - a.score);}#addPageLink(navMapSection, link, parentLabel = "") {diff --git a/app/assets/javascripts/discourse/tests/fixtures/admin-search-fixtures.js b/app/assets/javascripts/discourse/tests/fixtures/admin-search-fixtures.jsindex 03e3b653051..c33c4ee57b6 100644--- a/app/assets/javascripts/discourse/tests/fixtures/admin-search-fixtures.js+++ b/app/assets/javascripts/discourse/tests/fixtures/admin-search-fixtures.js@@ -47,7 +47,7 @@ export default {type: "page_view_anon_browser_reqs",title: "Anonymous Browser Pageviews",description:- "Number of pageviews by anonymous visitors using real browsers.",+ "Number of pageviews by anonymous visitors using real browsers. Anonym to test word matching.",description_link: null,},{diff --git a/app/assets/javascripts/discourse/tests/integration/components/admin-search-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/admin-search-test.gjsindex 3bceb5d29a0..373d33425ca 100644--- a/app/assets/javascripts/discourse/tests/integration/components/admin-search-test.gjs+++ b/app/assets/javascripts/discourse/tests/integration/components/admin-search-test.gjs@@ -54,7 +54,7 @@ module("Integration | Component | AdminSearch", function (hooks) {await click(filterButtonCss("setting"));assert.dom(".admin-search__result").doesNotExist();- await fillIn(".admin-search__input-field", "admin login");+ await fillIn(".admin-search__input-field", "admin logins");assert.dom(".admin-search__result").exists({ count: 1 });await click(filterButtonCss("report"));diff --git a/app/assets/javascripts/discourse/tests/unit/services/admin-search-data-source-test.js b/app/assets/javascripts/discourse/tests/unit/services/admin-search-data-source-test.jsindex b382e0aec60..91bfd4a527d 100644--- a/app/assets/javascripts/discourse/tests/unit/services/admin-search-data-source-test.js+++ b/app/assets/javascripts/discourse/tests/unit/services/admin-search-data-source-test.js@@ -134,6 +134,18 @@ module("Unit | Service | AdminSearchDataSource", function (hooks) {"/admin/reports/page_view_anon_browser_reqs");});++ test("search - prioritize whole word matches", async function (assert) {+ await this.subject.buildMap();+ let results = this.subject.search("anonym");+ assert.deepEqual(results[0].label, "Anonymous Browser Pageviews");+ });++ test("search - prioritize beginning of label", async function (assert) {+ await this.subject.buildMap();+ let results = this.subject.search("about");+ assert.deepEqual(results[0].label, "About your site > Title");+ });});module(diff --git a/app/assets/stylesheets/admin/search.scss b/app/assets/stylesheets/admin/search.scssindex 032d861a243..b60a8f6d3cb 100644--- a/app/assets/stylesheets/admin/search.scss+++ b/app/assets/stylesheets/admin/search.scss@@ -155,5 +155,4 @@.admin-search-modal {--modal-max-width: 700px;- --modal-min-height: 200px;}