var db = require('../db') //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'); function parse_terms (s) { return s.toLowerCase().split(wordRegexp).filter((term) => !!term) } function cmp (a,b){ return (a { if (! s.length) return; var partz = s.split(" ") return { thread: parseInt(partz[0]), comment: parseInt(partz[1]), file: parseInt(partz[2]), strength: parseInt(partz[3]) || 1, } }) return matches } function search (query, start, limit, cb) { if (!query) return start = parseInt(start) || 0; limit = parseInt(limit) || 10; var scores = {}; var terms = parse_terms(query); var total var to_display = limit var threads = {} var thread_ids = [] var comment_ids = [] var file_ids = [] var results = [] var promise_terms = terms.map((term) => { return new Promise((resolve, reject) => { if (STOPWORDS.has(term)) return; redisClient.get(term, function(err, results){ if (err) { reject(err) } if (!results) return resolve(); 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 }) resolve() }) }) }) Promise.all( promise_terms) .then(() => { 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) thread_ids.push(match.thread) if (match.comment) comment_ids.push(match.comment) if (match.file) file_ids.push(match.file) return false }) 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, }); }) } module.exports = { search: search }