From dc2d5a0a79eddf2e59191a53693f15532313bd9c Mon Sep 17 00:00:00 2001 From: zadam Date: Tue, 26 May 2020 23:25:13 +0200 Subject: [PATCH] fix fulltext content search --- package-lock.json | 6 +++--- package.json | 2 +- src/services/search/expressions/and.js | 4 ++-- .../search/expressions/note_content_fulltext.js | 8 ++++++-- src/services/search/expressions/or.js | 4 ++-- src/services/search/parser.js | 17 ++++++++++++++++- 6 files changed, 30 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index dd61de609..1f7ab50ae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10328,9 +10328,9 @@ "dev": true }, "sqlite": { - "version": "4.0.8", - "resolved": "https://registry.npmjs.org/sqlite/-/sqlite-4.0.8.tgz", - "integrity": "sha512-MOy63kITfjJnZimrwgQ50+L83J3IBPjuyTZ98YooAmSXdLtfGHDTMgH5csWturZ/mzm4TafLvtjkIbhmQVNgcw==" + "version": "4.0.9", + "resolved": "https://registry.npmjs.org/sqlite/-/sqlite-4.0.9.tgz", + "integrity": "sha512-vB6Xzn5S5XxMfmyO0ErKjuP5jEQ0z+oFXFC4zXC0s12NMULLETUTb6+PST8sZ7/2HR4KLk4Jsj5yeXkCvogYxg==" }, "sqlite3": { "version": "4.1.1", diff --git a/package.json b/package.json index 3ba80e1be..67115fee0 100644 --- a/package.json +++ b/package.json @@ -67,7 +67,7 @@ "serve-favicon": "2.5.0", "session-file-store": "1.4.0", "simple-node-logger": "18.12.24", - "sqlite": "4.0.8", + "sqlite": "4.0.9", "sqlite3": "4.1.1", "string-similarity": "4.0.1", "tar-stream": "2.1.2", diff --git a/src/services/search/expressions/and.js b/src/services/search/expressions/and.js index ee22f6b13..9d0237c2e 100644 --- a/src/services/search/expressions/and.js +++ b/src/services/search/expressions/and.js @@ -18,9 +18,9 @@ class AndExp extends Expression { this.subExpressions = subExpressions; } - execute(inputNoteSet, searchContext) { + async execute(inputNoteSet, searchContext) { for (const subExpression of this.subExpressions) { - inputNoteSet = subExpression.execute(inputNoteSet, searchContext); + inputNoteSet = await subExpression.execute(inputNoteSet, searchContext); } return inputNoteSet; diff --git a/src/services/search/expressions/note_content_fulltext.js b/src/services/search/expressions/note_content_fulltext.js index 94932cde7..6c8b9dc34 100644 --- a/src/services/search/expressions/note_content_fulltext.js +++ b/src/services/search/expressions/note_content_fulltext.js @@ -3,17 +3,21 @@ const Expression = require('./expression'); const NoteSet = require('../note_set'); const noteCache = require('../../note_cache/note_cache'); +const utils = require('../../utils'); class NoteContentFulltextExp extends Expression { - constructor(tokens) { + constructor(operator, tokens) { super(); + this.likePrefix = ["*=*", "*="].includes(operator) ? "%" : ""; + this.likeSuffix = ["*=*", "=*"].includes(operator) ? "%" : ""; + this.tokens = tokens; } async execute(inputNoteSet) { const resultNoteSet = new NoteSet(); - const wheres = this.tokens.map(token => "note_contents.content LIKE " + utils.prepareSqlForLike('%', token, '%')); + const wheres = this.tokens.map(token => "note_contents.content LIKE " + utils.prepareSqlForLike(this.likePrefix, token, this.likeSuffix)); const sql = require('../../sql'); diff --git a/src/services/search/expressions/or.js b/src/services/search/expressions/or.js index 62c16f5cf..63586e0cc 100644 --- a/src/services/search/expressions/or.js +++ b/src/services/search/expressions/or.js @@ -21,11 +21,11 @@ class OrExp extends Expression { this.subExpressions = subExpressions; } - execute(inputNoteSet, searchContext) { + async execute(inputNoteSet, searchContext) { const resultNoteSet = new NoteSet(); for (const subExpression of this.subExpressions) { - resultNoteSet.mergeIn(subExpression.execute(inputNoteSet, searchContext)); + resultNoteSet.mergeIn(await subExpression.execute(inputNoteSet, searchContext)); } return resultNoteSet; diff --git a/src/services/search/parser.js b/src/services/search/parser.js index 3ff60278e..e7b6bac15 100644 --- a/src/services/search/parser.js +++ b/src/services/search/parser.js @@ -25,7 +25,7 @@ function getFulltext(tokens, parsingContext) { else if (parsingContext.includeNoteContent) { return new OrExp([ new NoteCacheFulltextExp(tokens), - new NoteContentFulltextExp(tokens) + new NoteContentFulltextExp('*=*', tokens) ]); } else { @@ -55,6 +55,21 @@ function getExpression(tokens, parsingContext, level = 0) { i++; + if (tokens[i] === 'content') { + i += 1; + + const operator = tokens[i]; + + if (!isOperator(operator)) { + parsingContext.addError(`After content expected operator, but got "${tokens[i]}"`); + return; + } + + i++; + + return new NoteContentFulltextExp(operator, [tokens[i]]); + } + if (tokens[i] === 'parents') { i += 1;