( q: string, limit = 20 )
| 79 | } |
| 80 | |
| 81 | function query( |
| 82 | q: string, |
| 83 | limit = 20 |
| 84 | ): SearchResult[] { |
| 85 | const tokens = tokenize(q); |
| 86 | if (!tokens.length) return []; |
| 87 | |
| 88 | // Score by how many query tokens appear |
| 89 | const scores = new Map<number, number>(); |
| 90 | for (const token of tokens) { |
| 91 | for (const [term, set] of index) { |
| 92 | if (term.includes(token)) { |
| 93 | const boost = term === token ? 2 : 1; // exact > partial |
| 94 | for (const idx of set) { |
| 95 | scores.set(idx, (scores.get(idx) ?? 0) + boost); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return Array.from(scores.entries()) |
| 102 | .sort((a, b) => b[1] - a[1]) |
| 103 | .slice(0, limit) |
| 104 | .map(([idx, score]) => { |
| 105 | const entry = entries[idx]; |
| 106 | return { |
| 107 | conversationId: entry.conversationId, |
| 108 | messageId: entry.messageId, |
| 109 | snippet: extractSnippet(entry.text, q), |
| 110 | score, |
| 111 | createdAt: entry.createdAt, |
| 112 | }; |
| 113 | }) |
| 114 | .filter((r) => r.conversationId !== "__removed__"); |
| 115 | } |
| 116 | |
| 117 | self.addEventListener("message", (e: MessageEvent<InMessage>) => { |
| 118 | const msg = e.data; |
no test coverage detected