From 69e36d26779803a91276ebd15525987ff19fbe5f Mon Sep 17 00:00:00 2001 From: zadam Date: Mon, 7 Sep 2020 00:05:01 +0200 Subject: [PATCH] refactoring for archived notes handling in note cache --- ..._rename_hideInAutocomplete_to_archived.sql | 1 + src/routes/api/clipper.js | 4 +-- src/services/app_info.js | 2 +- src/services/note_cache/entities/note.js | 25 ++++++++----------- src/services/note_cache/note_cache_service.js | 4 +-- src/services/search/parsing_context.js | 1 + src/services/search/services/parse.js | 12 +++------ src/services/search/services/search.js | 2 ++ 8 files changed, 22 insertions(+), 29 deletions(-) create mode 100644 db/migrations/0168__rename_hideInAutocomplete_to_archived.sql diff --git a/db/migrations/0168__rename_hideInAutocomplete_to_archived.sql b/db/migrations/0168__rename_hideInAutocomplete_to_archived.sql new file mode 100644 index 000000000..29b72138c --- /dev/null +++ b/db/migrations/0168__rename_hideInAutocomplete_to_archived.sql @@ -0,0 +1 @@ +UPDATE attributes SET name = 'archived' WHERE name = 'hideInAutocomplete'; diff --git a/src/routes/api/clipper.js b/src/routes/api/clipper.js index b0d8e1df2..a58d03ce9 100644 --- a/src/routes/api/clipper.js +++ b/src/routes/api/clipper.js @@ -113,8 +113,8 @@ function addImagesToNote(images, note, content) { new Attribute({ noteId: imageNote.noteId, type: 'label', - name: 'hideInAutocomplete' - }).save(); + name: 'archived' + }).save(); // so that these image notes don't show up in search / autocomplete new Attribute({ noteId: note.noteId, diff --git a/src/services/app_info.js b/src/services/app_info.js index 0771d2600..89a95ed9e 100644 --- a/src/services/app_info.js +++ b/src/services/app_info.js @@ -4,7 +4,7 @@ const build = require('./build'); const packageJson = require('../../package'); const {TRILIUM_DATA_DIR} = require('./data_dir'); -const APP_DB_VERSION = 167; +const APP_DB_VERSION = 168; const SYNC_VERSION = 16; const CLIPPER_PROTOCOL_VERSION = "1.0"; diff --git a/src/services/note_cache/entities/note.js b/src/services/note_cache/entities/note.js index 9f8c5b483..7c39dd4ff 100644 --- a/src/services/note_cache/entities/note.js +++ b/src/services/note_cache/entities/note.js @@ -138,12 +138,6 @@ class Note { return this.hasAttribute('label', 'archived'); } - get isHideInAutocompleteOrArchived() { - return this.attributes.find(attr => - attr.type === 'label' - && ["archived", "hideInAutocomplete"].includes(attr.name)); - } - get hasInheritableOwnedArchivedLabel() { return !!this.ownedAttributes.find(attr => attr.type === 'label' && attr.name === 'archived' && attr.isInheritable); } @@ -155,20 +149,19 @@ class Note { } /** - * @return {string} - returns flattened textual representation of note, prefixes and attributes usable for searching + * This is used for: + * - fast searching + * - note similarity evaluation + * + * @return {string} - returns flattened textual representation of note, prefixes and attributes */ get flatText() { if (!this.flatTextCache) { - if (this.isHideInAutocompleteOrArchived) { - this.flatTextCache = " "; // can't be empty - return this.flatTextCache; - } - - this.flatTextCache = this.noteId + ' ' + this.type + ' ' + this.mime; + this.flatTextCache = this.noteId + ' ' + this.type + ' ' + this.mime + ' '; for (const branch of this.parentBranches) { if (branch.prefix) { - this.flatTextCache += branch.prefix + ' - '; + this.flatTextCache += branch.prefix + ' '; } } @@ -176,11 +169,13 @@ class Note { for (const attr of this.attributes) { // it's best to use space as separator since spaces are filtered from the search string by the tokenization into words - this.flatTextCache += ' ' + (attr.type === 'label' ? '#' : '@') + attr.name; + this.flatTextCache += (attr.type === 'label' ? '#' : '~') + attr.name; if (attr.value) { this.flatTextCache += '=' + attr.value; } + + this.flatTextCache += ' '; } this.flatTextCache = this.flatTextCache.toLowerCase(); diff --git a/src/services/note_cache/note_cache_service.js b/src/services/note_cache/note_cache_service.js index 234d6ed17..a586a8bc8 100644 --- a/src/services/note_cache/note_cache_service.js +++ b/src/services/note_cache/note_cache_service.js @@ -177,7 +177,7 @@ function getNotePath(noteId) { function evaluateSimilarity(sourceNote, candidateNote, results) { let coeff = stringSimilarity.compareTwoStrings(sourceNote.flatText, candidateNote.flatText); - if (coeff > 0.4) { + if (coeff > 0.5) { const notePath = getSomePath(candidateNote); // this takes care of note hoisting @@ -214,7 +214,7 @@ function findSimilarNotes(noteId) { } for (const note of Object.values(noteCache.notes)) { - if (note.isProtected && !note.isDecrypted) { + if (note.noteId === origNote.noteId) { continue; } diff --git a/src/services/search/parsing_context.js b/src/services/search/parsing_context.js index 7867ed186..93bf6d923 100644 --- a/src/services/search/parsing_context.js +++ b/src/services/search/parsing_context.js @@ -3,6 +3,7 @@ class ParsingContext { constructor(params = {}) { this.includeNoteContent = !!params.includeNoteContent; + this.excludeArchived = !!params.excludeArchived; this.fuzzyAttributeSearch = !!params.fuzzyAttributeSearch; this.highlightedTokens = []; this.originalQuery = ""; diff --git a/src/services/search/services/parse.js b/src/services/search/services/parse.js index 91f7baa99..29b9a699b 100644 --- a/src/services/search/services/parse.js +++ b/src/services/search/services/parse.js @@ -27,23 +27,16 @@ function getFulltext(tokens, parsingContext) { return null; } - let textSearchExpression; - if (parsingContext.includeNoteContent) { - textSearchExpression = new OrExp([ + return new OrExp([ new NoteCacheFulltextExp(tokens), new NoteContentProtectedFulltextExp('*=*', tokens), new NoteContentUnprotectedFulltextExp('*=*', tokens) ]); } else { - textSearchExpression = new NoteCacheFulltextExp(tokens); + return new NoteCacheFulltextExp(tokens); } - - return new AndExp([ - textSearchExpression, - new PropertyComparisonExp("isarchived", buildComparator("=", "false")) - ]); } function isOperator(str) { @@ -399,6 +392,7 @@ function getExpression(tokens, parsingContext, level = 0) { function parse({fulltextTokens, expressionTokens, parsingContext}) { return AndExp.of([ + parsingContext.excludeArchived ? new PropertyComparisonExp("isarchived", buildComparator("=", "false")) : null, getFulltext(fulltextTokens, parsingContext), getExpression(expressionTokens, parsingContext) ]); diff --git a/src/services/search/services/search.js b/src/services/search/services/search.js index d10cf8682..67f421ddb 100644 --- a/src/services/search/services/search.js +++ b/src/services/search/services/search.js @@ -91,6 +91,7 @@ function searchNotes(query) { const parsingContext = new ParsingContext({ includeNoteContent: true, + excludeArchived: true, fuzzyAttributeSearch: false }); @@ -114,6 +115,7 @@ function searchNotesForAutocomplete(query) { const parsingContext = new ParsingContext({ includeNoteContent: false, + excludeArchived: true, fuzzyAttributeSearch: true });