Skip to content

Build your personal knowledge base with Trilium Notes

added IN operator to search, closes #534

zadam1a87190f43b2
diff --git a/src/services/build_search_query.js b/src/services/build_search_query.js
index efd549140f..2f54afb76f 100644
--- a/src/services/build_search_query.js
+++ b/src/services/build_search_query.js
@@ -67,7 +67,7 @@ module.exports = function(filters, selectedColumns = 'notes.*') {
const params = [];
for (const filter of filters) {
- if (['isarchived', 'orderby', 'limit'].includes(filter.name.toLowerCase())) {
+ if (['isarchived', 'in', 'orderby', 'limit'].includes(filter.name.toLowerCase())) {
continue; // these are not real filters
}
diff --git a/src/services/note_cache.js b/src/services/note_cache.js
index c8c93cb3ad..dfdc2ae356 100644
--- a/src/services/note_cache.js
+++ b/src/services/note_cache.js
@@ -255,6 +255,25 @@ function isArchived(noteId) {
return isNotePathArchived(notePath);
}
+/**
+ * @param {string} noteId
+ * @param {string} ancestorNoteId
+ * @return {boolean} - true if given noteId has ancestorNoteId in any of its paths (even archived)
+ */
+function isInAncestor(noteId, ancestorNoteId) {
+ if (ancestorNoteId === noteId) { // special case
+ return true;
+ }
+
+ for (const parentNoteId of childToParent[noteId] || []) {
+ if (isInAncestor(parentNoteId, ancestorNoteId)) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
function getNoteTitleFromPath(notePath) {
const pathArr = notePath.split("/");
@@ -529,6 +548,7 @@ module.exports = {
getNoteTitleFromPath,
isAvailable,
isArchived,
+ isInAncestor,
load,
findSimilarNotes
};
\ No newline at end of file
diff --git a/src/services/search.js b/src/services/search.js
index 70436827d0..472e66e6fb 100644
--- a/src/services/search.js
+++ b/src/services/search.js
@@ -35,6 +35,20 @@ async function searchForNoteIds(searchString) {
}
}
+ const isInFilter = filters.find(filter => filter.name.toLowerCase() === 'in');
+
+ if (isInFilter) {
+ if (isInFilter.operator === '=') {
+ noteIds = noteIds.filter(noteId => noteCacheService.isInAncestor(noteId, isInFilter.value));
+ }
+ else if (isInFilter.operator === '!=') {
+ noteIds = noteIds.filter(noteId => !noteCacheService.isInAncestor(noteId, isInFilter.value));
+ }
+ else {
+ throw new Error(`Unrecognized isIn operator ${isInFilter.operator}`);
+ }
+ }
+
const limitFilter = filters.find(filter => filter.name.toLowerCase() === 'limit');
if (limitFilter) {