From dd9b7bbbb54efa8dff67237a9e5cc2bd04e42ac9 Mon Sep 17 00:00:00 2001 From: zadam Date: Mon, 11 Mar 2019 19:50:13 +0100 Subject: [PATCH] fix fulltext search --- src/routes/api/search.js | 22 +++------------------- src/services/note_fulltext.js | 4 ++-- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/src/routes/api/search.js b/src/routes/api/search.js index 2499b4254..40d5e959f 100644 --- a/src/routes/api/search.js +++ b/src/routes/api/search.js @@ -52,26 +52,10 @@ async function searchNotes(req) { } async function getFullTextResults(searchText) { - const tokens = searchText.toLowerCase().split(" "); - const tokenSql = ["1=1"]; + const safeSearchText = utils.sanitizeSql(searchText); - for (const token of tokens) { - const safeToken = utils.sanitizeSql(token); - - tokenSql.push(`(title LIKE '%${safeToken}%' OR content LIKE '%${safeToken}%')`); - } - - const noteIds = await sql.getColumn(` - SELECT DISTINCT noteId - FROM - notes - JOIN note_contents USING(noteId) - WHERE isDeleted = 0 - AND notes.isProtected = 0 - AND type IN ('text', 'code') - AND ${tokenSql.join(' AND ')}`); - - return noteIds; + return await sql.getColumn(`SELECT noteId FROM note_fulltext + WHERE note_fulltext MATCH '${safeSearchText}'`); } async function saveSearchToNote(req) { diff --git a/src/services/note_fulltext.js b/src/services/note_fulltext.js index 8d31b36df..44155d010 100644 --- a/src/services/note_fulltext.js +++ b/src/services/note_fulltext.js @@ -5,7 +5,7 @@ const html2plaintext = require('html2plaintext'); const noteIdQueue = []; async function updateNoteFulltext(note) { - if (note.isDeleted || note.isProtected || note.hasLabel('archived')) { + if (note.isDeleted || note.isProtected || await note.hasLabel('archived')) { await sql.execute(`DELETE FROM note_fulltext WHERE noteId = ?`, [note.noteId]); @@ -25,7 +25,7 @@ async function updateNoteFulltext(note) { } // optimistically try to update first ... - const res = await sql.execute(`UPDATE note_fulltext title = ?, titleHash = ?, content = ?, contentHash = ? WHERE noteId = ?`, [note.title, note.hash, content, contentHash, note.noteId]); + const res = await sql.execute(`UPDATE note_fulltext SET title = ?, titleHash = ?, content = ?, contentHash = ? WHERE noteId = ?`, [note.title, note.hash, content, contentHash, note.noteId]); // ... and insert only when the update did not work if (res.stmt.changes === 0) {