|
| 1 | +/** |
| 2 | + * Full-text search abstraction. |
| 3 | + * |
| 4 | + * SQLite implementations use FTS5 virtual tables. |
| 5 | + * Postgres implementations use tsvector columns with GIN indexes. |
| 6 | + * Methods return SQL strings — no database calls. |
| 7 | + */ |
| 8 | +export interface IFullTextSearch { |
| 9 | + /** |
| 10 | + * Generate DDL to create the full-text search index. |
| 11 | + * |
| 12 | + * @param config.table - Name for the FTS index/virtual table. |
| 13 | + * @param config.columns - Columns to index. |
| 14 | + * @param config.contentTable - Source table (for external-content FTS5). |
| 15 | + * @param config.tokenizer - Tokenizer config (e.g. 'porter ascii'). |
| 16 | + */ |
| 17 | + createIndex(config: { |
| 18 | + table: string; |
| 19 | + columns: string[]; |
| 20 | + contentTable?: string; |
| 21 | + tokenizer?: string; |
| 22 | + }): string; |
| 23 | + |
| 24 | + /** |
| 25 | + * Generate a WHERE clause fragment for full-text matching. |
| 26 | + * SQLite: `memory_traces_fts MATCH ?` |
| 27 | + * Postgres: `memory_traces._tsv @@ plainto_tsquery('english', $1)` |
| 28 | + */ |
| 29 | + matchClause(indexName: string, queryPlaceholder: string): string; |
| 30 | + |
| 31 | + /** |
| 32 | + * Generate an ORDER BY rank expression. |
| 33 | + * SQLite: `memory_traces_fts.rank` |
| 34 | + * Postgres: `ts_rank(memory_traces._tsv, plainto_tsquery('english', $1))` |
| 35 | + */ |
| 36 | + rankExpression(indexName: string, queryPlaceholder?: string): string; |
| 37 | + |
| 38 | + /** |
| 39 | + * Generate the rebuild/reindex command. |
| 40 | + * SQLite: `INSERT INTO fts_table(fts_table) VALUES('rebuild')` |
| 41 | + * Postgres: `UPDATE content_table SET _tsv = to_tsvector('english', col1 || ' ' || col2)` |
| 42 | + */ |
| 43 | + rebuildCommand(indexName: string): string; |
| 44 | + |
| 45 | + /** |
| 46 | + * Generate an INSERT to sync external-content FTS after a row insert. |
| 47 | + * SQLite: `INSERT INTO fts_table (rowid, col1, col2) VALUES (expr, ?, ?)` |
| 48 | + * Postgres: `UPDATE content_table SET _tsv = to_tsvector(...) WHERE ...` |
| 49 | + */ |
| 50 | + syncInsert(indexName: string, rowIdExpr: string, columns: string[]): string; |
| 51 | + |
| 52 | + /** |
| 53 | + * Sanitize natural-language input into a safe search query. |
| 54 | + * SQLite: wraps words in quotes, strips FTS5 operators. |
| 55 | + * Postgres: pass-through (plainto_tsquery handles it). |
| 56 | + */ |
| 57 | + sanitizeQuery(input: string): string; |
| 58 | + |
| 59 | + /** |
| 60 | + * Generate a SELECT joining the FTS index to the content table. |
| 61 | + * This handles the structural difference between FTS5 (separate virtual table |
| 62 | + * joined via rowid) and Postgres (tsvector column on the content table itself). |
| 63 | + * |
| 64 | + * @param contentTable - The base table (e.g. 'memory_traces'). |
| 65 | + * @param contentAlias - Alias for the content table (e.g. 't'). |
| 66 | + * @param ftsAlias - Alias for the FTS table/column (e.g. 'fts'). |
| 67 | + * @param indexName - FTS index/virtual table name. |
| 68 | + * @returns FROM/JOIN clause fragment. |
| 69 | + */ |
| 70 | + joinClause(contentTable: string, contentAlias: string, ftsAlias: string, indexName: string): string; |
| 71 | +} |
0 commit comments