Skip to content

Commit

Permalink
Handle deleting existing answers from Index before adding new answer
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Mar 27, 2024
1 parent 8dcbf0f commit 7a17de0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion MyApp.ServiceInterface/BackgroundMqServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public async Task Any(DbWrites request)
};
await Db.InsertAsync(rankJob);
modelWorkers.Enqueue(rankJob);
MessageProducer.Publish(new SearchTasks { AddPostId = postJob.PostId });
MessageProducer.Publish(new SearchTasks { AddPostToIndex = postJob.PostId });
}
}
}
Expand Down
21 changes: 12 additions & 9 deletions MyApp.ServiceInterface/SearchServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class SearchServices(ILogger<SearchServices> log, QuestionsProvider quest
{
public async Task Any(SearchTasks request)
{
if (request.AddPostId != null)
if (request.AddPostToIndex != null)
{
var id = request.AddPostId.Value;
var id = request.AddPostToIndex.Value;
var questionFiles = await questions.GetQuestionAsync(id);

log.LogInformation("Adding Post '{PostId}' Question and {AnswerCount} to Search Index...",
Expand Down Expand Up @@ -65,16 +65,18 @@ await db.ExecuteNonQueryAsync($@"INSERT INTO {nameof(PostFts)} (
var userName = fileType.Substring(2);
log.LogDebug("Adding Human Answer {FilePath}", file.VirtualPath);
var modifiedDate = post.LastEditDate ?? (post.CreationDate > minDate ? post.CreationDate : minDate);
db.ExecuteNonQuery($@"INSERT INTO {nameof(PostFts)} (
var refId = $"{id}-{userName}";
await db.ExecuteNonQueryAsync($"DELETE FROM {nameof(PostFts)} where {nameof(PostFts.RefId)} = {QuotedValue(refId)}");
await db.ExecuteNonQueryAsync($@"INSERT INTO {nameof(PostFts)} (
rowid,
{nameof(PostFts.RefId)},
{nameof(PostFts.UserName)},
{nameof(PostFts.Body)},
{nameof(PostFts.ModifiedDate)}
) VALUES (
{post.Id},
'{id}-{userName}',
'{userName}',
{QuotedValue(refId)},
{QuotedValue(userName)},
{QuotedValue(post.Body)},
{QuotedValue(modifiedDate.ToString("yyyy-MM-dd HH:mm:ss"))}
)");
Expand All @@ -92,6 +94,8 @@ await db.ExecuteNonQueryAsync($@"INSERT INTO {nameof(PostFts)} (
? DateTimeOffset.FromUnixTimeSeconds(created).DateTime
: file.LastModified;
log.LogDebug("Adding Model Answer {FilePath} {UserName}", file.VirtualPath, userName);
var refId = $"{id}-{userName}";
await db.ExecuteNonQueryAsync($"DELETE FROM {nameof(PostFts)} where {nameof(PostFts.RefId)} = {QuotedValue(refId)}");
db.ExecuteNonQuery($@"INSERT INTO {nameof(PostFts)} (
rowid,
{nameof(PostFts.RefId)},
Expand All @@ -100,8 +104,8 @@ await db.ExecuteNonQueryAsync($@"INSERT INTO {nameof(PostFts)} (
{nameof(PostFts.ModifiedDate)}
) VALUES (
{nextId++},
'{id}-{userName}',
'{userName}',
{QuotedValue(refId)},
{QuotedValue(userName)},
{QuotedValue(body)},
{QuotedValue(modifiedDate.ToString("yyyy-MM-dd HH:mm:ss"))}
)");
Expand All @@ -125,5 +129,4 @@ async Task<Post> ToPostAsync(IVirtualFile file)
var post = json.FromJson<Post>();
return post;
}

}
}
2 changes: 1 addition & 1 deletion MyApp.ServiceModel/Stats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,5 @@ public class DbWrites
[Restrict(InternalOnly = true)]
public class SearchTasks
{
public int? AddPostId { get; set; }
public int? AddPostToIndex { get; set; }
}

0 comments on commit 7a17de0

Please sign in to comment.