mirror of
https://github.com/marcominerva/SqlDatabaseVectorSearch.git
synced 2026-06-20 12:23:10 +00:00
Optimize delete logic
This commit is contained in:
@@ -19,7 +19,6 @@ var appSettings = builder.Services.ConfigureAndGet<AppSettings>(builder.Configur
|
|||||||
|
|
||||||
builder.Services.AddSqlServer<ApplicationDbContext>(builder.Configuration.GetConnectionString("SqlConnection"), options =>
|
builder.Services.AddSqlServer<ApplicationDbContext>(builder.Configuration.GetConnectionString("SqlConnection"), options =>
|
||||||
{
|
{
|
||||||
options.EnableRetryOnFailure(3, TimeSpan.FromSeconds(1), null);
|
|
||||||
options.UseVectorSearch();
|
options.UseVectorSearch();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -21,10 +21,13 @@ public class VectorSearchService(ApplicationDbContext dbContext, ITextEmbeddingG
|
|||||||
// Extract the contents of the file (current, only PDF are supported).
|
// Extract the contents of the file (current, only PDF are supported).
|
||||||
var content = await GetContentAsync(stream);
|
var content = await GetContentAsync(stream);
|
||||||
|
|
||||||
|
await dbContext.Database.BeginTransactionAsync();
|
||||||
|
|
||||||
if (documentId.HasValue)
|
if (documentId.HasValue)
|
||||||
{
|
{
|
||||||
// If the user is importing a document that already exists, delete the previous one.
|
// If the user is importing a document that already exists, delete the previous one.
|
||||||
await DeleteDocumentAsync(documentId.Value, saveChanges: false);
|
await dbContext.DocumentChunks.Where(c => c.DocumentId == documentId).ExecuteDeleteAsync();
|
||||||
|
await dbContext.Documents.Where(d => d.Id == documentId).ExecuteDeleteAsync();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -47,6 +50,8 @@ public class VectorSearchService(ApplicationDbContext dbContext, ITextEmbeddingG
|
|||||||
}
|
}
|
||||||
|
|
||||||
await dbContext.SaveChangesAsync();
|
await dbContext.SaveChangesAsync();
|
||||||
|
await dbContext.Database.CommitTransactionAsync();
|
||||||
|
|
||||||
return documentId.Value;
|
return documentId.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,21 +82,14 @@ public class VectorSearchService(ApplicationDbContext dbContext, ITextEmbeddingG
|
|||||||
return documentChunk;
|
return documentChunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DeleteDocumentAsync(Guid documentId, bool saveChanges = true)
|
public async Task DeleteDocumentAsync(Guid documentId)
|
||||||
{
|
{
|
||||||
var document = await dbContext.Documents.Include(d => d.Chunks).FirstOrDefaultAsync(d => d.Id == documentId);
|
await dbContext.Database.BeginTransactionAsync();
|
||||||
if (document is null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
dbContext.DocumentChunks.RemoveRange(document.Chunks);
|
await dbContext.DocumentChunks.Where(c => c.DocumentId == documentId).ExecuteDeleteAsync();
|
||||||
dbContext.Documents.Remove(document);
|
await dbContext.Documents.Where(d => d.Id == documentId).ExecuteDeleteAsync();
|
||||||
|
|
||||||
if (saveChanges)
|
await dbContext.Database.CommitTransactionAsync();
|
||||||
{
|
|
||||||
await dbContext.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Response> AskQuestionAsync(Question question, bool reformulate = true)
|
public async Task<Response> AskQuestionAsync(Question question, bool reformulate = true)
|
||||||
|
|||||||
Reference in New Issue
Block a user