implemented @limit for search

This commit is contained in:
zadam
2019-04-22 18:08:33 +02:00
parent 15eaf67189
commit f273b4334e
8 changed files with 251 additions and 40 deletions

View File

@@ -99,10 +99,24 @@ function BackendScriptApi(currentNote, apiParams) {
*
* @method
* @param {string} searchString
* @returns ${Promise<Note[]>}
* @returns {Promise<Note[]>}
*/
this.searchForNotes = searchService.searchForNotes;
/**
* This is a powerful search method - you can search by attributes and their values, e.g.:
* "@dateModified =* MONTH AND @log". See full documentation for all options at: https://github.com/zadam/trilium/wiki/Search
*
* @method
* @param {string} searchString
* @returns {Promise<Note|null>}
*/
this.searchForNote = async searchString => {
const notes = await searchService.searchForNotes(searchString);
return notes.length > 0 ? notes[0] : null;
};
/**
* Retrieves notes with given label name & value
*

View File

@@ -67,8 +67,8 @@ module.exports = function(filters, selectedColumns = 'notes.*') {
const params = [];
for (const filter of filters) {
if (filter.name.toLowerCase() === 'orderby') {
continue; // orderby is not real filter
if (['orderby', 'limit'].includes(filter.name.toLowerCase())) {
continue; // orderby and limit are not real filters
}
where += " " + filter.relation + " ";

View File

@@ -376,6 +376,16 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED
}
});
/**
* @param noteId
* @returns {boolean} - true if note exists (is not deleted) and is not archived.
*/
function isAvailable(noteId) {
const notePath = getNotePath(noteId);
return !!notePath;
}
eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, () => {
if (loaded) {
loadProtectedNotes();
@@ -388,5 +398,6 @@ module.exports = {
findNotes,
getNotePath,
getNoteTitleForPath,
isAvailable,
load
};

View File

@@ -42,6 +42,21 @@ async function getNote(noteId) {
return await getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]);
}
/** @returns {Promise<Note[]>} */
async function getNotes(noteIds) {
// this note might be optimised, but remember that it must keep the existing order of noteIds
// (important e.g. for @orderBy in search)
const notes = [];
for (const noteId of noteIds) {
const note = await getNote(noteId);
notes.push(note);
}
return notes;
}
/** @returns {Promise<Branch|null>} */
async function getBranch(branchId) {
return await getEntity("SELECT * FROM branches WHERE branchId = ?", [branchId]);
@@ -125,6 +140,7 @@ module.exports = {
getEntities,
getEntity,
getNote,
getNotes,
getBranch,
getAttribute,
getOption,

View File

@@ -3,20 +3,12 @@ const sql = require('./sql');
const log = require('./log');
const parseFilters = require('./parse_filters');
const buildSearchQuery = require('./build_search_query');
const noteCacheService = require('./note_cache');
async function searchForNotes(searchString) {
const filters = parseFilters(searchString);
const noteIds = await searchForNoteIds(searchString);
const {query, params} = buildSearchQuery(filters);
try {
return await repository.getEntities(query, params);
}
catch (e) {
log.error("Search failed for " + query);
throw e;
}
return await repository.getNotes(noteIds);
}
async function searchForNoteIds(searchString) {
@@ -25,7 +17,21 @@ async function searchForNoteIds(searchString) {
const {query, params} = buildSearchQuery(filters, 'notes.noteId');
try {
return await sql.getColumn(query, params);
const noteIds = await sql.getColumn(query, params);
const availableNoteIds = noteIds.filter(noteCacheService.isAvailable);
const limitFilter = filters.find(filter => filter.name.toLowerCase() === 'limit');
if (limitFilter) {
const limit = parseInt(limitFilter.value);
return availableNoteIds.splice(0, limit);
}
else {
return availableNoteIds;
}
}
catch (e) {
log.error("Search failed for " + query);