mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
ordering of search results
This commit is contained in:
parent
3ee33e1024
commit
81dc907afc
@ -1,3 +1,5 @@
|
|||||||
|
const utils = require('./utils');
|
||||||
|
|
||||||
const VIRTUAL_ATTRIBUTES = ["dateCreated", "dateCreated", "dateModified", "utcDateCreated", "utcDateModified", "isProtected", "title", "content", "type", "mime", "text"];
|
const VIRTUAL_ATTRIBUTES = ["dateCreated", "dateCreated", "dateModified", "utcDateCreated", "utcDateModified", "isProtected", "title", "content", "type", "mime", "text"];
|
||||||
|
|
||||||
module.exports = function(filters) {
|
module.exports = function(filters) {
|
||||||
@ -21,10 +23,9 @@ module.exports = function(filters) {
|
|||||||
accessor = `${alias}.value`;
|
accessor = `${alias}.value`;
|
||||||
}
|
}
|
||||||
else if (property === 'content') {
|
else if (property === 'content') {
|
||||||
const alias = "note_content";
|
const alias = "note_contents";
|
||||||
|
|
||||||
if (!(alias in joins)) {
|
if (!(alias in joins)) {
|
||||||
// FIXME: this will fail if there's more instances of content
|
|
||||||
joins[alias] = `JOIN note_contents ON note_contents.noteId = notes.noteId`;
|
joins[alias] = `JOIN note_contents ON note_contents.noteId = notes.noteId`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,7 +35,6 @@ module.exports = function(filters) {
|
|||||||
const alias = "note_fulltext";
|
const alias = "note_fulltext";
|
||||||
|
|
||||||
if (!(alias in joins)) {
|
if (!(alias in joins)) {
|
||||||
// FIXME: this will fail if there's more instances of content
|
|
||||||
joins[alias] = `JOIN note_fulltext ON note_fulltext.noteId = notes.noteId`;
|
joins[alias] = `JOIN note_fulltext ON note_fulltext.noteId = notes.noteId`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,10 +47,27 @@ module.exports = function(filters) {
|
|||||||
return accessor;
|
return accessor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let orderBy = [];
|
||||||
|
|
||||||
|
const orderByFilter = filters.find(filter => filter.name.toLowerCase() === 'orderby');
|
||||||
|
|
||||||
|
if (orderByFilter) {
|
||||||
|
orderBy = orderByFilter.value.split(",").map(prop => {
|
||||||
|
const direction = prop.includes("-") ? "DESC" : "ASC";
|
||||||
|
const cleanedProp = prop.trim().replace("-", "");
|
||||||
|
|
||||||
|
return getAccessor(cleanedProp) + " " + direction;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let where = '1';
|
let where = '1';
|
||||||
const params = [];
|
const params = [];
|
||||||
|
|
||||||
for (const filter of filters) {
|
for (const filter of filters) {
|
||||||
|
if (filter.name.toLowerCase() === 'orderby') {
|
||||||
|
continue; // orderby is not real filter
|
||||||
|
}
|
||||||
|
|
||||||
where += " " + filter.relation + " ";
|
where += " " + filter.relation + " ";
|
||||||
|
|
||||||
const accessor = getAccessor(filter.name);
|
const accessor = getAccessor(filter.name);
|
||||||
@ -64,8 +81,17 @@ module.exports = function(filters) {
|
|||||||
else if (filter.operator === '=' || filter.operator === '!=') {
|
else if (filter.operator === '=' || filter.operator === '!=') {
|
||||||
if (filter.name === 'text') {
|
if (filter.name === 'text') {
|
||||||
const safeSearchText = utils.sanitizeSql(filter.value);
|
const safeSearchText = utils.sanitizeSql(filter.value);
|
||||||
|
let condition = accessor + ' ' + `MATCH '${safeSearchText}'`;
|
||||||
|
|
||||||
where += accessor + ' ' + (filter.operator === '!=' ? 'NOT ' : '') + `MATCH '${safeSearchText}'`;
|
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;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
where += `${accessor} ${filter.operator} ?`;
|
where += `${accessor} ${filter.operator} ?`;
|
||||||
@ -110,11 +136,17 @@ module.exports = function(filters) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (orderBy.length === 0) {
|
||||||
|
// if no ordering is given then order at least by note title
|
||||||
|
orderBy.push("notes.title");
|
||||||
|
}
|
||||||
|
|
||||||
const query = `SELECT DISTINCT notes.noteId FROM notes
|
const query = `SELECT DISTINCT notes.noteId FROM notes
|
||||||
${Object.values(joins).join('\r\n')}
|
${Object.values(joins).join('\r\n')}
|
||||||
WHERE
|
WHERE
|
||||||
notes.isDeleted = 0
|
notes.isDeleted = 0
|
||||||
AND (${where})`;
|
AND (${where})
|
||||||
|
ORDER BY ` + orderBy.join(", ");
|
||||||
|
|
||||||
console.log(query);
|
console.log(query);
|
||||||
console.log(params);
|
console.log(params);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
const dayjs = require("dayjs");
|
const dayjs = require("dayjs");
|
||||||
|
|
||||||
const filterRegex = /(\b(AND|OR)\s+)?@(!?)([\w_]+|"[^"]+")((=|!=|<|<=|>|>=|!?\*=|!?=\*|!?\*=\*)([\w_-]+|"[^"]+"))?/ig;
|
const filterRegex = /(\b(AND|OR)\s+)?@(!?)([\w_]+|"[^"]+")\s*((=|!=|<|<=|>|>=|!?\*=|!?=\*|!?\*=\*)\s*([\w_-]+|"[^"]+"))?/ig;
|
||||||
const smartValueRegex = /^(NOW|TODAY|WEEK|MONTH|YEAR) *([+\-] *\d+)?$/i;
|
const smartValueRegex = /^(NOW|TODAY|WEEK|MONTH|YEAR) *([+\-] *\d+)?$/i;
|
||||||
|
|
||||||
function calculateSmartValue(v) {
|
function calculateSmartValue(v) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user