diff --git a/src/Ingest/IngestFlow.php b/src/Ingest/IngestFlow.php index 6639508..8d6c827 100644 --- a/src/Ingest/IngestFlow.php +++ b/src/Ingest/IngestFlow.php @@ -137,14 +137,30 @@ final readonly class IngestFlow throw new \RuntimeException('Document not found.'); } + // Chunks entfernen $this->chunkManager->compactByDocument($documentId); - $this->rebuildIndex(false); - + // Dokument aus DB entfernen $this->em->remove($document); $this->em->flush(); + + // Prüfen, ob noch INDEXED-Versionen existieren + $indexedCount = $this->em + ->getRepository(DocumentVersion::class) + ->count([ + 'ingestStatus' => DocumentVersion::INGEST_INDEXED, + ]); + + if ($indexedCount === 0) { + // Kein Reindex notwendig + return; + } + + // 4️⃣ Reindex nur wenn sinnvoll + $this->rebuildIndex(false); } + // ========================================================= // CENTRAL REBUILD // =========================================================