From af58e9cff4446f21e8a57908aa01b2b4f30e922e Mon Sep 17 00:00:00 2001 From: wizardchen Date: Thu, 16 Apr 2026 15:15:46 +0800 Subject: [PATCH] fix(repository): ensure expandedTopK is at least params.TopK in VectorRetrieve - Added a check to set expandedTopK to params.TopK if it is lower, ensuring the subquery limit is not less than the final limit. - Removed unnecessary type casting in the SQL query for embedding distance calculations, simplifying the query structure. This update improves the accuracy of vector retrieval operations in the PostgreSQL repository. --- .../repository/retriever/postgres/repository.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/application/repository/retriever/postgres/repository.go b/internal/application/repository/retriever/postgres/repository.go index 3ff7796a..5d26e591 100644 --- a/internal/application/repository/retriever/postgres/repository.go +++ b/internal/application/repository/retriever/postgres/repository.go @@ -348,6 +348,9 @@ func (g *pgRepository) VectorRetrieve(ctx context.Context, if expandedTopK > 1000 { expandedTopK = 1000 // Maximum 1000 candidates } + if expandedTopK < params.TopK { + expandedTopK = params.TopK // Ensure subquery limit is at least final limit + } // Optimized query: Use subquery to calculate distance once // Strategy: Use ORDER BY with vector distance to leverage HNSW index, @@ -364,16 +367,16 @@ func (g *pgRepository) VectorRetrieve(ctx context.Context, FROM ( SELECT id, content, source_id, source_type, chunk_id, knowledge_id, knowledge_base_id, tag_id, - embedding::halfvec(%d) <=> $1::halfvec as distance + embedding <=> $1::halfvec as distance FROM embeddings %s - ORDER BY embedding::halfvec(%d) <=> $1::halfvec + ORDER BY embedding <=> $1::halfvec LIMIT $%d ) AS candidates WHERE distance <= $%d ORDER BY distance ASC LIMIT $%d - `, dimension, whereClause, dimension, subqueryLimitParam, thresholdParam, finalLimitParam) + `, whereClause, subqueryLimitParam, thresholdParam, finalLimitParam) allVars = append(allVars, expandedTopK) // LIMIT in subquery allVars = append(allVars, 1-params.Threshold) // Distance threshold