mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
added protected note full text search
This commit is contained in:
parent
5f699cc28c
commit
56d6384ae1
@ -0,0 +1,54 @@
|
||||
"use strict";
|
||||
|
||||
const Expression = require('./expression');
|
||||
const NoteSet = require('../note_set');
|
||||
const log = require('../../log');
|
||||
const noteCache = require('../../note_cache/note_cache');
|
||||
const protectedSessionService = require('../../protected_session');
|
||||
|
||||
class NoteContentProtectedFulltextExp extends Expression {
|
||||
constructor(operator, tokens) {
|
||||
super();
|
||||
|
||||
if (operator !== '*=*') {
|
||||
throw new Error(`Note content can be searched only with *=* operator`);
|
||||
}
|
||||
|
||||
this.tokens = tokens;
|
||||
}
|
||||
|
||||
execute(inputNoteSet) {
|
||||
const resultNoteSet = new NoteSet();
|
||||
|
||||
if (!protectedSessionService.isProtectedSessionAvailable()) {
|
||||
return resultNoteSet;
|
||||
}
|
||||
|
||||
const sql = require('../../sql');
|
||||
|
||||
for (let {noteId, content} of sql.iterateRows(`SELECT noteId, content FROM notes JOIN note_contents USING (noteId) WHERE isDeleted = 0 AND isProtected = 1`)) {
|
||||
|
||||
try {
|
||||
content = protectedSessionService.decryptString(content);
|
||||
}
|
||||
catch (e) {
|
||||
log.info('Cannot decrypt content of note', noteId);
|
||||
continue;
|
||||
}
|
||||
|
||||
content = content.toLowerCase();
|
||||
|
||||
if (this.tokens.find(token => !content.includes(token))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (inputNoteSet.hasNoteId(noteId) && noteId in noteCache.notes) {
|
||||
resultNoteSet.add(noteCache.notes[noteId]);
|
||||
}
|
||||
}
|
||||
|
||||
return resultNoteSet;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = NoteContentProtectedFulltextExp;
|
@ -5,22 +5,27 @@ const NoteSet = require('../note_set');
|
||||
const noteCache = require('../../note_cache/note_cache');
|
||||
const utils = require('../../utils');
|
||||
|
||||
class NoteContentFulltextExp extends Expression {
|
||||
class NoteContentUnprotectedFulltextExp extends Expression {
|
||||
constructor(operator, tokens) {
|
||||
super();
|
||||
|
||||
this.likePrefix = ["*=*", "*="].includes(operator) ? "%" : "";
|
||||
this.likeSuffix = ["*=*", "=*"].includes(operator) ? "%" : "";
|
||||
if (operator !== '*=*') {
|
||||
throw new Error(`Note content can be searched only with *=* operator`);
|
||||
}
|
||||
|
||||
this.tokens = tokens;
|
||||
}
|
||||
|
||||
execute(inputNoteSet) {
|
||||
const resultNoteSet = new NoteSet();
|
||||
const wheres = this.tokens.map(token => "note_contents.content LIKE " + utils.prepareSqlForLike(this.likePrefix, token, this.likeSuffix));
|
||||
const wheres = this.tokens.map(token => "note_contents.content LIKE " + utils.prepareSqlForLike('%', token, '%'));
|
||||
|
||||
const sql = require('../../sql');
|
||||
|
||||
console.log(`
|
||||
SELECT notes.noteId
|
||||
FROM notes
|
||||
JOIN note_contents ON notes.noteId = note_contents.noteId
|
||||
WHERE isDeleted = 0 AND isProtected = 0 AND ${wheres.join(' AND ')}`);
|
||||
const noteIds = sql.getColumn(`
|
||||
SELECT notes.noteId
|
||||
FROM notes
|
||||
@ -37,4 +42,4 @@ class NoteContentFulltextExp extends Expression {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = NoteContentFulltextExp;
|
||||
module.exports = NoteContentUnprotectedFulltextExp;
|
@ -1,9 +1,4 @@
|
||||
const dayjs = require("dayjs");
|
||||
const AndExp = require('./expressions/and');
|
||||
const OrExp = require('./expressions/or');
|
||||
const NotExp = require('./expressions/not');
|
||||
const NoteCacheFulltextExp = require('./expressions/note_cache_fulltext');
|
||||
const NoteContentFulltextExp = require('./expressions/note_content_fulltext');
|
||||
|
||||
const filterRegex = /(\b(AND|OR)\s+)?@(!?)([\p{L}\p{Number}_]+|"[^"]+")\s*((=|!=|<|<=|>|>=|!?\*=|!?=\*|!?\*=\*)\s*([^\s=*"]+|"[^"]+"))?/igu;
|
||||
const smartValueRegex = /^(NOW|TODAY|WEEK|MONTH|YEAR) *([+\-] *\d+)?$/i;
|
||||
|
@ -11,7 +11,8 @@ const PropertyComparisonExp = require('./expressions/property_comparison');
|
||||
const AttributeExistsExp = require('./expressions/attribute_exists');
|
||||
const LabelComparisonExp = require('./expressions/label_comparison');
|
||||
const NoteCacheFulltextExp = require('./expressions/note_cache_fulltext');
|
||||
const NoteContentFulltextExp = require('./expressions/note_content_fulltext');
|
||||
const NoteContentProtectedFulltextExp = require('./expressions/note_content_protected_fulltext');
|
||||
const NoteContentUnprotectedFulltextExp = require('./expressions/note_content_unprotected_fulltext');
|
||||
const OrderByAndLimitExp = require('./expressions/order_by_and_limit');
|
||||
const comparatorBuilder = require('./comparator_builder');
|
||||
const ValueExtractor = require('./value_extractor');
|
||||
@ -25,7 +26,8 @@ function getFulltext(tokens, parsingContext) {
|
||||
else if (parsingContext.includeNoteContent) {
|
||||
return new OrExp([
|
||||
new NoteCacheFulltextExp(tokens),
|
||||
new NoteContentFulltextExp('*=*', tokens)
|
||||
new NoteContentProtectedFulltextExp('*=*', tokens),
|
||||
new NoteContentUnprotectedFulltextExp('*=*', tokens)
|
||||
]);
|
||||
}
|
||||
else {
|
||||
@ -67,7 +69,10 @@ function getExpression(tokens, parsingContext, level = 0) {
|
||||
|
||||
i++;
|
||||
|
||||
return new NoteContentFulltextExp(operator, [tokens[i]]);
|
||||
return new OrExp([
|
||||
NoteContentUnprotectedFulltextExp(operator, [tokens[i]]),
|
||||
NoteContentProtectedFulltextExp(operator, [tokens[i]])
|
||||
]);
|
||||
}
|
||||
|
||||
if (tokens[i] === 'parents') {
|
||||
|
Loading…
x
Reference in New Issue
Block a user