2018-03-24 11:08:29 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const sql = require('../../services/sql');
|
2019-02-16 04:21:26 +08:00
|
|
|
const utils = require('../../services/utils');
|
2018-04-02 09:27:46 +08:00
|
|
|
const noteService = require('../../services/notes');
|
2018-06-06 07:12:52 +08:00
|
|
|
const noteCacheService = require('../../services/note_cache');
|
2018-03-24 11:08:29 +08:00
|
|
|
const parseFilters = require('../../services/parse_filters');
|
|
|
|
const buildSearchQuery = require('../../services/build_search_query');
|
|
|
|
|
2018-03-31 05:29:13 +08:00
|
|
|
async function searchNotes(req) {
|
2018-04-01 21:59:44 +08:00
|
|
|
const {labelFilters, searchText} = parseFilters(req.params.searchString);
|
2018-03-24 11:08:29 +08:00
|
|
|
|
2018-06-04 08:42:25 +08:00
|
|
|
let labelFiltersNoteIds = null;
|
2018-03-24 11:08:29 +08:00
|
|
|
|
2018-06-04 08:42:25 +08: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-13 04:28:38 +08:00
|
|
|
searchTextResults = await noteCacheService.findNotes(searchText);
|
2018-06-04 08:42:25 +08:00
|
|
|
|
|
|
|
let fullTextNoteIds = await getFullTextResults(searchText);
|
|
|
|
|
|
|
|
for (const noteId of fullTextNoteIds) {
|
|
|
|
if (!searchTextResults.some(item => item.noteId === noteId)) {
|
2018-06-06 07:12:52 +08:00
|
|
|
const result = noteCacheService.getNotePath(noteId);
|
2018-06-04 08:42:25 +08:00
|
|
|
|
|
|
|
if (result) {
|
|
|
|
searchTextResults.push(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let results;
|
|
|
|
|
|
|
|
if (labelFiltersNoteIds && searchTextResults) {
|
2018-08-15 04:50:05 +08:00
|
|
|
results = searchTextResults.filter(item => labelFiltersNoteIds.includes(item.noteId));
|
2018-06-04 08:42:25 +08:00
|
|
|
}
|
|
|
|
else if (labelFiltersNoteIds) {
|
2018-06-06 07:12:52 +08:00
|
|
|
results = labelFiltersNoteIds.map(noteCacheService.getNotePath).filter(res => !!res);
|
2018-06-04 08:42:25 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
results = searchTextResults;
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getFullTextResults(searchText) {
|
|
|
|
const tokens = searchText.toLowerCase().split(" ");
|
|
|
|
const tokenSql = ["1=1"];
|
|
|
|
|
|
|
|
for (const token of tokens) {
|
2019-02-16 04:21:26 +08:00
|
|
|
const safeToken = utils.sanitizeSql(token);
|
|
|
|
|
|
|
|
tokenSql.push(`(title LIKE '%${safeToken}%' OR content LIKE '%${safeToken}%')`);
|
2018-06-04 08:42:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const noteIds = await sql.getColumn(`
|
|
|
|
SELECT DISTINCT noteId
|
2019-02-16 04:21:26 +08:00
|
|
|
FROM
|
|
|
|
notes
|
|
|
|
JOIN note_contents USING(noteId)
|
2018-06-04 08:42:25 +08:00
|
|
|
WHERE isDeleted = 0
|
2019-02-16 04:21:26 +08:00
|
|
|
AND notes.isProtected = 0
|
2018-08-15 04:50:05 +08:00
|
|
|
AND type IN ('text', 'code')
|
2018-06-04 08:42:25 +08:00
|
|
|
AND ${tokenSql.join(' AND ')}`);
|
2018-03-24 11:08:29 +08:00
|
|
|
|
2018-03-31 05:29:13 +08:00
|
|
|
return noteIds;
|
|
|
|
}
|
2018-03-24 11:08:29 +08:00
|
|
|
|
2018-03-31 05:29:13 +08:00
|
|
|
async function saveSearchToNote(req) {
|
2018-03-24 11:08:29 +08:00
|
|
|
const noteContent = {
|
|
|
|
searchString: req.params.searchString
|
|
|
|
};
|
|
|
|
|
2018-06-06 07:12:52 +08:00
|
|
|
const {note} = await noteService.createNote('root', req.params.searchString, noteContent, {
|
2018-03-24 11:08:29 +08:00
|
|
|
json: true,
|
2018-03-26 11:25:17 +08:00
|
|
|
type: 'search',
|
|
|
|
mime: "application/json"
|
2018-03-24 11:08:29 +08:00
|
|
|
});
|
|
|
|
|
2018-04-04 10:15:28 +08:00
|
|
|
return { noteId: note.noteId };
|
2018-03-31 05:29:13 +08:00
|
|
|
}
|
2018-03-24 11:08:29 +08:00
|
|
|
|
2018-03-31 05:29:13 +08:00
|
|
|
module.exports = {
|
|
|
|
searchNotes,
|
|
|
|
saveSearchToNote
|
|
|
|
};
|