allow searching with noteId & fix combining fulltext and other conditions with OR

This commit is contained in:
zadam 2019-03-31 22:23:50 +02:00
parent 26621c0318
commit 5bda254184
3 changed files with 13 additions and 21 deletions

View file

@ -5,6 +5,7 @@ const VIRTUAL_ATTRIBUTES = [
"dateModified",
"utcDateCreated",
"utcDateModified",
"noteId",
"isProtected",
"title",
"content",
@ -37,20 +38,11 @@ module.exports = function(filters, selectedColumns = 'notes.*') {
const alias = "note_contents";
if (!(alias in joins)) {
joins[alias] = `JOIN note_contents ON note_contents.noteId = notes.noteId`;
joins[alias] = `LEFT JOIN note_contents ON note_contents.noteId = notes.noteId`;
}
accessor = `${alias}.${property}`;
}
else if (property === 'text') {
const alias = "note_fulltext";
if (!(alias in joins)) {
joins[alias] = `JOIN note_fulltext ON note_fulltext.noteId = notes.noteId`;
}
accessor = alias;
}
else {
accessor = "notes." + property;
}
@ -92,17 +84,11 @@ module.exports = function(filters, selectedColumns = 'notes.*') {
else if (filter.operator === '=' || filter.operator === '!=') {
if (filter.name === 'text') {
const safeSearchText = utils.sanitizeSql(filter.value);
let condition = accessor + ' ' + `MATCH '${safeSearchText}'`;
const not = filter.operator.includes("!") ? "NOT" : "";
if (filter.operator.includes("!")) {
// not supported!
}
else if (orderBy.length === 0) {
// if there's a positive full text search and there's no defined order then order by rank
orderBy.push("rank");
}
where += condition;
// fulltext needs to use subselect because fulltext doesn't support OR operations at all
// which makes it impossible to combine more operations together
where += `notes.noteId ${not} IN (SELECT noteId FROM note_fulltext WHERE note_fulltext MATCH '${safeSearchText}')`;
}
else {
where += `${accessor} ${filter.operator} ?`;

View file

@ -51,6 +51,12 @@ module.exports = function (searchText) {
name: 'text',
operator: '=',
value: searchText
},
{
relation: 'or',
name: 'noteId',
operator: '=',
value: searchText
}
]
}

View file

@ -91,7 +91,7 @@ function decryptNoteRevision(hist) {
}
}
catch (e) {
throw new Error(`Decryption failed for note ${hist.noteId}: ` + e.message + " " + e.stack);
throw new Error(`Decryption failed for note ${hist.noteId}, revision ${hist.noteRevisionId}: ` + e.message + " " + e.stack);
}
}