small search refactorings

This commit is contained in:
zadam 2023-02-28 23:23:17 +01:00
parent ee2953a5e1
commit f883fde74a
6 changed files with 30 additions and 23 deletions

View file

@ -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;

View file

@ -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;
}
}

View file

@ -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;

View file

@ -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();
}
}

View file

@ -58,7 +58,7 @@ async function resolveNotePathToSegments(notePath, hoistedNoteId = 'root', logEr
return;
}
child.resortParents();
child.sortParents();
const parents = child.getParentNotes();

View file

@ -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);