Skip to content

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

fix: notes searching

thecodrr949ca5693677
diff --git a/packages/core/__tests__/lookup.test.js b/packages/core/__tests__/lookup.test.js
index f5755ade4..8739c25f4 100644
--- a/packages/core/__tests__/lookup.test.js
+++ b/packages/core/__tests__/lookup.test.js
@@ -15,12 +15,12 @@ beforeEach(async () => {
});
//TODO
-test.skip("search notes", () =>
+test("search notes", () =>
noteTest({
content: { delta: "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);
}));
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index 8ce26caeb..36539f136 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -37,7 +37,7 @@ class Database {
await this.trash.init(this.notes, this.notebooks, this.delta);
this.syncer = new Sync(this);
this.vault = new Vault(this, this.context);
- this.lookup = new Lookup();
+ this.lookup = new Lookup(this);
}
sync() {
diff --git a/packages/core/api/lookup.js b/packages/core/api/lookup.js
index 0bedf0724..85e926db8 100644
--- a/packages/core/api/lookup.js
+++ b/packages/core/api/lookup.js
@@ -1,14 +1,33 @@
import fuzzysearch from "fuzzysearch";
+import Database from "./index";
+import set from "../utils/set";
var tfun = require("transfun/transfun.js").tfun;
if (!tfun) {
tfun = global.tfun;
}
export default class Lookup {
- notes(array, query) {
- return tfun.filter(note =>
- fuzzysearch(query, note.title + " " + note.content.text)
- )(array);
+ /**
+ *
+ * @param {Database} db
+ */
+ constructor(db) {
+ this._db = db;
+ }
+
+ async notes(array, query) {
+ const textIds = array.map(v => v.content.text);
+ const textArray = await this._db.text.multi(textIds);
+ const filteredText = tfun.filter(text => fuzzysearch(query, text.data))(
+ textArray
+ );
+ const filteredByText = filteredText.map(text =>
+ array.find(note => note.id === text.noteId)
+ );
+ const filteredByTitle = tfun.filter(note => fuzzysearch(query, note.title))(
+ array
+ );
+ return set.union(filteredByText, filteredByTitle);
}
notebooks(array, query) {
diff --git a/packages/core/collections/content.js b/packages/core/collections/content.js
index e86cb1826..9a1910f5c 100644
--- a/packages/core/collections/content.js
+++ b/packages/core/collections/content.js
@@ -13,18 +13,32 @@ export default class Content {
async add(content) {
if (!content) return;
const id = content.id || getId();
- await this._collection.addItem({ id, data: content.data || content });
+ await this._collection.addItem({
+ noteId: content.noteId,
+ id,
+ data: content.data || content
+ });
return id;
}
async get(id) {
- const content = await this._collection.getItem(id);
+ const content = await this.raw(id);
if (!content) return;
return content.data;
}
+ async raw(id) {
+ const content = await this._collection.getItem(id);
+ if (!content) return;
+ return content;
+ }
+
remove(id) {
if (!id) return;
return this._collection.removeItem(id);
}
+
+ multi(ids) {
+ return this._collection.getItems(ids);
+ }
}
diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.js
index 490c3bc6e..f439652ca 100644
--- a/packages/core/collections/notes.js
+++ b/packages/core/collections/notes.js
@@ -71,6 +71,7 @@ export default class Notes {
if (note.content.delta && note.content.delta.ops) {
deltaId = await this._deltaCollection.add({
+ noteId: id,
id: deltaId,
data: note.content.delta
});
@@ -78,6 +79,7 @@ export default class Notes {
if (note.content.text !== textId) {
textId = await this._textCollection.add({
+ noteId: id,
id: textId,
data: note.content.text
});
diff --git a/packages/core/database/indexed-collection.js b/packages/core/database/indexed-collection.js
index 9435bd48c..30f46f8b1 100644
--- a/packages/core/database/indexed-collection.js
+++ b/packages/core/database/indexed-collection.js
@@ -41,4 +41,12 @@ export default class IndexedCollection {
getItem(id) {
return this.indexer.read(id);
}
+
+ async getItems(indices) {
+ const data = await this.indexer.readMulti(indices);
+ return data.reduce((total, current) => {
+ total.push(current[1]);
+ return total;
+ }, []);
+ }
}