diff --git a/services/apps/assignments/src/embedding/embedding.handler.ts b/services/apps/assignments/src/embedding/embedding.handler.ts index 923c3bf7..734fffe9 100644 --- a/services/apps/assignments/src/embedding/embedding.handler.ts +++ b/services/apps/assignments/src/embedding/embedding.handler.ts @@ -22,12 +22,12 @@ export class EmbeddingHandler { const taskIds = new Set(); const assignmentId = assignment._id.toString(); this.upsertTasks(apiKey, assignmentId, assignment.tasks, '', taskIds); - await this.embeddingService.deleteNotIn(assignmentId, [...taskIds]); + await this.embeddingService.deleteTasksNotIn(assignmentId, [...taskIds]); } @OnEvent('assignments.*.deleted') async onAssignmentDeleted(assignment: AssignmentDocument) { - await this.embeddingService.deleteNotIn(assignment._id.toString(), []); + await this.embeddingService.deleteAll(assignment._id.toString()); } private upsertTasks(apiKey: string, assignment: string, tasks: Task[], prefix: string, taskIds: Set) { diff --git a/services/apps/assignments/src/embedding/embedding.service.ts b/services/apps/assignments/src/embedding/embedding.service.ts index 00c2e661..84206520 100644 --- a/services/apps/assignments/src/embedding/embedding.service.ts +++ b/services/apps/assignments/src/embedding/embedding.service.ts @@ -196,7 +196,7 @@ export class EmbeddingService implements OnModuleInit { return response.hits.hits.map(({_score, _source}) => ({...(_source as Embeddable), _score: _score || 0})); } - async deleteNotIn(assignment: string, tasks: string[]): Promise { + async deleteTasksNotIn(assignment: string, tasks: string[]): Promise { const body = await this.elasticsearchService.deleteByQuery({ index: 'embeddings', query: { @@ -204,6 +204,7 @@ export class EmbeddingService implements OnModuleInit { must: { term: { assignment, + type: 'task', }, }, must_not: { @@ -217,6 +218,22 @@ export class EmbeddingService implements OnModuleInit { return body.deleted || 0; } + async deleteAll(assignment: string): Promise { + const body = await this.elasticsearchService.deleteByQuery({ + index: 'embeddings', + query: { + bool: { + must: { + term: { + assignment, + }, + }, + }, + }, + }); + return body.deleted || 0; + } + async deleteBySolution(assignment: string, solution: string): Promise { const body = await this.elasticsearchService.deleteByQuery({ index: 'embeddings',