diff options
| author | julian laplace <julescarbon@gmail.com> | 2022-10-06 16:18:29 +0200 |
|---|---|---|
| committer | julian laplace <julescarbon@gmail.com> | 2022-10-06 16:18:29 +0200 |
| commit | c00f021e48f2414f3c94e18712a3565fe2b51803 (patch) | |
| tree | 92c7f2946353060cd012bb0e2f96f51787df74de | |
| parent | 4b610a6262bdefa777630f69af1f5d5043d61bcd (diff) | |
search is broken...
| -rw-r--r-- | bucky/search/search.js | 120 |
1 files changed, 66 insertions, 54 deletions
diff --git a/bucky/search/search.js b/bucky/search/search.js index 1a56f53..fb3bb2d 100644 --- a/bucky/search/search.js +++ b/bucky/search/search.js @@ -1,74 +1,86 @@ -var db = require('../db') -var bdb = require('./bdb')('search') -var STOPWORDS = require('./stopwords') +var db = require("../db"); +var bdb = require("./bdb")("search"); +var STOPWORDS = require("./stopwords"); -var wordRegexp = new RegExp("[^a-z0-9]+", 'g'); -function parse_terms (s) { - return s.toLowerCase().split(wordRegexp).filter((term) => !!term) +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 < b ? -1 : 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() + var row = bdb.get(term); + if (!row) return []; + var res = row.toString(); // console.log(res) - if (! res.length) return [] + if (!res.length) return []; var matches = res.split(",").map((s) => { - if (! s.length) return; - var partz = s.split(" ") + 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, - } - }) + }; + }); // console.log(matches) - return matches + return matches; } -function search (query, start, limit) { - 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 = [] +function search(query, start, limit) { + 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 = []; 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 + 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); }) - }) - 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 - }) + .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; + }); return { meta: { @@ -80,10 +92,10 @@ function search (query, start, limit) { total: total, }, results: results, - thread_ids: thread_ids, - comment_ids: comment_ids, - file_ids: file_ids, + thread_ids: thread_ids, + comment_ids: comment_ids, + file_ids: file_ids, }; } -module.exports = { search: search } +module.exports = { search: search }; |
