* Find documents where any indexed term starts with the given prefix. * This is more efficient than searchPrefix for word-based prefix search. * * @param termPrefix The term prefix to search for. * @returns Set of element IDs with terms starting with the prefix.
(termPrefix: string)
| 222 | * @returns Set of element IDs with terms starting with the prefix. |
| 223 | */ |
| 224 | public searchTermPrefix(termPrefix: string): Set<ElementId> { |
| 225 | const result = new Set<ElementId>(); |
| 226 | |
| 227 | if (!termPrefix || termPrefix.length === 0) { |
| 228 | return result; |
| 229 | } |
| 230 | |
| 231 | // Normalize and stem the prefix |
| 232 | const normalizedPrefixes = extractTerms(termPrefix, { |
| 233 | ...this.#options, |
| 234 | stem: false, // Don't stem for prefix matching |
| 235 | }); |
| 236 | |
| 237 | if (normalizedPrefixes.length === 0) { |
| 238 | return result; |
| 239 | } |
| 240 | |
| 241 | const prefix = normalizedPrefixes[0]!; |
| 242 | |
| 243 | for (const [term, ids] of this.#invertedIndex) { |
| 244 | if (term.startsWith(prefix)) { |
| 245 | for (const id of ids) { |
| 246 | result.add(id); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return result; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Get the document IDs that contain ALL of the given terms (AND semantics). |
no test coverage detected