1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-15 23:00:36 +02:00

Finished search engine integration with desktop app

This commit is contained in:
Laurent Cozic
2018-12-13 23:57:14 +01:00
parent 5ec7c16e3e
commit 061ce646d2
6 changed files with 121 additions and 34 deletions

View File

@ -101,11 +101,19 @@ class SearchEngine {
// https://stackoverflow.com/a/13818704/561309
queryTermToRegex(term) {
while (term.length && term.indexOf('*') === 0) {
term = term.substr(1);
}
const preg_quote = (str, delimiter) => {
return (str + '').replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&');
} // [^ \t,\.,\+\-\\*?!={}<>\|:"\'\(\)[\]]
let regexString = preg_quote(term);
if (regexString[regexString.length - 1] === '*') {
regexString = regexString.substr(0, regexString.length - 2) + '[^' + preg_quote(' \t\n\r,.,+-*?!={}<>|:"\'()[]') + ']' + '*';
}
const regexString = preg_quote(term).replace(/\\\*/g, '.*').replace(/\\\?/g, '.');
return new RegExp(regexString, 'gmi');
return regexString;
}
parseQuery(query) {
@ -173,7 +181,7 @@ class SearchEngine {
}
if (term.indexOf('*') >= 0) {
terms[col][i] = this.queryTermToRegex(term);
terms[col][i] = { type: 'regex', value: this.queryTermToRegex(term) };
}
}
@ -189,6 +197,17 @@ class SearchEngine {
};
}
allParsedQueryTerms(parsedQuery) {
if (!parsedQuery || !parsedQuery.termCount) return [];
let output = [];
for (let col in parsedQuery.terms) {
if (!parsedQuery.terms.hasOwnProperty(col)) continue;
output = output.concat(parsedQuery.terms[col]);
}
return output;
}
async search(query) {
const parsedQuery = this.parseQuery(query);
const sql = 'SELECT id, title, offsets(notes_fts) AS offsets FROM notes_fts WHERE notes_fts MATCH ?'