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:
@@ -30,10 +30,36 @@ func (p *PluginMerge) mergeOverlappingChunks(
|
||||
continue
|
||||
}
|
||||
|
||||
// Partial overlap: append the non-overlapping suffix
|
||||
// Partial overlap: append the non-overlapping suffix.
|
||||
//
|
||||
// Offset math assumes len([]rune(Content)) == EndAt-StartAt, but a
|
||||
// few upstream paths break that invariant:
|
||||
// - Parent-child chunker prepends table headers, so Content may be
|
||||
// longer than EndAt-StartAt.
|
||||
// - Legacy data / mixed chunk sources may carry EndAt-StartAt that
|
||||
// exceed the actual rune length of Content.
|
||||
// We clamp offset into [0, len(contentRunes)] so the merge degrades
|
||||
// gracefully instead of panicking with a negative slice bound.
|
||||
if chunks[i].EndAt > lastChunk.EndAt {
|
||||
contentRunes := []rune(chunks[i].Content)
|
||||
offset := len(contentRunes) - (chunks[i].EndAt - lastChunk.EndAt)
|
||||
suffixLen := chunks[i].EndAt - lastChunk.EndAt
|
||||
offset := len(contentRunes) - suffixLen
|
||||
if offset < 0 || offset > len(contentRunes) {
|
||||
pipelineWarn(ctx, "Merge", "overlap_offset_clamp", map[string]interface{}{
|
||||
"knowledge_id": knowledgeID,
|
||||
"chunk_id": chunks[i].ID,
|
||||
"content_runes": len(contentRunes),
|
||||
"chunk_start": chunks[i].StartAt,
|
||||
"chunk_end": chunks[i].EndAt,
|
||||
"last_end": lastChunk.EndAt,
|
||||
"computed_offset": offset,
|
||||
})
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
} else {
|
||||
offset = len(contentRunes)
|
||||
}
|
||||
}
|
||||
lastChunk.Content = lastChunk.Content + string(contentRunes[offset:])
|
||||
lastChunk.EndAt = chunks[i].EndAt
|
||||
lastChunk.SubChunkID = append(lastChunk.SubChunkID, chunks[i].ID)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user