|
|
@@ -84,9 +84,30 @@ export class QdrantEmbeddingWorkflow {
|
|
|
|
|
|
logger.info(`Content ${contentId} split into ${chunks.length} chunks (avg: ${chunkStats.avgWordCount} words/chunk)`);
|
|
|
|
|
|
- // Get iterator for collection naming
|
|
|
- const iterator = await this.qdrantService.getNextIterator(shop.custom_id, contentType);
|
|
|
- const collectionName = this.qdrantService.generateCollectionName(shop.custom_id, contentType, iterator);
|
|
|
+ // Check if this content already has a collection assigned
|
|
|
+ const existingEmbedding = existingEmbeddings.find(emb => emb.collection_name);
|
|
|
+ let collectionName: string;
|
|
|
+
|
|
|
+ if (existingEmbedding && existingEmbedding.collection_name) {
|
|
|
+ // Reuse existing collection for this URL
|
|
|
+ collectionName = existingEmbedding.collection_name;
|
|
|
+ logger.debug(`Reusing existing collection ${collectionName} for content ${contentId}`);
|
|
|
+
|
|
|
+ // Delete all existing points in this collection for this content_id
|
|
|
+ // This ensures we don't have duplicate/stale chunks
|
|
|
+ await this.qdrantService.deletePointsByContentId(collectionName, contentId);
|
|
|
+
|
|
|
+ // Delete all existing embedding records for this content
|
|
|
+ // We'll create new ones for each chunk
|
|
|
+ for (const emb of existingEmbeddings) {
|
|
|
+ this.database.deleteShopEmbedding(emb.id);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // Get next iterator for this category (one collection per URL)
|
|
|
+ const iterator = await this.qdrantService.getNextIterator(shop.custom_id, contentType);
|
|
|
+ collectionName = this.qdrantService.generateCollectionName(shop.custom_id, contentType, iterator);
|
|
|
+ logger.info(`Assigned new collection ${collectionName} for content ${contentId}`);
|
|
|
+ }
|
|
|
|
|
|
// Ensure collection exists
|
|
|
if (!(await this.qdrantService.collectionExists(collectionName))) {
|