Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fulltext search now doesn't use FTS5, closes #489
zadam151641b659b7
diff --git a/src/services/build_search_query.js b/src/services/build_search_query.jsindex f81a6ff334..9ec2024239 100644--- a/src/services/build_search_query.js+++ b/src/services/build_search_query.js@@ -82,18 +82,8 @@ module.exports = function(filters, selectedColumns = 'notes.*') {where += `${accessor} IS NULL`;}else if (filter.operator === '=' || filter.operator === '!=') {- if (filter.name === 'text') {- const safeSearchText = utils.sanitizeSql(filter.value);- const not = filter.operator.includes("!") ? "NOT" : "";-- // 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} ?`;- params.push(filter.value);- }+ where += `${accessor} ${filter.operator} ?`;+ params.push(filter.value);}else if (filter.operator === '*=' || filter.operator === '!*=') {where += `${accessor}`@@ -106,9 +96,17 @@ module.exports = function(filters, selectedColumns = 'notes.*') {+ ` LIKE ` + utils.prepareSqlForLike('', filter.value, '%');}else if (filter.operator === '*=*' || filter.operator === '!*=*') {- where += `${accessor}`- + (filter.operator.includes('!') ? ' NOT' : '')- + ` LIKE ` + utils.prepareSqlForLike('%', filter.value, '%');+ const columns = filter.name === 'text' ? [getAccessor("title"), getAccessor("content")] : [accessor];++ let condition = "(" + columns.map(column =>+ `${column}` + ` LIKE ` + utils.prepareSqlForLike('%', filter.value, '%'))+ .join(" OR ") + ")";++ if (filter.operator.includes('!')) {+ condition = "NOT(" + condition + ")";+ }++ where += condition;}else if ([">", ">=", "<", "<="].includes(filter.operator)) {let floatParam;diff --git a/src/services/parse_filters.js b/src/services/parse_filters.jsindex e6c04beca7..c46cd207b1 100644--- a/src/services/parse_filters.js+++ b/src/services/parse_filters.js@@ -43,25 +43,45 @@ function calculateSmartValue(v) {}module.exports = function (searchText) {+ searchText = searchText.trim();+// if the string doesn't start with attribute then we consider it as just standard full text search- if (!searchText.trim().startsWith("@")) {+ if (!searchText.startsWith("@")) {// replace with space instead of empty string since these characters are probably separators- const sanitizedSearchText = searchText.replace(/[^\w ]+/g, " ");+ const filters = [];++ if (searchText.startsWith('"') && searchText.endsWith('"')) {+ // "bla bla" will search for exact match+ searchText = searchText.substr(1, searchText.length - 2);- return [- {+ filters.push({relation: 'and',name: 'text',- operator: '=',- value: sanitizedSearchText- },- {- relation: 'or',- name: 'noteId',- operator: '=',- value: sanitizedSearchText+ operator: '*=*',+ value: searchText+ });+ }+ else {+ const tokens = searchText.split(/\s+/);++ for (const token of tokens) {+ filters.push({+ relation: 'and',+ name: 'text',+ operator: '*=*',+ value: token+ });}- ]+ }++ filters.push({+ relation: 'or',+ name: 'noteId',+ operator: '=',+ value: searchText+ });++ return filters;}const filters = [];