Skip to content

Build your personal knowledge base with Trilium Notes

search script UI

zadama1f67e830d8f
diff --git a/src/public/app/services/date_notes.js b/src/public/app/services/date_notes.js
index d46d81dd4d..30a734a44f 100644
--- a/src/public/app/services/date_notes.js
+++ b/src/public/app/services/date_notes.js
@@ -47,8 +47,8 @@ async function createSearchNote(opts = {}) {
const note = await server.post('search-note');
const attrsToUpdate = [
- opts.ancestor ? { type: 'relation', name: 'ancestor', value: opts.ancestorNoteId } : undefined,
- { type: 'label', name: 'searchString', value: opts.searchString }
+ opts.ancestorNoteId ? { type: 'relation', name: 'ancestor', value: opts.ancestorNoteId } : undefined,
+ { type: 'label', name: 'searchString', value: opts.searchStringe }
].filter(attr => !!attr);
if (attrsToUpdate.length > 0) {
diff --git a/src/public/app/services/tree_cache.js b/src/public/app/services/tree_cache.js
index 6e2e67dd3c..d9661437e4 100644
--- a/src/public/app/services/tree_cache.js
+++ b/src/public/app/services/tree_cache.js
@@ -167,7 +167,7 @@ class TreeCache {
for (const note of resp.notes) {
if (note.type === 'search') {
const searchResultNoteIds = await server.get('search-note/' + note.noteId);
-
+console.log("searchResultNoteIds", searchResultNoteIds);
if (!Array.isArray(searchResultNoteIds)) {
throw new Error(`Search note ${note.noteId} failed: ${searchResultNoteIds}`);
}
diff --git a/src/public/app/widgets/search_actions/abstract_search_action.js b/src/public/app/widgets/search_actions/abstract_search_action.js
index 3a9f771ee9..28d3133a8d 100644
--- a/src/public/app/widgets/search_actions/abstract_search_action.js
+++ b/src/public/app/widgets/search_actions/abstract_search_action.js
@@ -1,6 +1,7 @@
import server from "../../services/server.js";
import ws from "../../services/ws.js";
import Component from "../component.js";
+import utils from "../../services/utils.js";
export default class AbstractSearchAction extends Component {
constructor(attribute, actionDef) {
@@ -18,6 +19,8 @@ export default class AbstractSearchAction extends Component {
.on('click', () => this.deleteAction())
.attr('title', 'Remove this search action');
+ utils.initHelpDropdown($rendered);
+
return $rendered;
}
catch (e) {
diff --git a/src/public/app/widgets/search_actions/execute_script.js b/src/public/app/widgets/search_actions/execute_script.js
index 1bd24ec42a..16ea0a9b91 100644
--- a/src/public/app/widgets/search_actions/execute_script.js
+++ b/src/public/app/widgets/search_actions/execute_script.js
@@ -21,6 +21,10 @@ const TPL = `
For example to append a string to a note's title, use this small script:
<pre>note.title = note.title + ' - suffix';</pre>
+
+ More complex example would be deleting all matched note's attributes:
+
+ <pre>for (const attr of note.getOwnedAttributes) { attr.isDeleted = true; attr.save(); }</pre>
</div>
</div>
diff --git a/src/public/app/widgets/search_definition.js b/src/public/app/widgets/search_definition.js
index 431640128f..8e49a8bdcf 100644
--- a/src/public/app/widgets/search_definition.js
+++ b/src/public/app/widgets/search_definition.js
@@ -1,4 +1,3 @@
-import noteAutocompleteService from "../services/note_autocomplete.js";
import server from "../services/server.js";
import TabAwareWidget from "./tab_aware_widget.js";
import treeCache from "../services/tree_cache.js";
@@ -18,12 +17,13 @@ import FastSearch from "./search_options/fast_search.js";
import Ancestor from "./search_options/ancestor.js";
import IncludeArchivedNotes from "./search_options/include_archived_notes.js";
import OrderBy from "./search_options/order_by.js";
+import SearchScript from "./search_options/search_script.js";
const TPL = `
<div class="search-definition-widget">
<style>
.search-setting-table {
- margin-top: 7px;
+ margin-top: 0;
margin-bottom: 7px;
width: 100%;
border-collapse: separate;
@@ -63,6 +63,10 @@ const TPL = `
.search-definition-widget input:invalid {
border: 3px solid red;
}
+
+ .add-search-option button {
+ margin-top: 5px; /* to give some spacing when buttons overflow on the next line */
+ }
</style>
<div class="search-settings">
@@ -75,6 +79,11 @@ const TPL = `
search string
</button>
+ <button type="button" class="btn btn-sm" data-search-option-add="searchScript">
+ <span class="bx bx-code"></span>
+ search script
+ </button>
+
<button type="button" class="btn btn-sm" data-search-option-add="ancestor">
<span class="bx bx-filter-alt"></span>
ancestor
@@ -150,6 +159,7 @@ const TPL = `
const OPTION_CLASSES = [
SearchString,
+ SearchScript,
Ancestor,
FastSearch,
IncludeArchivedNotes,
@@ -232,7 +242,12 @@ export default class SearchDefinitionWidget extends TabAwareWidget {
}
async refreshResultsCommand() {
- await treeCache.reloadNotes([this.noteId]);
+ try {
+ await treeCache.reloadNotes([this.noteId]);
+ }
+ catch (e) {
+ toastService.showError(e.message);
+ }
this.triggerEvent('searchRefreshed', {tabId: this.tabContext.tabId});
}
diff --git a/src/public/app/widgets/search_options/abstract_search_option.js b/src/public/app/widgets/search_options/abstract_search_option.js
index 9dab8ae887..6103f9dde9 100644
--- a/src/public/app/widgets/search_options/abstract_search_option.js
+++ b/src/public/app/widgets/search_options/abstract_search_option.js
@@ -1,6 +1,7 @@
import server from "../../services/server.js";
import ws from "../../services/ws.js";
import Component from "../component.js";
+import utils from "../../services/utils.js";
export default class AbstractSearchOption extends Component {
constructor(attribute, note) {
@@ -28,6 +29,8 @@ export default class AbstractSearchOption extends Component {
.on('click', () => this.deleteOption())
.attr('title', 'Remove this search option');
+ utils.initHelpDropdown($rendered);
+
return $rendered;
}
catch (e) {
diff --git a/src/public/app/widgets/search_options/ancestor.js b/src/public/app/widgets/search_options/ancestor.js
index 3866430e13..add4429907 100644
--- a/src/public/app/widgets/search_options/ancestor.js
+++ b/src/public/app/widgets/search_options/ancestor.js
@@ -30,14 +30,18 @@ export default class Ancestor extends AbstractSearchOption {
noteAutocompleteService.initNoteAutocomplete($ancestor);
$ancestor.on('autocomplete:closed', async () => {
- const ancestorOfNoteId = $ancestor.getSelectedNoteId();
+ const ancestorNoteId = $ancestor.getSelectedNoteId();
- await this.setAttribute('relation', 'ancestor', ancestorOfNoteId);
+ if (ancestorNoteId) {
+ await this.setAttribute('relation', 'ancestor', ancestorNoteId);
+ }
});
const ancestorNoteId = this.note.getRelationValue('ancestor');
- $ancestor.setNote(ancestorNoteId);
+ if (ancestorNoteId !== 'root') {
+ $ancestor.setNote(ancestorNoteId);
+ }
return $option;
}
diff --git a/src/public/app/widgets/search_options/search_script.js b/src/public/app/widgets/search_options/search_script.js
new file mode 100644
index 0000000000..4ce1cb1f27
--- /dev/null
+++ b/src/public/app/widgets/search_options/search_script.js
@@ -0,0 +1,70 @@
+import AbstractSearchOption from "./abstract_search_option.js";
+import noteAutocompleteService from "../../services/note_autocomplete.js";
+
+const TPL = `
+<tr>
+ <td class="title-column">
+ Search script:
+ </td>
+ <td>
+ <div class="input-group">
+ <input class="search-script form-control" placeholder="search for note by its name">
+ </div>
+ </td>
+ <td class="button-column">
+ <div class="dropdown help-dropdown">
+ <span class="bx bx-help-circle icon-action" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></span>
+ <div class="dropdown-menu dropdown-menu-right p-4">
+ <p>Search script allows to define search results by running a script. This provides maximal flexibility when standard search doesn't suffice.</p>
+
+ <p>Search script must be of type "code" and subtype "JavaScript backend". The script receives needs to return an array of noteIds or notes.</p>
+
+ <p>See this example:</p>
+
+ <pre>
+// 1. prefiltering using standard search
+const candidateNotes = api.searchForNotes("#journal");
+
+// 2. applying custom search criteria
+const matchedNotes = candidateNotes
+ .filter(note => note.title.match(/[0-9]{1,2}\. ?[0-9]{1,2}\. ?[0-9]{4}\/));
+
+return matchedNotes;</pre>
+
+ <p>Note that search script and search string can't be combined with each other.</p>
+ </div>
+
+ <span class="bx bx-x icon-action search-option-del"></span>
+ </td>
+</tr>`;
+
+export default class SearchScript extends AbstractSearchOption {
+ static get optionName() { return "searchScript" };
+ static get attributeType() { return "relation" };
+
+ static async create(noteId) {
+ await AbstractSearchOption.setAttribute(noteId, 'relation', 'searchScript', 'root');
+ }
+
+ doRender() {
+ const $option = $(TPL);
+ const $searchScript = $option.find('.search-script');
+ noteAutocompleteService.initNoteAutocomplete($searchScript);
+
+ $searchScript.on('autocomplete:closed', async () => {
+ const searchScriptNoteId = $searchScript.getSelectedNoteId();
+
+ if (searchScriptNoteId) {
+ await this.setAttribute('relation', 'searchScript', searchScriptNoteId);
+ }
+ });
+
+ const searchScriptNoteId = this.note.getRelationValue('searchScript');
+
+ if (searchScriptNoteId !== 'root') {
+ $searchScript.setNote(searchScriptNoteId);
+ }
+
+ return $option;
+ }
+}
diff --git a/src/public/app/widgets/search_options/search_string.js b/src/public/app/widgets/search_options/search_string.js
index a895934c8d..ce83d8a612 100644
--- a/src/public/app/widgets/search_options/search_string.js
+++ b/src/public/app/widgets/search_options/search_string.js
@@ -7,7 +7,7 @@ const TPL = `
<tr>
<td class="title-column">Search string:</td>
<td>
- <input type="text" class="form-control search-string">
+ <input type="text" class="form-control search-string" placeholder="fulltext keywords, #tag = value ...">
</td>
<td class="button-column">
<div class="dropdown help-dropdown">
@@ -64,8 +64,6 @@ export default class SearchString extends AbstractSearchOption {
this.$searchString.val(this.note.getLabelValue('searchString'));
- utils.initHelpDropdown($option);
-
return $option;
}
diff --git a/src/public/stylesheets/style.css b/src/public/stylesheets/style.css
index f1d686e98e..fa607898c2 100644
--- a/src/public/stylesheets/style.css
+++ b/src/public/stylesheets/style.css
@@ -935,7 +935,7 @@ ul.fancytree-container li {
border-width: 2px;
box-shadow: 10px 10px 93px -25px black;
padding: 10px 15px 10px 15px !important;
- width: 500px;
+ width: 600px;
}
.help-dropdown .dropdown-menu pre {
diff --git a/src/routes/api/search.js b/src/routes/api/search.js
index e4e62af6ab..572518a0a4 100644
--- a/src/routes/api/search.js
+++ b/src/routes/api/search.js
@@ -9,34 +9,28 @@ const searchService = require('../../services/search/services/search');
async function search(note) {
let searchResultNoteIds;
- try {
- const searchScript = note.getRelationValue('searchScript');
- const searchString = note.getLabelValue('searchString');
-
- if (searchScript) {
- searchResultNoteIds = await searchFromRelation(note, 'searchScript');
- } else {
- const searchContext = new SearchContext({
- fastSearch: note.hasLabel('fastSearch'),
- ancestorNoteId: note.getRelationValue('ancestor'),
- includeArchivedNotes: note.hasLabel('includeArchivedNotes'),
- orderBy: note.getLabelValue('orderBy'),
- orderDirection: note.getLabelValue('orderDirection'),
- fuzzyAttributeSearch: false
- });
-
- searchResultNoteIds = searchService.findNotesWithQuery(searchString, searchContext)
- .map(sr => sr.noteId);
- }
-
- // we won't return search note's own noteId
- // also don't allow root since that would force infinite cycle
- return searchResultNoteIds.filter(resultNoteId => !['root', note.noteId].includes(resultNoteId));
- } catch (e) {
- log.error(`Search failed for note ${note.noteId}: ` + e.message + ": " + e.stack);
-
- throw new Error("Search failed, see logs for details.");
+ const searchScript = note.getRelationValue('searchScript');
+ const searchString = note.getLabelValue('searchString');
+
+ if (searchScript) {
+ searchResultNoteIds = await searchFromRelation(note, 'searchScript');
+ } else {
+ const searchContext = new SearchContext({
+ fastSearch: note.hasLabel('fastSearch'),
+ ancestorNoteId: note.getRelationValue('ancestor'),
+ includeArchivedNotes: note.hasLabel('includeArchivedNotes'),
+ orderBy: note.getLabelValue('orderBy'),
+ orderDirection: note.getLabelValue('orderDirection'),
+ fuzzyAttributeSearch: false
+ });
+
+ searchResultNoteIds = searchService.findNotesWithQuery(searchString, searchContext)
+ .map(sr => sr.noteId);
}
+
+ // we won't return search note's own noteId
+ // also don't allow root since that would force infinite cycle
+ return searchResultNoteIds.filter(resultNoteId => !['root', note.noteId].includes(resultNoteId));
}
async function searchFromNote(req) {
@@ -55,13 +49,7 @@ async function searchFromNote(req) {
return [400, `Note ${req.params.noteId} is not a search note.`]
}
- let searchResultNoteIds = await search(note);
-
- if (searchResultNoteIds.length > 200) {
- searchResultNoteIds = searchResultNoteIds.slice(0, 200);
- }
-
- return searchResultNoteIds;
+ return await search(note);
}
const ACTION_HANDLERS = {
diff --git a/src/routes/routes.js b/src/routes/routes.js
index df1314acb4..0bf7b25cb7 100644
--- a/src/routes/routes.js
+++ b/src/routes/routes.js
@@ -101,6 +101,11 @@ function route(method, path, middleware, routeHandler, resultHandler, transactio
if (resultHandler) {
if (result && result.then) {
result.then(actualResult => resultHandler(req, res, actualResult))
+ .catch(e => {
+ log.error(`${method} ${path} threw exception: ` + e.stack);
+
+ res.status(500).send(e.message);
+ });
}
else {
resultHandler(req, res, result);
@@ -110,10 +115,10 @@ function route(method, path, middleware, routeHandler, resultHandler, transactio
catch (e) {
log.error(`${method} ${path} threw exception: ` + e.stack);
- res.sendStatus(500);
+ res.status(500).send(e.message);
}
- log.request(req, Date.now() - start);
+ log.request(req, res, Date.now() - start);
});
}
diff --git a/src/services/log.js b/src/services/log.js
index b4c39e99f9..6a30b89f0b 100644
--- a/src/services/log.js
+++ b/src/services/log.js
@@ -64,7 +64,7 @@ function error(message) {
const requestBlacklist = [ "/libraries", "/app", "/images", "/stylesheets" ];
-function request(req, timeMs) {
+function request(req, res, timeMs) {
for (const bl of requestBlacklist) {
if (req.url.startsWith(bl)) {
return;
@@ -76,7 +76,7 @@ function request(req, timeMs) {
}
info((timeMs >= 10 ? "Slow " : "") +
- req.method + " " + req.url + " took " + timeMs + "ms");
+ res.statusCode + " " + req.method + " " + req.url + " took " + timeMs + "ms");
}
function pad(num) {