Skip to content
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

[Front Search] Better, stabler, cooler, stronger sort order for results #4212

Merged
merged 4 commits into from
Mar 8, 2024
Merged
Changes from 2 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
88 changes: 79 additions & 9 deletions front/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ export const objectToMarkdown = (obj: any, indent = 0) => {
return markdown;
};

// Returns true if a is a subsequence of b.
export function subFilter(a: string, b: string) {
function subFilterLastIndex(a: string, b: string) {
let i = 0;
let j = 0;
while (i < a.length && j < b.length) {
Expand All @@ -145,16 +144,61 @@ export function subFilter(a: string, b: string) {
}
j++;
}
return i === a.length;
return i === a.length ? j : -1;
}

function subFilterFirstIndex(a: string, b: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what this does. Could you add a comment ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 made one as clear as I could, hope it's fine

let i = a.length - 1;
let j = b.length - 1;
while (i >= 0 && j >= 0) {
if (a[i] === b[j]) {
i--;
}
j--;
}
return i === -1 ? j + 1 : -1;
}

/**
* Returns true if a is a subfilter of b, i.e. all characters in a are present
* in b in the same order.
*/
export function subFilter(a: string, b: string) {
return subFilterLastIndex(a, b) > -1;
}

/**
* Compares two strings for fuzzy sorting against a query First sort by spread
* of subfilter, then by first index of subfilter, then length, then by
* lexicographic order
*/
export function compareForFuzzySort(query: string, a: string, b: string) {
const distanceToQuery = (s: string) =>
s.length - query.length + s.indexOf(query.charAt(0));
if (distanceToQuery(a) === distanceToQuery(b)) {
return a.localeCompare(b);
const subFilterFirstIndexA = subFilterFirstIndex(query, a);
if (subFilterFirstIndexA === -1) {
return 1;
}
return distanceToQuery(a) - distanceToQuery(b);

const subFilterFirstIndexB = subFilterFirstIndex(query, b);
if (subFilterFirstIndexB === -1) {
return -1;
}

const subFilterLastIndexA = subFilterLastIndex(query, a);
const subFilterLastIndexB = subFilterLastIndex(query, b);
const distanceA = subFilterLastIndexA - subFilterFirstIndexA;
const distanceB = subFilterLastIndexB - subFilterFirstIndexB;
if (distanceA !== distanceB) {
return distanceA - distanceB;
}

if (subFilterFirstIndexA !== subFilterFirstIndexB) {
return subFilterFirstIndexA - subFilterFirstIndexB;
}

if (a.length !== b.length) {
return a.length - b.length;
}
return a.localeCompare(b);
}

export function filterAndSortAgents(
Expand All @@ -167,7 +211,6 @@ export function filterAndSortAgents(
subFilter(lowerCaseSearchText, a.name.toLowerCase())
);

// Sort by position of the subFilter in the name (position of the first character matching).
if (searchText.length > 0) {
filtered.sort((a, b) =>
compareForFuzzySort(lowerCaseSearchText, a.name, b.name)
Expand All @@ -176,3 +219,30 @@ export function filterAndSortAgents(

return filtered;
}

function testCompareForFuzzySort() {
// a is always closer to the query than b
const data = [
{ query: "eng", a: "eng", b: "ContentMarketing" },
{ query: "sql", a: "sqlGod", b: "sqlCoreGod" },
{ query: "sql", a: "sql", b: "sqlGod" },
{ query: "sql", a: "sql", b: "SEOQualityRater" },
{ query: "gp", a: "gpt-4", b: "GabHelp" },
{ query: "gp", a: "gpt-4", b: "gemni-pro" },
{ query: "start", a: "robotstart", b: "strongrt" },
{ query: "mygod", a: "ohmygodbot", b: "moatmode" },
{ query: "test", a: "test", b: "testlong" },
{ query: "test", a: "testlonger", b: "longtest" },
];
console.log(
"Testing compareForFuzzySort, expected first then expected second"
);
for (const d of data) {
console.log(
compareForFuzzySort(d.query, d.a, d.b) < 0 ? "PASS" : "FAIL",
d
);
}
}

testCompareForFuzzySort();
Loading