Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: fix quadratic merge of search matches (#10527)

Abdullah Attacc151fd1f9ac
diff --git a/packages/core/__tests__/lookup.test.js b/packages/core/__tests__/lookup.test.js
index e5b8867c5..acbf5f4fb 100644
--- a/packages/core/__tests__/lookup.test.js
+++ b/packages/core/__tests__/lookup.test.js
@@ -200,6 +200,32 @@ describe("notesWithHighlighting", () => {
expect(item.item.content[0][0].suffix.includes(" ")).toBe(false);
}));
+ test("search should return each matching note once with many matches", () =>
+ databaseTest().then(async (db) => {
+ const ids = [];
+ for (let i = 0; i < 2000; ++i)
+ ids.push(
+ await db.notes.add({
+ title: `cafe ${i}`,
+ content: { type: "tiptap", data: "<p>cafe</p>" }
+ })
+ );
+
+ for (const sortOptions of [
+ undefined,
+ { sortBy: "dateCreated", sortDirection: "asc" }
+ ]) {
+ const result = await db.lookup.notesWithHighlighting(
+ "cafe",
+ db.notes.all,
+ sortOptions
+ );
+ const found = await result.ids();
+ expect(found).toHaveLength(ids.length);
+ expect(new Set(found).size).toBe(ids.length);
+ }
+ }), 30000);
+
test("search notes with parentheses in query should load the item", () =>
noteTest({
title: "(with parantheses)"
diff --git a/packages/core/src/api/lookup.ts b/packages/core/src/api/lookup.ts
index 5f6981be0..29ee7f842 100644
--- a/packages/core/src/api/lookup.ts
+++ b/packages/core/src/api/lookup.ts
@@ -242,23 +242,17 @@ export default class Lookup {
values: { id: string; types: string[]; rank: number }[];
};
let matches: Matches = { ids: [], values: [] };
+ const matchesById = new Map<string, Matches["values"][number]>();
for (const { id, type, rank } of ftsResults) {
- const index = matches.ids.indexOf(id);
- const match =
- index === -1
- ? {
- id,
- types: [],
- rank: 0
- }
- : matches.values[index];
- match.types.push(type);
- match.rank += rank || 0;
-
- if (index === -1) {
+ let match = matchesById.get(id);
+ if (!match) {
+ match = { id, types: [], rank: 0 };
+ matchesById.set(id, match);
matches.ids.push(id);
matches.values.push(match);
}
+ match.types.push(type);
+ match.rank += rank || 0;
}
if (!sortOptions || sortOptions.sortBy === "relevance") {
@@ -274,9 +268,9 @@ export default class Lookup {
.items(matches.ids, sortOptions);
const sorted: Matches = { ids: [], values: [] };
for (const { id } of sortedNoteIds) {
- const index = matches.ids.indexOf(id);
- if (index === -1) continue;
- sorted.values.push(matches.values[index]);
+ const match = matchesById.get(id);
+ if (!match) continue;
+ sorted.values.push(match);
sorted.ids.push(id);
}
matches = sorted;