Files
notesnook/packages/core/src/api/lookup.ts

105 lines
3.1 KiB
TypeScript
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
2023-01-16 13:44:52 +05:00
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-30 16:13:11 +05:00
2023-10-07 13:26:03 +05:00
import uFuzzy from "@leeoniya/ufuzzy";
import Database from ".";
import {
Attachment,
2023-08-21 16:24:05 +05:00
GroupedItems,
Note,
Notebook,
Reminder,
Tag,
2023-10-07 13:26:03 +05:00
TrashItem
} from "../types";
2023-10-12 10:29:11 +05:00
import { DatabaseSchemaWithFTS, isFalse } from "../database";
import { Kysely, sql } from "kysely";
2020-03-09 12:39:49 +05:00
export default class Lookup {
constructor(private readonly db: Database) {}
2020-03-19 12:38:33 +05:00
2023-10-07 13:26:03 +05:00
async notes(
query: string,
ids?: string[]
): Promise<Note & { rank: number }[]> {
2023-10-12 10:29:11 +05:00
const db = this.db.sql() as Kysely<DatabaseSchemaWithFTS>;
return (await db
2023-10-07 13:26:03 +05:00
.with("matching", (eb) =>
eb
.selectFrom("content_fts")
.where("data", "match", query)
.select(["noteId as id", "rank"])
.unionAll(
eb
.selectFrom("notes_fts")
.where("title", "match", query)
// add 10 weight to title
.select(["id", sql.raw<number>(`rank * 10`).as("rank")])
)
)
2023-10-07 13:26:03 +05:00
.selectFrom("notes")
.$if(!!ids && ids.length > 0, (eb) => eb.where("id", "in", ids!))
.where(isFalse("notes.deleted"))
.where(isFalse("notes.dateDeleted"))
.innerJoin("matching", (eb) => eb.onRef("notes.id", "==", "matching.id"))
.orderBy("matching.rank")
.selectAll()
.execute()) as unknown as Note & { rank: number }[];
2020-03-09 12:39:49 +05:00
}
notebooks(array: Notebook[], query: string) {
return search(array, query, (n) => `${n.title} ${n.description}}`);
2020-03-09 12:39:49 +05:00
}
2023-08-21 16:24:05 +05:00
tags(array: GroupedItems<Tag>, query: string) {
return this.byTitle(array, query);
2020-03-09 12:39:49 +05:00
}
reminders(array: Reminder[], query: string) {
2022-11-17 15:41:20 +05:00
return search(array, query, (n) => `${n.title} ${n.description || ""}`);
}
trash(array: TrashItem[], query: string) {
return this.byTitle(array, query);
2020-03-09 12:39:49 +05:00
}
attachments(array: Attachment[], query: string) {
2023-10-07 13:26:03 +05:00
return search(array, query, (n) => `${n.filename} ${n.mimeType} ${n.hash}`);
2022-02-28 13:02:16 +05:00
}
private byTitle<T extends { title: string }>(array: T[], query: string) {
return search(array, query, (n) => n.title);
2021-12-21 11:24:45 +05:00
}
}
2023-10-07 13:26:03 +05:00
const uf = new uFuzzy();
function search<T>(items: T[], query: string, selector: (item: T) => string) {
2021-12-21 11:24:45 +05:00
try {
2023-10-07 13:26:03 +05:00
const [_idxs, _info, order] = uf.search(items.map(selector), query, true);
if (!order) return [];
const filtered: T[] = [];
for (const i of order) {
filtered.push(items[i]);
}
return filtered;
2021-12-21 11:24:45 +05:00
} catch (e) {
return [];
2020-03-09 12:39:49 +05:00
}
}