MCPcopy Create free account
hub / github.com/ChatLab/ChatLab / searchByFts

Function searchByFts

packages/node-runtime/src/fts/index.ts:111–140  ·  view source on GitHub ↗
(
  db: DatabaseAdapter,
  keywords: string[],
  limit = 1000,
  offset = 0
)

Source from the content-addressed store, hash-verified

109 * Search messages using FTS5, returning matching rowids.
110 */
111export function searchByFts(
112 db: DatabaseAdapter,
113 keywords: string[],
114 limit = 1000,
115 offset = 0
116): { rowids: number[]; total: number } {
117 if (keywords.length === 0) return { rowids: [], total: 0 }
118
119 const matchQuery = tokenizeQueryForFts(keywords)
120 if (!matchQuery) return { rowids: [], total: 0 }
121
122 try {
123 const countRow = db.prepare('SELECT COUNT(*) as total FROM message_fts WHERE content MATCH ?').get(matchQuery) as
124 | { total: number }
125 | undefined
126 const total = countRow?.total ?? 0
127
128 const rows = db
129 .prepare(`SELECT rowid FROM message_fts WHERE content MATCH ? ORDER BY rank LIMIT ? OFFSET ?`)
130 .all(matchQuery, limit, offset) as Array<{ rowid: number }>
131
132 return {
133 rowids: rows.map((r) => r.rowid),
134 total,
135 }
136 } catch (error) {
137 console.error('[FTS] Search failed, query:', matchQuery, error)
138 return { rowids: [], total: 0 }
139 }
140}

Callers 2

searchFunction · 0.90
searchFtsFunction · 0.90

Calls 5

tokenizeQueryForFtsFunction · 0.90
getMethod · 0.65
prepareMethod · 0.65
allMethod · 0.65
errorMethod · 0.65

Tested by

no test coverage detected