refactor(postgres): optimize vector retrieval and HNSW settings

- Adjusted the expanded TopK limit to a maximum of 200 candidates to enhance HNSW efficiency.
- Updated the vector retrieval query to ensure the ORDER BY expression matches the indexed expression, improving index usage.
- Implemented local settings for HNSW's ef_search and iterative_scan within a transaction to optimize candidate retrieval.
- Added fallback logic to handle unsupported GUCs gracefully, ensuring query execution continues even with older pgvector versions.

These changes improve the performance and reliability of vector retrieval in the PostgreSQL implementation.
This commit is contained in:
wizardchen
2026-04-17 11:55:32 +08:00
committed by lyingbug
parent d546406a15
commit 092044b68f
3 changed files with 103 additions and 21 deletions
@@ -114,8 +114,8 @@ func (s *knowledgeBaseService) HybridSearch(ctx context.Context,
// Use 5x over-retrieval to ensure sufficient candidates for RRF fusion and reranking.
// Scale proportionally when searching multiple KBs to maintain per-KB recall quality.
matchCount := max(params.MatchCount*5, 50) * len(searchKBIDs)
if matchCount > 1000 {
matchCount = 1000
if matchCount > 500 {
matchCount = 500
}
// Build retrieval parameters for vector and keyword engines