2018-03-23 23:08:29 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const sql = require('../../services/sql');
|
2019-02-15 21:21:26 +01:00
|
|
|
const utils = require('../../services/utils');
|
2018-04-01 21:27:46 -04:00
|
|
|
const noteService = require('../../services/notes');
|
2018-06-05 19:12:52 -04:00
|
|
|
const noteCacheService = require('../../services/note_cache');
|
2018-03-23 23:08:29 -04:00
|
|
|
const parseFilters = require('../../services/parse_filters');
|
|
|
|
const buildSearchQuery = require('../../services/build_search_query');
|
|
|
|
|
2018-03-30 17:29:13 -04:00
|
|
|
async function searchNotes(req) {
|
2018-04-01 09:59:44 -04:00
|
|
|
const {labelFilters, searchText} = parseFilters(req.params.searchString);
|
2018-03-23 23:08:29 -04:00
|
|
|
|
2018-06-03 20:42:25 -04:00
|
|
|
let labelFiltersNoteIds = null;
|
2018-03-23 23:08:29 -04:00
|
|
|
|
2018-06-03 20:42:25 -04:00
|
|
|
if (labelFilters.length > 0) {
|
|
|
|
const {query, params} = buildSearchQuery(labelFilters, searchText);
|
|
|
|
|
|
|
|
labelFiltersNoteIds = await sql.getColumn(query, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
let searchTextResults = null;
|
|
|
|
|
|
|
|
if (searchText.trim().length > 0) {
|
2018-12-12 21:28:38 +01:00
|
|
|
searchTextResults = await noteCacheService.findNotes(searchText);
|
2018-06-03 20:42:25 -04:00
|
|
|
|
|
|
|
let fullTextNoteIds = await getFullTextResults(searchText);
|
|
|
|
|
|
|
|
for (const noteId of fullTextNoteIds) {
|
|
|
|
if (!searchTextResults.some(item => item.noteId === noteId)) {
|
2018-06-05 19:12:52 -04:00
|
|
|
const result = noteCacheService.getNotePath(noteId);
|
2018-06-03 20:42:25 -04:00
|
|
|
|
|
|
|
if (result) {
|
|
|
|
searchTextResults.push(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let results;
|
|
|
|
|
|
|
|
if (labelFiltersNoteIds && searchTextResults) {
|
2018-08-14 22:50:05 +02:00
|
|
|
results = searchTextResults.filter(item => labelFiltersNoteIds.includes(item.noteId));
|
2018-06-03 20:42:25 -04:00
|
|
|
}
|
|
|
|
else if (labelFiltersNoteIds) {
|
2018-06-05 19:12:52 -04:00
|
|
|
results = labelFiltersNoteIds.map(noteCacheService.getNotePath).filter(res => !!res);
|
2018-06-03 20:42:25 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
results = searchTextResults;
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getFullTextResults(searchText) {
|
2019-03-11 19:50:13 +01:00
|
|
|
const safeSearchText = utils.sanitizeSql(searchText);
|
2018-06-03 20:42:25 -04:00
|
|
|
|
2019-03-11 19:50:13 +01:00
|
|
|
return await sql.getColumn(`SELECT noteId FROM note_fulltext
|
|
|
|
WHERE note_fulltext MATCH '${safeSearchText}'`);
|
2018-03-30 17:29:13 -04:00
|
|
|
}
|
2018-03-23 23:08:29 -04:00
|
|
|
|
2018-03-30 17:29:13 -04:00
|
|
|
async function saveSearchToNote(req) {
|
2018-03-23 23:08:29 -04:00
|
|
|
const noteContent = {
|
|
|
|
searchString: req.params.searchString
|
|
|
|
};
|
|
|
|
|
2018-06-05 19:12:52 -04:00
|
|
|
const {note} = await noteService.createNote('root', req.params.searchString, noteContent, {
|
2018-03-23 23:08:29 -04:00
|
|
|
json: true,
|
2018-03-25 23:25:17 -04:00
|
|
|
type: 'search',
|
|
|
|
mime: "application/json"
|
2018-03-23 23:08:29 -04:00
|
|
|
});
|
|
|
|
|
2018-04-03 22:15:28 -04:00
|
|
|
return { noteId: note.noteId };
|
2018-03-30 17:29:13 -04:00
|
|
|
}
|
2018-03-23 23:08:29 -04:00
|
|
|
|
2018-03-30 17:29:13 -04:00
|
|
|
module.exports = {
|
|
|
|
searchNotes,
|
|
|
|
saveSearchToNote
|
|
|
|
};
|