Skip to content

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

feat: add hypersearch for searching

thecodrr5362e5442846
diff --git a/packages/core/__tests__/lookup.skip.js b/packages/core/__tests__/lookup.test.js
similarity index 78%
rename from packages/core/__tests__/lookup.skip.js
rename to packages/core/__tests__/lookup.test.js
index 4a27f49ff..ba24f1c74 100644
--- a/packages/core/__tests__/lookup.skip.js
+++ b/packages/core/__tests__/lookup.test.js
@@ -17,31 +17,31 @@ beforeEach(async () => {
//TODO
test("search notes", () =>
noteTest({
- content: { delta: [{ insert: "5" }], text: "5" },
+ content: { type: "delta", data: [{ insert: "5" }], text: "5" },
}).then(async ({ db }) => {
await db.notes.add(TEST_NOTE);
- let filtered = await db.lookup.notes(db.notes.all, "5");
+ let filtered = db.lookup.notes(db.notes.all, "5");
expect(filtered.length).toBe(1);
}));
test("search notes with a locked note", () =>
noteTest({
- content: { delta: [{ insert: "5" }], text: "5" },
+ content: { type: "delta", data: [{ insert: "5" }], text: "5" },
}).then(async ({ db }) => {
const noteId = await db.notes.add(TEST_NOTE);
await db.vault.create("password");
await db.vault.add(noteId);
- let filtered = await db.lookup.notes(db.notes.all, "I am a");
+ let filtered = db.lookup.notes(db.notes.all, "Thi");
expect(filtered.length).toBe(1);
}));
test("search notes with an empty note", () =>
noteTest({
- content: { delta: [{ insert: "5" }], text: "5" },
+ content: { type: "delta", data: [{ insert: "5" }], text: "5" },
}).then(async ({ db }) => {
await db.notes.add({
title: "hello world",
- content: { delta: [], text: "" },
+ content: { type: "delta", data: [{ insert: "\n" }] },
});
let filtered = await db.lookup.notes(db.notes.all, "hello world");
expect(filtered.length).toBe(1);
diff --git a/packages/core/api/lookup.js b/packages/core/api/lookup.js
index 5a4e509e9..3918439b7 100644
--- a/packages/core/api/lookup.js
+++ b/packages/core/api/lookup.js
@@ -14,27 +14,22 @@ export default class Lookup {
}
notes(notes, query) {
- return new Promise((resolve) => {
- const results = [];
- let index = 0,
- max = notes.length;
- notes.forEach(async (note) => {
- const text =
- note.locked || !note.content
- ? ""
- : await this._db.text.get(note.content.text);
- const title = note.title;
- if (fzs(query, text + title)) results.push(note);
- if (++index >= max) return resolve(results);
- });
+ let contentIds = this._db.content._collection.search.searchDocs(query);
+ let noteIds = this._db.notes._collection.search.searchDocs(query);
+ return notes.filter((note) => {
+ return (
+ contentIds.findIndex((content) => note.id === content.noteId) > -1 ||
+ noteIds.findIndex((n) => n.id === note.id) > -1
+ );
});
}
notebooks(array, query) {
+ const notebooksIds = this._db.notebooks._collection.search.searchDocs(
+ query
+ );
return tfun.filter(
- (nb) =>
- fzs(query, nb.title + " " + nb.description) ||
- nb.topics.some((topic) => fuzzysearch(query, topic.title))
+ (nb) => notebooksIds.findIndex((notebook) => notebook.id === nb.id) > -1
)(array);
}
diff --git a/packages/core/api/sync/__tests__/collector.test.js b/packages/core/api/sync/__tests__/collector.test.js
index d6f1fa9d7..bfb669d57 100644
--- a/packages/core/api/sync/__tests__/collector.test.js
+++ b/packages/core/api/sync/__tests__/collector.test.js
@@ -51,7 +51,10 @@ const tests = [
type: "note",
title: "someTitle",
}),
- getMainCollectionParams("content", { ops: [{ insert: "true" }] }),
+ getMainCollectionParams("content", {
+ data: [{ insert: "true" }],
+ type: "delta",
+ }),
];
describe.each(tests)("%s preparation", (collection, add, addExtra) => {
diff --git a/packages/core/database/cached-collection.js b/packages/core/database/cached-collection.js
index 0f026790b..74d4ef854 100644
--- a/packages/core/database/cached-collection.js
+++ b/packages/core/database/cached-collection.js
@@ -19,8 +19,8 @@ export default class CachedCollection extends IndexedCollection {
this.map.clear();
}
- async updateItem(item) {
- await super.updateItem(item);
+ async updateItem(item, index = true) {
+ await super.updateItem(item, index);
this.map.set(item.id, item);
EV.publish("db:write", item);
}
diff --git a/packages/core/database/indexed-collection.js b/packages/core/database/indexed-collection.js
index 7a77c198c..93d253993 100644
--- a/packages/core/database/indexed-collection.js
+++ b/packages/core/database/indexed-collection.js
@@ -1,8 +1,15 @@
import Indexer from "./indexer";
+import HyperSearch from "hypersearch";
+import { getSchema } from "./schemas";
export default class IndexedCollection {
constructor(context, type) {
this.indexer = new Indexer(context, type);
+ this.type = type;
+ this.search = new HyperSearch({
+ schema: getSchema(type),
+ tokenizer: "forward",
+ });
}
clear() {
@@ -11,11 +18,11 @@ export default class IndexedCollection {
async init() {
await this.indexer.init();
+ const index = await this.indexer.read(`${this.type}-index`);
+ if (index) this.search.import(index);
}
async addItem(item) {
- if (!item.id) throw new Error("The item must contain the id field.");
-
const exists = await this.exists(item.id);
if (!exists) item.dateCreated = item.dateCreated || Date.now();
await this.updateItem(item);
@@ -24,22 +31,32 @@ export default class IndexedCollection {
}
}
- async updateItem(item) {
+ async updateItem(item, index = true) {
if (!item.id) throw new Error("The item must contain the id field.");
// if item is newly synced, remote will be true.
item.dateEdited = item.remote ? item.dateEdited : Date.now();
// the item has become local now, so remove the flag.
delete item.remote;
await this.indexer.write(item.id, item);
+
+ if (index && (this.type === "notes" || this.type === "notebooks")) {
+ this.search.addDoc(item);
+ this.indexer.write(`${this.type}-index`, this.search.export());
+ }
}
- removeItem(id) {
- return this.updateItem({
- id,
- deleted: true,
- dateCreated: Date.now(),
- dateEdited: Date.now(),
- });
+ async removeItem(id) {
+ await this.updateItem(
+ {
+ id,
+ deleted: true,
+ dateCreated: Date.now(),
+ dateEdited: Date.now(),
+ },
+ false
+ );
+ if (this.type === "notes" || this.type === "notebooks")
+ this.search.remove(id);
}
exists(id) {
diff --git a/packages/core/database/schemas.js b/packages/core/database/schemas.js
new file mode 100644
index 000000000..4a69617e5
--- /dev/null
+++ b/packages/core/database/schemas.js
@@ -0,0 +1,42 @@
+const { getContentFromData } = require("../contenttypes");
+
+const schemas = {
+ content: {
+ id: { asId: true, store: false },
+ noteId: { store: true, index: false },
+ data: {
+ resolve: (doc) => {
+ if (doc.data.iv) return "";
+ const content = getContentFromData(doc.type, doc.data);
+ return content._text;
+ },
+ store: false,
+ index: true,
+ },
+ },
+ notes: {
+ id: { asId: true, store: false },
+ title: true,
+ // pinned: {
+ // resolve: (doc) => (doc.pinned ? "pinned:true" : "pinned:false"),
+ // index: true,
+ // tokenizer: "strict",
+ // splitter: "-",
+ // },
+ },
+ notebooks: {
+ id: { asId: true, store: false },
+ title: true,
+ description: true,
+ topics: {
+ resolve: (doc) => {
+ if (!doc.topics) return "";
+ return doc.topics.map((v) => v.title || v).join(" ");
+ },
+ },
+ },
+};
+
+export function getSchema(type) {
+ return schemas[type];
+}
diff --git a/packages/core/yarn.lock b/packages/core/yarn.lock
index fb40e8cf9..c21487b4a 100644
--- a/packages/core/yarn.lock
+++ b/packages/core/yarn.lock
@@ -2704,8 +2704,8 @@ http-signature@~1.2.0:
sshpk "^1.7.0"
"hypersearch@https://github.com/streetwriters/hypersearch":
- version "0.0.1"
- resolved "https://github.com/streetwriters/hypersearch#bbeac8ad63c3f70e94dfd05b0f00f44ecc989a19"
+ version "0.0.4"
+ resolved "https://github.com/streetwriters/hypersearch#9cad0f7c2d93f19bf06d7fa6602357b89cdba38d"
version "0.4.24"