diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2017-12-10 04:42:16 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2017-12-10 04:42:16 +0100 |
| commit | a932b664db987f2cf9ceefe9bb56e43793470d5e (patch) | |
| tree | bd0722849164218e418cc032d5a8a50d6b106709 /bucky/search | |
| parent | 9978bb56fc2c56ad52930bde9bcaa561158a158a (diff) | |
style search. LOTTA styling
Diffstat (limited to 'bucky/search')
| -rw-r--r-- | bucky/search/bdb.js | 69 | ||||
| -rw-r--r-- | bucky/search/middleware.js | 51 | ||||
| -rw-r--r-- | bucky/search/search.js | 20 | ||||
| -rw-r--r-- | bucky/search/snippet.js | 47 |
4 files changed, 122 insertions, 65 deletions
diff --git a/bucky/search/bdb.js b/bucky/search/bdb.js index ba0124d..a7ced4a 100644 --- a/bucky/search/bdb.js +++ b/bucky/search/bdb.js @@ -1,38 +1,55 @@ var bdb_lib = require('berkeleydb') var dbenv = new bdb_lib.DbEnv(); var bdb_status = dbenv.open('./search/db/env') -console.log('open /search/db:', bdb_status) - -var db - -function exitHandler(options, err) { - db.close() - // if (options.cleanup) console.log('clean'); - if (err) console.log(err.stack); - if (options.exit) process.exit(); +if (bdb_status) { + console.log('open dbenv failed:', bdb_status) + process.exit() } -// do something when app is closing -process.on('exit', exitHandler.bind(null, {cleanup: true})); +function db(fn){ + var db + fn = "./" + fn + ".db" -// catches ctrl+c event -process.on('SIGINT', exitHandler.bind(null, {exit: true})); + function exitHandler(options, err) { + db.close() + // if (options.cleanup) console.log('clean'); + if (err) console.log(err.stack); + if (options.exit) process.exit(); + } -// catches "kill pid" (for example: nodemon restart) -process.on('SIGUSR1', exitHandler.bind(null, {exit: true})); -process.on('SIGUSR2', exitHandler.bind(null, {exit: true})); + // do something when app is closing + process.on('exit', exitHandler.bind(null, {cleanup: true})); -//catches uncaught exceptions -process.on('uncaughtException', exitHandler.bind(null, {exit:true})); + // catches ctrl+c event + process.on('SIGINT', exitHandler.bind(null, {exit: true})); -function open(){ - if (db) db.close() - var _db = new bdb_lib.Db(dbenv); - var bdb_status = _db.open('./search.db') - console.log('open ./search.db:', bdb_status) - db = _db -} + // catches "kill pid" (for example: nodemon restart) + process.on('SIGUSR1', exitHandler.bind(null, {exit: true})); + process.on('SIGUSR2', exitHandler.bind(null, {exit: true})); -open() + //catches uncaught exceptions + process.on('uncaughtException', exitHandler.bind(null, {exit:true})); + function open(fn){ + if (db) db.close() + var _db = new bdb_lib.Db(dbenv); + var bdb_status = _db.open(fn) + if (bdb_status) { + console.log('open ' + fn + ' failed:', bdb_status) + process.exit() + } + db = _db + } + + open(fn) + + return { + put: function(term, serialized){ + db.put(term, serialized) + }, + get: function(term){ + return db.get(term) + }, + } +} module.exports = db diff --git a/bucky/search/middleware.js b/bucky/search/middleware.js index 39d7a71..32e3321 100644 --- a/bucky/search/middleware.js +++ b/bucky/search/middleware.js @@ -6,16 +6,26 @@ module.exports = { search: function (req, res, next) { res.search = search.search(req.query.query, req.query.start, req.query.limit) - console.log(res.search) + if (! res.search) { + res.sendStatus(400) + return + } next() }, getThreads: function (req, res, next){ var thread_ids = res.search.thread_ids; if (! thread_ids || ! thread_ids.length) { + res.search.threads = [] return next() } db.getThreadsById(thread_ids).then(function(threads){ + threads.forEach((thread) => { + var flag_id = thread.get('flagged') + if (flag_id) { + res.search.file_ids.push(flag_id) + } + }) res.search.threads = threads next() }) @@ -24,11 +34,14 @@ module.exports = { getComments: function (req, res, next){ var comment_ids = res.search.comment_ids; if (! comment_ids || ! comment_ids.length) { + res.search.comments = [] return next() } db.getCommentsById(comment_ids).then(function(comments){ + var terms = res.search.meta.terms comments.forEach(function(comment){ - comment.set('comment', comment.get('comment').toString()) + const snip = snippet(comment.get('comment').toString(), terms) + comment.set('comment', snip) }) res.search.comments = comments next() @@ -38,6 +51,7 @@ module.exports = { getFiles: function (req, res, next){ var file_ids = res.search.file_ids if (! file_ids || ! file_ids.length) { + res.search.files = [] return next() } db.getFilesById(file_ids).then(function(files){ @@ -52,7 +66,38 @@ module.exports = { }, success: function(req, res, next){ - res.send(res.search) + var terms = res.search.meta.terms + var threads = {}, comments = {}, files = {} + res.search.threads.forEach((t) => { threads[t.id] = t }) + res.search.comments.forEach((t) => { comments[t.id] = t }) + res.search.files.forEach((t) => { files[t.id] = t }) + var results = res.search.results.map((r) => { + var m = {} + m.thread = threads[r.thread] + m.comment = comments[r.comment] + m.file = files[r.file] + m.count = r.count + m.strength = r.strength + if (m.thread) { + var flagged = m.thread.get('flagged') + if (flagged) { + m.thread.set('flagged', files[flagged]) + } + var allowed = m.thread.get('allowed') + if (allowed) { + m.thread.set('allowed', allowed.toString().split(" ")) + } + var display = m.thread.get('display') + if (display) { + m.thread.set('display', display.toString().split(" ")) + } + } + return m + }) + res.send({ + meta: res.search.meta, + results: results, + }) }, } diff --git a/bucky/search/search.js b/bucky/search/search.js index a28d49c..4818ef9 100644 --- a/bucky/search/search.js +++ b/bucky/search/search.js @@ -1,5 +1,5 @@ var db = require('../db') -var bdb = require('./bdb') +var bdb = require('./bdb')('search') var STOPWORDS = require('./stopwords') var wordRegexp = new RegExp("(\W+)"); @@ -28,7 +28,7 @@ function find_term(term) { strength: parseInt(partz[3]) || 1, } }) - console.log(matches) + // console.log(matches) return matches } @@ -69,7 +69,7 @@ function search (query, start, limit) { if (i < start) return false if (to_display-- === 0) return true results.push(match) - console.log(match) + // console.log(match) thread_ids.push(match.thread) if (match.comment) comment_ids.push(match.comment) if (match.file) file_ids.push(match.file) @@ -77,16 +77,18 @@ function search (query, start, limit) { }) return { - query: query, - start: start, - next: start + limit, - limit: limit, - total: total, + meta: { + query: query, + terms: terms, + start: start, + next: start + limit, + limit: limit, + total: total, + }, results: results, thread_ids: thread_ids, comment_ids: comment_ids, file_ids: file_ids, - terms: terms, }; } diff --git a/bucky/search/snippet.js b/bucky/search/snippet.js index cd0657f..17988d2 100644 --- a/bucky/search/snippet.js +++ b/bucky/search/snippet.js @@ -1,19 +1,11 @@ var util = require('../util/util') var STOPWORDS = require('./stopwords') -function bold_snippet(s, terms) { - return bold_terms(snippet(s, terms), terms) -} -function bold_terms (s, terms) { - s = util.sanitize(s) - terms.forEach( (term) => { - s.replace(new RegExp("\b" + term + "\b", "i"), "<b>" + term + "</b>") - }) -} function snippet(s, terms) { s = util.sanitize(s) - var term_re = new RegExp("\b(" + terms.join("|") + ")\b", "i") - var words = s.split(/\s+/) + var term_set = new Set(terms) + + var words = s.split(/[^a-zA-Z0-9]+/) var snippet = ""; // deduper for matching @words indexes, so we don't add a word twice @@ -26,27 +18,30 @@ function snippet(s, terms) { var aggr = 0; // amount of context to show, in number of words surrounding a match - var $pad = 4; + var pad = 10; // loop over each of the words in the string - words.some((word, i) => { + var word + for (var i = 0, len = words.length; i < len; i++) { + word = words[i] + // if the word matches... - if (term_re.match(word) && ! STOPWORDS.has(word.toLowerCase())) { + if (term_set.has(word.toLowerCase()) && ! STOPWORDS.has(word.toLowerCase())) { // if we aren't already aggregating, add an ellipsis - if (! $aggr) { + if (! aggr) { words_matched.push("...") } - + // look backward $pad words var idx; - for (var j = -pad; j < 1; j++) { + INNER: for (var j = -pad; j < 1; j++) { // create a new index from the offset idx = i + j; // is this a valid index? has it already been encountered? - if (idx < 0) continue; - if (idx > words.length) continue; - if (index_matches[idx]) continue; + if (idx < 0) continue INNER; + if (idx > words.length) continue INNER; + if (index_matches[idx]) continue INNER; // checks out, save this word words_matched.push(words[idx]) @@ -69,20 +64,18 @@ function snippet(s, terms) { // one less word to aggregate aggr--; } - + // keep snippets to a modest length - return words_matched.length > 30; - }) + if (words_matched.length > 30) break + } // add a trailing ellipsis words_matched.push("...") // create the snippet from the saved context words snippet = words_matched.join(" ") - + return snippet } -module.exports = { - bold_snippet, bold_terms, snippet, -}
\ No newline at end of file +module.exports = snippet
\ No newline at end of file |
