Skip to content

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

core: fix notes search for queries less than 3 letters (#6489)

Abdullah Atta15329597a574
diff --git a/packages/core/src/api/lookup.ts b/packages/core/src/api/lookup.ts
index 9357d5d32..f7aea041d 100644
--- a/packages/core/src/api/lookup.ts
+++ b/packages/core/src/api/lookup.ts
@@ -43,10 +43,49 @@ export default class Lookup {
notes(query: string, notes?: FilteredSelector<Note>): SearchResults<Note> {
return this.toSearchResults(async (limit) => {
- if (query.length < 3) return [];
-
const db = this.db.sql() as unknown as Kysely<RawDatabaseSchema>;
const excludedIds = this.db.trash.cache.notes;
+
+ if (query.length <= 2) {
+ const results = await db
+ .selectFrom((eb) =>
+ eb
+ .selectFrom("notes")
+ .$if(!!notes, (eb) =>
+ eb.where("id", "in", notes!.filter.select("id"))
+ )
+ .$if(excludedIds.length > 0, (eb) =>
+ eb.where("id", "not in", excludedIds)
+ )
+ .where("title", "like", `%${query}%`)
+ .select(["id"])
+ .unionAll((eb) =>
+ eb
+ .selectFrom("content")
+ .$if(!!notes, (eb) =>
+ eb.where("id", "in", notes!.filter.select("id"))
+ )
+ .$if(excludedIds.length > 0, (eb) =>
+ eb.where("id", "not in", excludedIds)
+ )
+ .where("locked", "!=", true)
+ .where("data", "like", `%${query}%`)
+ .select(["noteId as id"])
+ .$castTo<{ id: string; dateCreated: number }>()
+ )
+ .as("results")
+ )
+ .select(["results.id"])
+ .groupBy("results.id")
+ .$if(!!limit, (eb) => eb.limit(limit!))
+ .execute()
+ .catch((e) => {
+ logger.error(e, `Error while searching`, { query });
+ return [];
+ });
+ return results.map((r) => r.id);
+ }
+
const results = await db
.selectFrom((eb) =>
eb