Skip to content

Build your personal knowledge base with Trilium Notes

small search refactorings

zadamf883fde74ab0
diff --git a/src/becca/becca_service.js b/src/becca/becca_service.js
index 95a8c95ae1..80942c5640 100644
--- a/src/becca/becca_service.js
+++ b/src/becca/becca_service.js
@@ -124,35 +124,43 @@ function getNoteTitleForPath(notePathArray) {
* Archived (and hidden) notes are also returned, but non-archived paths are preferred if available
* - this means that archived paths is returned only if there's no non-archived path
* - you can check whether returned path is archived using isArchived
+ *
+ * @param {BNote} note
+ * @param {string[]} path
*/
function getSomePath(note, path = []) {
// first try to find note within hoisted note, otherwise take any existing note path
- // each branch needs a separate copy since it's mutable
- return getSomePathInner(note, [...path], true)
- || getSomePathInner(note, [...path], false);
+ return getSomePathInner(note, path, true)
+ || getSomePathInner(note, path, false);
}
+/**
+ * @param {BNote} note
+ * @param {string[]} path
+ * @param {boolean}respectHoisting
+ * @returns {string[]|false}
+ */
function getSomePathInner(note, path, respectHoisting) {
if (note.isRoot()) {
- path.push(note.noteId);
- path.reverse();
+ const foundPath = [...path, note.noteId];
+ foundPath.reverse();
- if (respectHoisting && !path.includes(cls.getHoistedNoteId())) {
+ if (respectHoisting && !foundPath.includes(cls.getHoistedNoteId())) {
return false;
}
- return path;
+ return foundPath;
}
const parents = note.parents;
if (parents.length === 0) {
- console.log(`Note ${note.noteId} - "${note.title}" has no parents.`);
+ console.log(`Note '${note.noteId}' - '${note.title}' has no parents.`);
return false;
}
for (const parentNote of parents) {
- const retPath = getSomePathInner(parentNote, path.concat([note.noteId]), respectHoisting);
+ const retPath = getSomePathInner(parentNote, [...path, note.noteId], respectHoisting);
if (retPath) {
return retPath;
diff --git a/src/becca/entities/bnote.js b/src/becca/entities/bnote.js
index 8a752058a0..6ef73b28d5 100644
--- a/src/becca/entities/bnote.js
+++ b/src/becca/entities/bnote.js
@@ -1168,9 +1168,7 @@ class BNote extends AbstractBeccaEntity {
return false;
} else if (parentNote.noteId === '_hidden') {
continue;
- }
-
- if (!parentNote.isHiddenCompletely()) {
+ } else if (!parentNote.isHiddenCompletely()) {
return false;
}
}
diff --git a/src/public/app/entities/fnote.js b/src/public/app/entities/fnote.js
index 7078cecae9..1f44a97bfd 100644
--- a/src/public/app/entities/fnote.js
+++ b/src/public/app/entities/fnote.js
@@ -73,7 +73,7 @@ class FNote {
this.mime = row.mime;
}
- addParent(parentNoteId, branchId) {
+ addParent(parentNoteId, branchId, sort = true) {
if (parentNoteId === 'none') {
return;
}
@@ -83,6 +83,10 @@ class FNote {
}
this.parentToBranch[parentNoteId] = branchId;
+
+ if (sort) {
+ this.sortParents();
+ }
}
addChild(childNoteId, branchId, sort = true) {
@@ -189,7 +193,7 @@ class FNote {
// will sort the parents so that non-search & non-archived are first and archived at the end
// this is done so that non-search & non-archived paths are always explored as first when looking for note path
- resortParents() {
+ sortParents() {
this.parents.sort((aNoteId, bNoteId) => {
const aBranchId = this.parentToBranch[aNoteId];
@@ -197,7 +201,7 @@ class FNote {
return 1;
}
- const aNote = this.froca.getNoteFromCache([aNoteId]);
+ const aNote = this.froca.getNoteFromCache(aNoteId);
if (aNote.isArchived || aNote.isHiddenCompletely()) {
return 1;
diff --git a/src/public/app/services/froca.js b/src/public/app/services/froca.js
index 476700cec0..8bedf2fa60 100644
--- a/src/public/app/services/froca.js
+++ b/src/public/app/services/froca.js
@@ -115,7 +115,7 @@ class Froca {
const childNote = this.notes[branch.noteId];
if (childNote) {
- childNote.addParent(branch.parentNoteId, branch.branchId);
+ childNote.addParent(branch.parentNoteId, branch.branchId, false);
}
const parentNote = this.notes[branch.parentNoteId];
@@ -152,6 +152,7 @@ class Froca {
// sort all of them at once, this avoids repeated sorts (#1480)
for (const noteId of noteIdsToSort) {
this.notes[noteId].sortChildren();
+ this.notes[noteId].sortParents();
}
}
diff --git a/src/public/app/services/tree.js b/src/public/app/services/tree.js
index cc0112d49b..3c4925f3ef 100644
--- a/src/public/app/services/tree.js
+++ b/src/public/app/services/tree.js
@@ -58,7 +58,7 @@ async function resolveNotePathToSegments(notePath, hoistedNoteId = 'root', logEr
return;
}
- child.resortParents();
+ child.sortParents();
const parents = child.getParentNotes();
diff --git a/src/services/search/services/search.js b/src/services/search/services/search.js
index 9ca8386ba4..06362b28cc 100644
--- a/src/services/search/services/search.js
+++ b/src/services/search/services/search.js
@@ -155,11 +155,8 @@ function findResultsWithExpression(expression, searchContext) {
const noteSet = expression.execute(allNoteSet, executionContext, searchContext);
const searchResults = noteSet.notes
+ .filter(note => !note.isDeleted)
.map(note => {
- if (note.isDeleted) {
- return null;
- }
-
const notePathArray = executionContext.noteIdToNotePath[note.noteId] || beccaService.getSomePath(note);
if (!notePathArray) {
@@ -167,8 +164,7 @@ function findResultsWithExpression(expression, searchContext) {
}
return new SearchResult(notePathArray);
- })
- .filter(note => !!note);
+ });
for (const res of searchResults) {
res.computeScore(searchContext.fulltextQuery, searchContext.highlightedTokens);