| 27 | * - Contains search (CONTAINS) |
| 28 | */ |
| 29 | export class FullTextIndex implements Index { |
| 30 | public readonly type = "fulltext" as const; |
| 31 | public readonly label: string; |
| 32 | public readonly property: string; |
| 33 | |
| 34 | /** |
| 35 | * Matcher options for text-search. |
| 36 | */ |
| 37 | #options: MatcherOptions; |
| 38 | |
| 39 | /** |
| 40 | * Inverted index: term -> Set<ElementId> |
| 41 | */ |
| 42 | #invertedIndex: Map<string, Set<ElementId>> = new Map(); |
| 43 | |
| 44 | /** |
| 45 | * Document store: elementId -> original text |
| 46 | */ |
| 47 | #documents: Map<ElementId, string> = new Map(); |
| 48 | |
| 49 | public constructor(label: string, property: string, options?: MatcherOptions) { |
| 50 | this.label = label; |
| 51 | this.property = property; |
| 52 | this.#options = options ?? {}; |
| 53 | } |
| 54 | |
| 55 | public add(elementId: ElementId, value: unknown): void { |
| 56 | if (typeof value !== "string" || value.length === 0) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | // Store the original document |
| 61 | this.#documents.set(elementId, value); |
| 62 | |
| 63 | // Extract and index terms |
| 64 | const terms = extractTerms(value, this.#options); |
| 65 | |
| 66 | for (const term of terms) { |
| 67 | let ids = this.#invertedIndex.get(term); |
| 68 | if (!ids) { |
| 69 | ids = new Set(); |
| 70 | this.#invertedIndex.set(term, ids); |
| 71 | } |
| 72 | ids.add(elementId); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public remove(elementId: ElementId, _value: unknown): void { |
| 77 | const storedText = this.#documents.get(elementId); |
| 78 | if (!storedText) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | // Remove from inverted index |
| 83 | const terms = extractTerms(storedText, this.#options); |
| 84 | |
| 85 | for (const term of terms) { |
| 86 | const ids = this.#invertedIndex.get(term); |
nothing calls this directly
no outgoing calls
no test coverage detected