fix: KB indexing fails when Qdrant collection exists but has no vectors

- Check Qdrant collection exists and has points before skipping re-index
- Previously only checked database document_count, causing issues when:
  * Qdrant collection was deleted but DB still showed docs
  * Vectors were not properly indexed (indexed_vectors_count = 0)
  * New PDFs added to existing KB folder
- Now validates both DB and Qdrant state before deciding to skip indexing
- Logs warning when DB and Qdrant are out of sync

Fixes issue where KB from PDF files in drive were not being indexed to Qdrant
This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2026-04-27 16:49:38 +00:00
parent 602a7da1dd
commit 50977b5fa7

View file

@ -202,13 +202,29 @@ impl KnowledgeBaseManager {
.ok();
if let Some(row) = existing {
if let Some(count) = row.document_count {
if count > 0 {
info!(
"KB {} for bot {}/{} already indexed with {} docs, skipping re-index",
kb_name, bot_name, bot_id, count
);
return Ok(());
if let Some(db_doc_count) = row.document_count {
if db_doc_count > 0 {
let collection_name = format!("{}_{}_{}", bot_name, bot_id.to_string().chars().take(8).collect::<String>(), kb_name);
if let Ok(collection_info) = self.indexer.get_collection_info(&collection_name).await {
if collection_info.points_count > 0 {
info!(
"KB {} for bot {}/{} already indexed with {} docs in Qdrant ({} points), skipping re-index",
kb_name, bot_name, bot_id, db_doc_count, collection_info.points_count
);
return Ok(());
} else {
warn!(
"KB {} for bot {}/{} has {} docs in DB but 0 points in Qdrant, will re-index",
kb_name, bot_name, bot_id, db_doc_count
);
}
} else {
warn!(
"KB {} for bot {}/{} collection not found in Qdrant, will re-index",
kb_name, bot_name, bot_id
);
}
}
}
}