From 03428e58484887bd6943043e97453baba0ea53a1 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Wed, 1 May 2024 09:16:14 +0500 Subject: [PATCH] core: allow full FTS5 search syntax --- packages/core/src/api/lookup.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/core/src/api/lookup.ts b/packages/core/src/api/lookup.ts index 1ba0c2629..d312ebd75 100644 --- a/packages/core/src/api/lookup.ts +++ b/packages/core/src/api/lookup.ts @@ -24,6 +24,7 @@ import { DatabaseSchema, RawDatabaseSchema } from "../database"; import { AnyColumnWithTable, Kysely, sql } from "kysely"; import { FilteredSelector } from "../database/sql-collection"; import { VirtualizedGrouping } from "../utils/virtualized-grouping"; +import { logger } from "../logger"; type SearchResults = { sorted: (limit?: number) => Promise>; @@ -44,8 +45,6 @@ export default class Lookup { if (query.length < 3) return []; const db = this.db.sql() as unknown as Kysely; - query = query.replace(/"/, '""'); - const excludedIds = this.db.trash.cache.notes; const results = await db .selectFrom((eb) => @@ -57,7 +56,7 @@ export default class Lookup { .$if(excludedIds.length > 0, (eb) => eb.where("id", "not in", excludedIds) ) - .where("title", "match", `"${query}"`) + .where("title", "match", query) .select(["id", sql`rank * 10`.as("rank")]) .unionAll((eb) => eb @@ -68,7 +67,7 @@ export default class Lookup { .$if(excludedIds.length > 0, (eb) => eb.where("id", "not in", excludedIds) ) - .where("data", "match", `"${query}"`) + .where("data", "match", query) .select(["noteId as id", "rank"]) .$castTo<{ id: string; rank: number }>() ) @@ -85,7 +84,11 @@ export default class Lookup { "in", (notes || this.db.notes.all).filter.select("id") ) - .execute(); + .execute() + .catch((e) => { + logger.error(e, `Error while searching`, { query }); + return []; + }); return results.map((r) => r.id); }, notes || this.db.notes.all); }