fix(editor): Reduce length penalty so longer node names rank fairly (#31294)

This commit is contained in:
Charlie Kolb 2026-06-01 10:43:10 +02:00 committed by GitHub
parent 00431d7505
commit 1b8235ef76
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 5 deletions

View File

@ -1,22 +1,36 @@
import { reRankSearchResults } from './reRankSearchResults';
import topLevel from './snapshots/toplevel.snapshot.json';
import { sublimeSearch } from './sublimeSearch';
// Mirrors packages/frontend/editor-ui/data/node-popularity.json scaled by the factor
// applied in useViewStacks.ts. Only the nodes asserted below are included.
const popularity: Record<string, number> = {
/* eslint-disable @typescript-eslint/naming-convention */
'n8n-nodes-base.set': 0.959 * 100,
'n8n-nodes-base.editImage': 0.617 * 100,
'@n8n/n8n-nodes-langchain.agent': 0.975 * 100,
/* eslint-enable @typescript-eslint/naming-convention */
};
describe('sublimeSearch', () => {
describe('search finds specific matches first', () => {
// Note that this only tests the order of the specified matches
// Further results may appear after the listed ones
// Mirrors the production pipeline in useViewStacks.ts:
// sublimeSearch(query, items) → reRankSearchResults(results, { popularity })
// Note that this only tests the order of the specified matches.
// Further results may appear after the listed ones.
const testCases: Array<[string, string[]]> = [
['set', ['Edit Fields (Set)']],
['edit', ['Edit Fields (Set)']],
['agent', ['AI Agent', 'Magento 2']],
];
test.each(testCases)(
'should return at least "$expectedOrder" for filter "$filter"',
(filter, expectedOrder) => {
// These match the weights in the production use case
const results = sublimeSearch(filter, topLevel);
const reRanked = reRankSearchResults(results, { popularity });
const resultNames = results.map((result) => result.item.properties.displayName);
const resultNames = reRanked.map((result) => result.item.properties.displayName);
expectedOrder.forEach((expectedName, index) => {
expect(resultNames[index]).toBe(expectedName);
});

View File

@ -10,7 +10,7 @@ const FIRST_LETTER_BONUS = 15; // bonus if the first letter is matched
const LEADING_LETTER_PENALTY = -20; // penalty applied for every letter in str before the first match
const MAX_LEADING_LETTER_PENALTY = -200; // maximum penalty for leading letters
const UNMATCHED_LETTER_PENALTY = -5;
const UNMATCHED_LETTER_PENALTY = -2.5;
export const DEFAULT_KEYS = [
{ key: 'properties.displayName', weight: 1.3 },