mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-22 22:49:45 +01:00
Revert "feat: add hypersearch for searching"
This reverts commit 5362e54428.
This commit is contained in:
@@ -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);
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
iconv-lite@0.4.24:
|
||||
version "0.4.24"
|
||||
|
||||
Reference in New Issue
Block a user