From 3cb368c4deb0ad53482f6cba022667b6e41e6e7b Mon Sep 17 00:00:00 2001 From: zadam Date: Mon, 7 Nov 2022 23:56:53 +0100 Subject: [PATCH] search link map will display only direct results --- src/becca/entities/note.js | 34 ++++++++++++++++++++++++---------- src/routes/api/note_map.js | 17 +++++++++++++---- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/becca/entities/note.js b/src/becca/entities/note.js index 1f3d208be..abaf9f60a 100644 --- a/src/becca/entities/note.js +++ b/src/becca/entities/note.js @@ -11,6 +11,7 @@ const NoteRevision = require("./note_revision"); const TaskContext = require("../../services/task_context"); const dayjs = require("dayjs"); const utc = require('dayjs/plugin/utc'); +const searchService = require("../../services/search/services/search.js"); dayjs.extend(utc) const LABEL = 'label'; @@ -839,6 +840,27 @@ class Note extends AbstractEntity { return Array.from(set); } + /** @return {Note[]} */ + getSearchResultNotes() { + if (this.type !== 'search') { + return []; + } + + try { + const searchService = require("../../services/search/services/search"); + const {searchResultNoteIds} = searchService.searchFromNote(this); + + const becca = this.becca; + return searchResultNoteIds + .map(resultNoteId => becca.notes[resultNoteId]) + .filter(note => !!note); + } + catch (e) { + log.error(`Could not resolve search note ${this.noteId}: ${e.message}`); + return []; + } + } + /** * @returns {{notes: Note[], relationships: Array.<{parentNoteId: string, childNoteId: string}>}} */ @@ -848,16 +870,8 @@ class Note extends AbstractEntity { function resolveSearchNote(searchNote) { try { - const searchService = require("../../services/search/services/search"); - const becca = searchNote.becca; - const {searchResultNoteIds} = searchService.searchFromNote(searchNote); - - for (const resultNoteId of searchResultNoteIds) { - const resultNote = becca.notes[resultNoteId]; - - if (resultNote) { - addSubtreeNotesInner(resultNote, searchNote); - } + for (const resultNote of searchNote.getSearchResultNotes()) { + addSubtreeNotesInner(resultNote, searchNote); } } catch (e) { diff --git a/src/routes/api/note_map.js b/src/routes/api/note_map.js index 9b49c8d3c..2851d9589 100644 --- a/src/routes/api/note_map.js +++ b/src/routes/api/note_map.js @@ -37,7 +37,7 @@ function getNeighbors(note, depth) { const retNoteIds = []; function isIgnoredRelation(relation) { - return ['relationMapLink', 'template', 'image'].includes(relation.name); + return ['relationMapLink', 'template', 'image', 'ancestor'].includes(relation.name); } // forward links @@ -86,10 +86,17 @@ function getLinkMap(req) { // if the map root itself has exclude attribute (journal typically) then there wouldn't be anything to display, so // we'll just ignore it const ignoreExcludeFromNoteMap = mapRootNote.hasLabel('excludeFromNoteMap'); - const subtree = mapRootNote.getSubtree({includeArchived: false, resolveSearch: true}); + let unfilteredNotes; + + if (mapRootNote.type === 'search') { + // for search notes we want to consider the direct search results only without the descendants + unfilteredNotes = mapRootNote.getSearchResultNotes(); + } else { + unfilteredNotes = mapRootNote.getSubtree({includeArchived: false, resolveSearch: true}).notes; + } const noteIds = new Set( - subtree.notes + unfilteredNotes .filter(note => ignoreExcludeFromNoteMap || !note.hasLabel('excludeFromNoteMap')) .map(note => note.noteId) ); @@ -102,6 +109,8 @@ function getLinkMap(req) { noteIds.add(noteId); } + console.log(noteIds); + const notes = Array.from(noteIds).map(noteId => { const note = becca.getNote(noteId); @@ -128,7 +137,7 @@ function getLinkMap(req) { return true; } }) - .map(rel => ({ + .map(rel => ({ id: rel.noteId + "-" + rel.name + "-" + rel.value, sourceNoteId: rel.noteId, targetNoteId: rel.value,