mirror of
https://github.com/zadam/trilium.git
synced 2025-11-18 03:00:41 +01:00
saved search can now be created from the search dialog
This commit is contained in:
67
src/services/build_search_query.js
Normal file
67
src/services/build_search_query.js
Normal file
@@ -0,0 +1,67 @@
|
||||
module.exports = function(attrFilters, searchText) {
|
||||
const joins = [];
|
||||
const joinParams = [];
|
||||
let where = '1';
|
||||
const whereParams = [];
|
||||
|
||||
let i = 1;
|
||||
|
||||
for (const filter of attrFilters) {
|
||||
joins.push(`LEFT JOIN attributes AS attr${i} ON attr${i}.noteId = notes.noteId AND attr${i}.name = ?`);
|
||||
joinParams.push(filter.name);
|
||||
|
||||
where += " " + filter.relation + " ";
|
||||
|
||||
if (filter.operator === 'exists') {
|
||||
where += `attr${i}.attributeId IS NOT NULL`;
|
||||
}
|
||||
else if (filter.operator === 'not-exists') {
|
||||
where += `attr${i}.attributeId IS NULL`;
|
||||
}
|
||||
else if (filter.operator === '=' || filter.operator === '!=') {
|
||||
where += `attr${i}.value ${filter.operator} ?`;
|
||||
whereParams.push(filter.value);
|
||||
}
|
||||
else if ([">", ">=", "<", "<="].includes(filter.operator)) {
|
||||
const floatParam = parseFloat(filter.value);
|
||||
|
||||
if (isNaN(floatParam)) {
|
||||
where += `attr${i}.value ${filter.operator} ?`;
|
||||
whereParams.push(filter.value);
|
||||
}
|
||||
else {
|
||||
where += `CAST(attr${i}.value AS DECIMAL) ${filter.operator} ?`;
|
||||
whereParams.push(floatParam);
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw new Error("Unknown operator " + filter.operator);
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
let searchCondition = '';
|
||||
const searchParams = [];
|
||||
|
||||
if (searchText.trim() !== '') {
|
||||
// searching in protected notes is pointless because of encryption
|
||||
searchCondition = ' AND (notes.isProtected = 0 AND (notes.title LIKE ? OR notes.content LIKE ?))';
|
||||
|
||||
searchText = '%' + searchText.trim() + '%';
|
||||
|
||||
searchParams.push(searchText);
|
||||
searchParams.push(searchText); // two occurences in searchCondition
|
||||
}
|
||||
|
||||
const query = `SELECT DISTINCT notes.noteId FROM notes
|
||||
${joins.join('\r\n')}
|
||||
WHERE
|
||||
notes.isDeleted = 0
|
||||
AND (${where})
|
||||
${searchCondition}`;
|
||||
|
||||
const params = joinParams.concat(whereParams).concat(searchParams);
|
||||
|
||||
return { query, params };
|
||||
};
|
||||
@@ -96,7 +96,7 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {})
|
||||
mime: extraOptions.mime
|
||||
};
|
||||
|
||||
if (extraOptions.json) {
|
||||
if (extraOptions.json && !note.type) {
|
||||
note.type = "code";
|
||||
note.mime = "application/json";
|
||||
}
|
||||
|
||||
28
src/services/parse_filters.js
Normal file
28
src/services/parse_filters.js
Normal file
@@ -0,0 +1,28 @@
|
||||
module.exports = function(searchText) {
|
||||
const attrFilters = [];
|
||||
|
||||
const attrRegex = /(\b(and|or)\s+)?@(!?)([\w_-]+|"[^"]+")((=|!=|<|<=|>|>=)([\w_-]+|"[^"]+"))?/i;
|
||||
|
||||
let match = attrRegex.exec(searchText);
|
||||
|
||||
function trimQuotes(str) { return str.startsWith('"') ? str.substr(1, str.length - 2) : str; }
|
||||
|
||||
while (match != null) {
|
||||
const relation = match[2] !== undefined ? match[2].toLowerCase() : 'and';
|
||||
const operator = match[3] === '!' ? 'not-exists' : 'exists';
|
||||
|
||||
attrFilters.push({
|
||||
relation: relation,
|
||||
name: trimQuotes(match[4]),
|
||||
operator: match[6] !== undefined ? match[6] : operator,
|
||||
value: match[7] !== undefined ? trimQuotes(match[7]) : null
|
||||
});
|
||||
|
||||
// remove attributes from further fulltext search
|
||||
searchText = searchText.split(match[0]).join('');
|
||||
|
||||
match = attrRegex.exec(searchText);
|
||||
}
|
||||
|
||||
return {attrFilters, searchText};
|
||||
};
|
||||
Reference in New Issue
Block a user