From bf496488964251814d624048dc7bd784b4034801 Mon Sep 17 00:00:00 2001 From: zadam Date: Wed, 11 May 2022 22:43:21 +0200 Subject: [PATCH] allow operators =, *= and =* on note content --- .../expressions/note_content_fulltext.js | 43 +++++++++++++------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/services/search/expressions/note_content_fulltext.js b/src/services/search/expressions/note_content_fulltext.js index 667943afdd..bab10644ed 100644 --- a/src/services/search/expressions/note_content_fulltext.js +++ b/src/services/search/expressions/note_content_fulltext.js @@ -8,14 +8,17 @@ const protectedSessionService = require('../../protected_session'); const striptags = require('striptags'); const utils = require("../../utils"); +const ALLOWED_OPERATORS = ['*=*', '=', '*=', '=*']; + class NoteContentFulltextExp extends Expression { constructor(operator, {tokens, raw, flatText}) { super(); - if (operator !== '*=*') { - throw new Error(`Note content can be searched only with *=* operator`); + if (!ALLOWED_OPERATORS.includes(operator)) { + throw new Error(`Note content can be searched only with operators: ` + ALLOWED_OPERATORS.join(", ")); } + this.operator = operator; this.tokens = tokens; this.raw = !!raw; this.flatText = !!flatText; @@ -49,18 +52,30 @@ class NoteContentFulltextExp extends Expression { content = this.preprocessContent(content, type, mime); - const nonMatchingToken = this.tokens.find(token => - !content.includes(token) && - ( - // in case of default fulltext search we should consider both title, attrs and content - // so e.g. "hello world" should match when "hello" is in title and "world" in content - !this.flatText - || !becca.notes[noteId].getFlatText().includes(token) - ) - ); - - if (!nonMatchingToken) { - resultNoteSet.add(becca.notes[noteId]); + if (this.tokens.length === 1 && this.operator !== '*=*') { + const [token] = this.tokens; + + if ((this.operator === '=' && token === content) + || (this.operator === '*=' && content.endsWith(token)) + || (this.operator === '=*' && content.startsWith(token))) { + + resultNoteSet.add(becca.notes[noteId]); + } + } + else { + const nonMatchingToken = this.tokens.find(token => + !content.includes(token) && + ( + // in case of default fulltext search we should consider both title, attrs and content + // so e.g. "hello world" should match when "hello" is in title and "world" in content + !this.flatText + || !becca.notes[noteId].getFlatText().includes(token) + ) + ); + + if (!nonMatchingToken) { + resultNoteSet.add(becca.notes[noteId]); + } } }