Skip to content

Commit 7a17de0

Browse files
committed
Handle deleting existing answers from Index before adding new answer
1 parent 8dcbf0f commit 7a17de0

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

MyApp.ServiceInterface/BackgroundMqServices.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public async Task Any(DbWrites request)
108108
};
109109
await Db.InsertAsync(rankJob);
110110
modelWorkers.Enqueue(rankJob);
111-
MessageProducer.Publish(new SearchTasks { AddPostId = postJob.PostId });
111+
MessageProducer.Publish(new SearchTasks { AddPostToIndex = postJob.PostId });
112112
}
113113
}
114114
}

MyApp.ServiceInterface/SearchServices.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public class SearchServices(ILogger<SearchServices> log, QuestionsProvider quest
1111
{
1212
public async Task Any(SearchTasks request)
1313
{
14-
if (request.AddPostId != null)
14+
if (request.AddPostToIndex != null)
1515
{
16-
var id = request.AddPostId.Value;
16+
var id = request.AddPostToIndex.Value;
1717
var questionFiles = await questions.GetQuestionAsync(id);
1818

1919
log.LogInformation("Adding Post '{PostId}' Question and {AnswerCount} to Search Index...",
@@ -65,16 +65,18 @@ await db.ExecuteNonQueryAsync($@"INSERT INTO {nameof(PostFts)} (
6565
var userName = fileType.Substring(2);
6666
log.LogDebug("Adding Human Answer {FilePath}", file.VirtualPath);
6767
var modifiedDate = post.LastEditDate ?? (post.CreationDate > minDate ? post.CreationDate : minDate);
68-
db.ExecuteNonQuery($@"INSERT INTO {nameof(PostFts)} (
68+
var refId = $"{id}-{userName}";
69+
await db.ExecuteNonQueryAsync($"DELETE FROM {nameof(PostFts)} where {nameof(PostFts.RefId)} = {QuotedValue(refId)}");
70+
await db.ExecuteNonQueryAsync($@"INSERT INTO {nameof(PostFts)} (
6971
rowid,
7072
{nameof(PostFts.RefId)},
7173
{nameof(PostFts.UserName)},
7274
{nameof(PostFts.Body)},
7375
{nameof(PostFts.ModifiedDate)}
7476
) VALUES (
7577
{post.Id},
76-
'{id}-{userName}',
77-
'{userName}',
78+
{QuotedValue(refId)},
79+
{QuotedValue(userName)},
7880
{QuotedValue(post.Body)},
7981
{QuotedValue(modifiedDate.ToString("yyyy-MM-dd HH:mm:ss"))}
8082
)");
@@ -92,6 +94,8 @@ await db.ExecuteNonQueryAsync($@"INSERT INTO {nameof(PostFts)} (
9294
? DateTimeOffset.FromUnixTimeSeconds(created).DateTime
9395
: file.LastModified;
9496
log.LogDebug("Adding Model Answer {FilePath} {UserName}", file.VirtualPath, userName);
97+
var refId = $"{id}-{userName}";
98+
await db.ExecuteNonQueryAsync($"DELETE FROM {nameof(PostFts)} where {nameof(PostFts.RefId)} = {QuotedValue(refId)}");
9599
db.ExecuteNonQuery($@"INSERT INTO {nameof(PostFts)} (
96100
rowid,
97101
{nameof(PostFts.RefId)},
@@ -100,8 +104,8 @@ await db.ExecuteNonQueryAsync($@"INSERT INTO {nameof(PostFts)} (
100104
{nameof(PostFts.ModifiedDate)}
101105
) VALUES (
102106
{nextId++},
103-
'{id}-{userName}',
104-
'{userName}',
107+
{QuotedValue(refId)},
108+
{QuotedValue(userName)},
105109
{QuotedValue(body)},
106110
{QuotedValue(modifiedDate.ToString("yyyy-MM-dd HH:mm:ss"))}
107111
)");
@@ -125,5 +129,4 @@ async Task<Post> ToPostAsync(IVirtualFile file)
125129
var post = json.FromJson<Post>();
126130
return post;
127131
}
128-
129-
}
132+
}

MyApp.ServiceModel/Stats.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,5 @@ public class DbWrites
131131
[Restrict(InternalOnly = true)]
132132
public class SearchTasks
133133
{
134-
public int? AddPostId { get; set; }
134+
public int? AddPostToIndex { get; set; }
135135
}

0 commit comments

Comments
 (0)