Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions src/store/DocumentStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,19 @@ export class DocumentStore {
const ftsQuery = this.escapeFtsQuery(query);
const normalizedVersion = version.toLowerCase();

// Resolve library/version to IDs upfront so we can pass them directly
// to the vec0 partition columns and avoid full-table scans.
const versionRow = this.statements.getVersionId.get(
library.toLowerCase(),
normalizedVersion,
) as { id: number; library_id: number } | undefined;

if (!versionRow) {
return [];
}

const { id: versionId, library_id: libraryId } = versionRow;

if (this.isVectorSearchEnabled) {
// Hybrid search: vector + full-text search with RRF ranking
const rawEmbedding = await this.embeddings.embedQuery(query);
Expand All @@ -1740,12 +1753,8 @@ export class DocumentStore {
dv.rowid as id,
dv.distance as vec_distance
FROM documents_vec dv
JOIN documents d ON dv.rowid = d.id
JOIN pages p ON d.page_id = p.id
JOIN versions v ON p.version_id = v.id
JOIN libraries l ON v.library_id = l.id
WHERE l.name = ?
AND COALESCE(v.name, '') = COALESCE(?, '')
WHERE dv.library_id = ?
AND dv.version_id = ?
AND dv.embedding MATCH ?
AND dv.k = ?
ORDER BY dv.distance
Expand All @@ -1757,10 +1766,7 @@ export class DocumentStore {
FROM documents_fts f
JOIN documents d ON f.rowid = d.id
JOIN pages p ON d.page_id = p.id
JOIN versions v ON p.version_id = v.id
JOIN libraries l ON v.library_id = l.id
WHERE l.name = ?
AND COALESCE(v.name, '') = COALESCE(?, '')
WHERE p.version_id = ?
AND documents_fts MATCH ?
ORDER BY fts_score
LIMIT ?
Expand All @@ -1787,12 +1793,11 @@ export class DocumentStore {
`);

const rawResults = stmt.all(
library.toLowerCase(),
normalizedVersion,
libraryId,
versionId,
JSON.stringify(embedding),
vectorSearchK,
library.toLowerCase(),
normalizedVersion,
versionId,
ftsQuery,
overfetchLimit,
) as RawSearchResult[];
Expand Down Expand Up @@ -1835,10 +1840,7 @@ export class DocumentStore {
FROM documents_fts f
JOIN documents d ON f.rowid = d.id
JOIN pages p ON d.page_id = p.id
JOIN versions v ON p.version_id = v.id
JOIN libraries l ON v.library_id = l.id
WHERE l.name = ?
AND COALESCE(v.name, '') = COALESCE(?, '')
WHERE p.version_id = ?
AND documents_fts MATCH ?
AND NOT EXISTS (
SELECT 1 FROM json_each(json_extract(d.metadata, '$.types')) je
Expand All @@ -1848,12 +1850,9 @@ export class DocumentStore {
LIMIT ?
`);

const rawResults = stmt.all(
library.toLowerCase(),
normalizedVersion,
ftsQuery,
limit,
) as (RawSearchResult & { fts_score: number })[];
const rawResults = stmt.all(versionId, ftsQuery, limit) as (RawSearchResult & {
fts_score: number;
})[];

// Assign FTS ranks based on order (best score = rank 1)
return rawResults.map((row, index) => {
Expand Down