diff options
Diffstat (limited to 'bucky/search/search.js')
| -rw-r--r-- | bucky/search/search.js | 40 |
1 files changed, 20 insertions, 20 deletions
diff --git a/bucky/search/search.js b/bucky/search/search.js index 1d06aea..a28d49c 100644 --- a/bucky/search/search.js +++ b/bucky/search/search.js @@ -12,33 +12,32 @@ function parse_terms (s) { return false }) } -function cmp (a,b){ return (a<b)?a:(a===b)?0:1 } +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) + // console.log(res) if (! res.length) return [] var matches = res.split(",").map((s) => { if (! s.length) return; - console.log(s) var partz = s.split(" ") return { - thread: s[0], - comment: s[1], - file: s[2], - strength: s[3], + thread: parseInt(partz[0]), + comment: parseInt(partz[1]), + file: parseInt(partz[2]), + strength: parseInt(partz[3]) || 1, } }) + console.log(matches) return matches } function search (query, start, limit) { if (!query) return - start = start || 0; - limit = limit || 10; + start = parseInt(start) || 0; + limit = parseInt(limit) || 10; var scores = {}; var terms = parse_terms(query); - var i = 0 var total var to_display = limit var threads = {} @@ -53,23 +52,24 @@ function search (query, start, limit) { if (!results) return; results.forEach((result) => { var score = scores[result.thread] = scores[result.thread] || { count: 0, strength: 0 } - score.thread = score.thread || result.thread - score.comment = score.comment || result.comment - score.file = score.file || result.file + score.thread = score.thread || parseInt(result.thread) + score.comment = score.comment || parseInt(result.comment) + score.file = score.file || parseInt(result.file) score.strength += result.strength score.count += 1 }) }) total = Object.keys(scores).length - Object.values(scores).sort((a,b) => { - if (b.count !== a.count) { - return cmp(b.count, a.count) - } - return cmp(b.strength * b.count, a.strength * a.count) - }).some((match) => { - if (i++ < start) return false + 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) |
