Notes/src/routes/api/search.js

134 lines
3.6 KiB
JavaScript
Raw Normal View History

"use strict";
2021-06-29 22:15:57 +02:00
const becca = require('../../becca/becca');
2021-05-11 22:00:16 +02:00
const SearchContext = require('../../services/search/search_context');
2020-08-06 23:55:17 +02:00
const searchService = require('../../services/search/services/search');
2022-06-05 23:36:46 +02:00
const bulkActionService = require("../../services/bulk_actions");
const cls = require("../../services/cls");
2021-05-11 22:00:16 +02:00
const {formatAttrForSearch} = require("../../services/attribute_formatter");
const ValidationError = require("../../errors/validation_error");
function searchFromNote(req) {
const note = becca.getNoteOrThrow(req.params.noteId);
2023-06-05 09:23:42 +02:00
if (!note) {
// this can be triggered from recent changes, and it's harmless to return an empty list rather than fail
return [];
}
if (note.type !== 'search') {
throw new ValidationError(`Note '${req.params.noteId}' is not a search note.`);
}
return searchService.searchFromNote(note);
}
2022-06-08 22:25:00 +02:00
function searchAndExecute(req) {
const note = becca.getNoteOrThrow(req.params.noteId);
2023-06-05 09:23:42 +02:00
if (!note) {
// this can be triggered from recent changes, and it's harmless to return an empty list rather than fail
return [];
}
if (note.type !== 'search') {
throw new ValidationError(`Note '${req.params.noteId}' is not a search note.`);
}
const {searchResultNoteIds} = searchService.searchFromNote(note);
2022-06-05 23:36:46 +02:00
bulkActionService.executeActions(note, searchResultNoteIds);
}
2021-01-31 22:45:45 +01:00
function quickSearch(req) {
const {searchString} = req.params;
const searchContext = new SearchContext({
fastSearch: false,
includeArchivedNotes: false,
fuzzyAttributeSearch: false
});
2022-12-17 11:58:30 +01:00
const resultNoteIds = searchService.findResultsWithQuery(searchString, searchContext)
2021-01-31 22:45:45 +01:00
.map(sr => sr.noteId);
2022-12-17 11:58:30 +01:00
return {
searchResultNoteIds: resultNoteIds,
error: searchContext.getError()
};
2021-01-31 22:45:45 +01:00
}
function search(req) {
const {searchString} = req.params;
const searchContext = new SearchContext({
fastSearch: false,
includeArchivedNotes: true,
fuzzyAttributeSearch: false,
ignoreHoistedNote: true
});
2021-04-17 20:52:46 +02:00
return searchService.findResultsWithQuery(searchString, searchContext)
.map(sr => sr.noteId);
}
2020-06-25 23:56:06 +02:00
function getRelatedNotes(req) {
const attr = req.body;
2020-09-13 22:23:03 +02:00
const searchSettings = {
fastSearch: true,
includeArchivedNotes: false,
2020-09-13 22:23:03 +02:00
fuzzyAttributeSearch: false
};
2021-04-17 20:52:46 +02:00
const matchingNameAndValue = searchService.findResultsWithQuery(formatAttrForSearch(attr, true), new SearchContext(searchSettings));
const matchingName = searchService.findResultsWithQuery(formatAttrForSearch(attr, false), new SearchContext(searchSettings));
2020-06-25 23:56:06 +02:00
const results = [];
const allResults = matchingNameAndValue.concat(matchingName);
const allResultNoteIds = new Set();
for (const record of allResults) {
allResultNoteIds.add(record.noteId);
}
for (const record of allResults) {
2020-06-25 23:56:06 +02:00
if (results.length >= 20) {
break;
}
if (results.find(res => res.noteId === record.noteId)) {
continue;
}
results.push(record);
}
return {
count: allResultNoteIds.size,
2020-06-25 23:56:06 +02:00
results
};
}
2022-05-31 22:45:57 +02:00
function searchTemplates() {
const query = cls.getHoistedNoteId() === 'root'
? '#template'
: '#template OR #workspaceTemplate';
2022-05-31 22:45:57 +02:00
return searchService.searchNotes(query, {
includeArchivedNotes: true,
ignoreHoistedNote: false
}).map(note => note.noteId);
}
module.exports = {
2020-06-25 23:56:06 +02:00
searchFromNote,
searchAndExecute,
2021-01-31 22:45:45 +01:00
getRelatedNotes,
quickSearch,
2022-05-31 22:45:57 +02:00
search,
searchTemplates
2020-05-16 23:12:29 +02:00
};