diff options
Diffstat (limited to 'bucky/search/search.js')
| -rw-r--r-- | bucky/search/search.js | 113 |
1 files changed, 63 insertions, 50 deletions
diff --git a/bucky/search/search.js b/bucky/search/search.js index 1a56f53..bde1ad9 100644 --- a/bucky/search/search.js +++ b/bucky/search/search.js @@ -1,5 +1,8 @@ var db = require('../db') -var bdb = require('./bdb')('search') +var eachSeries = require('async/eachSeries'); +var redisClient = require('./redis-client.js') +const { promisify } = require("util"); +const lookupAsync = promisify(redisClient.get).bind(redisClient); var STOPWORDS = require('./stopwords') var wordRegexp = new RegExp("[^a-z0-9]+", 'g'); @@ -8,11 +11,9 @@ function parse_terms (s) { } function cmp (a,b){ return (a<b)?-1:(a===b)?0:1 } -function find_term(term) { - var res = bdb.get(term).toString() - // console.log(res) - if (! res.length) return [] - var matches = res.split(",").map((s) => { +function split_results(data) { + if (! data.length) return [] + var matches = data.split(",").map((s) => { if (! s.length) return; var partz = s.split(" ") return { @@ -22,11 +23,11 @@ function find_term(term) { strength: parseInt(partz[3]) || 1, } }) - // console.log(matches) return matches } -function search (query, start, limit) { +function search (query, start, limit, cb) { + if (!query) return start = parseInt(start) || 0; limit = parseInt(limit) || 10; @@ -40,50 +41,62 @@ function search (query, start, limit) { var file_ids = [] var results = [] - terms.forEach((term) => { - if (STOPWORDS.has(term)) return; - var results = find_term(term); - if (!results) return; - results.forEach((result) => { - var score = scores[result.thread] = scores[result.thread] || { count: 0, strength: 0 } - score.thread = score.thread || parseInt(result.thread) - score.comment = score.comment || parseInt(result.comment) - score.file = score.file || parseInt(result.file) - score.strength += parseFloat(result.strength) - score.count += 1 - }) - }) - total = Object.keys(scores).length - Object.values(scores).sort((b,a) => { - // if (a.count !== b.count) { - // return cmp(a.count, b.count) - // } - return cmp(a.strength, b.strength) - }).some((match, i) => { - if (i < start) return false - if (to_display-- === 0) return true - results.push(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) - return false + + console.log(terms) + eachSeries( + terms, + function(term, callback){ + console.log(term); + if (STOPWORDS.has(term)) return; + console.log("howdy") + redisClient.get(term, function(err, results){ +// if (!results) return callback(); + + results = split_results(results) + results.forEach((result) => { + //this is a reference, mutating scores + var score = scores[result.thread] = scores[result.thread] || { count: 0, strength: 0 } + score.thread = score.thread || parseInt(result.thread) + score.comment = score.comment || parseInt(result.comment) + score.file = score.file || parseInt(result.file) + score.strength += parseFloat(result.strength) + score.count += 1 + }) + callback() + }) + }, + function() { + total = Object.keys(scores).length + Object.values(scores).sort((b,a) => { + return cmp(a.strength, b.strength) + }).some((match, i) => { + if (i < start) return false + if (to_display-- === 0) return true + results.push(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) + return false + }) + //console.log(results) + redisClient.quit() + cb( { + 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, + }); }) - return { - 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, - }; } module.exports = { search: search } |
