Skip to content

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

Revert "feat: add hypersearch for searching"

thecodrrcaaa8081740a
diff --git a/packages/core/__tests__/lookup.test.js b/packages/core/__tests__/lookup.skip.js
similarity index 78%
rename from packages/core/__tests__/lookup.test.js
rename to packages/core/__tests__/lookup.skip.js
index ba24f1c74..4a27f49ff 100644
--- a/packages/core/__tests__/lookup.test.js
+++ b/packages/core/__tests__/lookup.skip.js
@@ -17,31 +17,31 @@ beforeEach(async () => {
//TODO
test("search notes", () =>
noteTest({
- content: { type: "delta", data: [{ insert: "5" }], text: "5" },
+ content: { delta: [{ insert: "5" }], text: "5" },
}).then(async ({ db }) => {
await db.notes.add(TEST_NOTE);
- let filtered = db.lookup.notes(db.notes.all, "5");
+ let filtered = await db.lookup.notes(db.notes.all, "5");
expect(filtered.length).toBe(1);
}));
test("search notes with a locked note", () =>
noteTest({
- content: { type: "delta", data: [{ insert: "5" }], text: "5" },
+ content: { delta: [{ 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 = db.lookup.notes(db.notes.all, "Thi");
+ let filtered = await db.lookup.notes(db.notes.all, "I am a");
expect(filtered.length).toBe(1);
}));
test("search notes with an empty note", () =>
noteTest({
- content: { type: "delta", data: [{ insert: "5" }], text: "5" },
+ content: { delta: [{ insert: "5" }], text: "5" },
}).then(async ({ db }) => {
await db.notes.add({
title: "hello world",
- content: { type: "delta", data: [{ insert: "\n" }] },
+ content: { delta: [], text: "" },
});
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 3918439b7..5a4e509e9 100644
--- a/packages/core/api/lookup.js
+++ b/packages/core/api/lookup.js
@@ -14,22 +14,27 @@ export default class Lookup {
}
notes(notes, query) {
- 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
- );
+ 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);
+ });
});
}
notebooks(array, query) {
- const notebooksIds = this._db.notebooks._collection.search.searchDocs(
- query
- );
return tfun.filter(
- (nb) => notebooksIds.findIndex((notebook) => notebook.id === nb.id) > -1
+ (nb) =>
+ fzs(query, nb.title + " " + nb.description) ||
+ nb.topics.some((topic) => fuzzysearch(query, topic.title))
)(array);
}
diff --git a/packages/core/api/sync/__tests__/collector.test.js b/packages/core/api/sync/__tests__/collector.test.js
index bfb669d57..d6f1fa9d7 100644
--- a/packages/core/api/sync/__tests__/collector.test.js
+++ b/packages/core/api/sync/__tests__/collector.test.js
@@ -51,10 +51,7 @@ const tests = [
type: "note",
title: "someTitle",
}),
- getMainCollectionParams("content", {
- data: [{ insert: "true" }],
- type: "delta",
- }),
+ getMainCollectionParams("content", { ops: [{ insert: "true" }] }),
];
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 74d4ef854..0f026790b 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, index = true) {
- await super.updateItem(item, index);
+ async updateItem(item) {
+ await super.updateItem(item);
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 93d253993..7a77c198c 100644
--- a/packages/core/database/indexed-collection.js
+++ b/packages/core/database/indexed-collection.js
@@ -1,15 +1,8 @@
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() {
@@ -18,11 +11,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);
@@ -31,32 +24,22 @@ export default class IndexedCollection {
}
}
- async updateItem(item, index = true) {
+ async updateItem(item) {
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());
- }
}
- 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);
+ removeItem(id) {
+ return this.updateItem({
+ id,
+ deleted: true,
+ dateCreated: Date.now(),
+ dateEdited: Date.now(),
+ });
}
exists(id) {
diff --git a/packages/core/database/schemas.js b/packages/core/database/schemas.js
deleted file mode 100644
index 4a69617e5..000000000
--- a/packages/core/database/schemas.js
+++ /dev/null
@@ -1,42 +0,0 @@
-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 c21487b4a..fb40e8cf9 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.4"
- resolved "https://github.com/streetwriters/hypersearch#9cad0f7c2d93f19bf06d7fa6602357b89cdba38d"
+ version "0.0.1"
+ resolved "https://github.com/streetwriters/hypersearch#bbeac8ad63c3f70e94dfd05b0f00f44ecc989a19"
version "0.4.24"