fix(search): change search SQL builder to fixed search result is contains deleted content.

This commit is contained in:
kumfo 2023-04-10 16:04:31 +08:00
parent 5fc73a3277
commit f8281cd841
1 changed files with 25 additions and 35 deletions

View File

@ -101,26 +101,21 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagIDs
argsQ = append(argsQ, entity.QuestionStatusDeleted)
argsA = append(argsA, entity.QuestionStatusDeleted, entity.AnswerStatusDeleted)
for i, word := range words {
if i == 0 {
b.Where(builder.Like{"title", word}).
Or(builder.Like{"original_text", word})
argsQ = append(argsQ, "%"+word+"%")
argsQ = append(argsQ, "%"+word+"%")
likeConQ := builder.NewCond()
likeConA := builder.NewCond()
for _, word := range words {
likeConQ = likeConQ.Or(builder.Like{"title", word}).
Or(builder.Like{"original_text", word})
argsQ = append(argsQ, "%"+word+"%")
argsQ = append(argsQ, "%"+word+"%")
ub.Where(builder.Like{"`answer`.original_text", word})
argsA = append(argsA, "%"+word+"%")
} else {
b.Or(builder.Like{"title", word}).
Or(builder.Like{"original_text", word})
argsQ = append(argsQ, "%"+word+"%")
argsQ = append(argsQ, "%"+word+"%")
ub.Or(builder.Like{"`answer`.original_text", word})
argsA = append(argsA, "%"+word+"%")
}
likeConA = likeConA.Or(builder.Like{"`answer`.original_text", word})
argsA = append(argsA, "%"+word+"%")
}
b.Where(likeConQ)
ub.Where(likeConA)
// check tag
if len(tagIDs) > 0 {
for ti, tagID := range tagIDs {
@ -231,17 +226,14 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, tagID
b.Where(builder.Lt{"`question`.`status`": entity.QuestionStatusDeleted})
args = append(args, entity.QuestionStatusDeleted)
for i, word := range words {
if i == 0 {
b.Where(builder.Like{"title", word}).
Or(builder.Like{"original_text", word})
args = append(args, "%"+word+"%")
args = append(args, "%"+word+"%")
} else {
b.Or(builder.Like{"original_text", word})
args = append(args, "%"+word+"%")
}
likeConQ := builder.NewCond()
for _, word := range words {
likeConQ = likeConQ.Or(builder.Like{"title", word}).
Or(builder.Like{"original_text", word})
args = append(args, "%"+word+"%")
args = append(args, "%"+word+"%")
}
b.Where(likeConQ)
// check tag
if len(tagIDs) > 0 {
@ -346,16 +338,14 @@ func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, tagIDs
And(builder.Lt{"`answer`.`status`": entity.AnswerStatusDeleted})
args = append(args, entity.QuestionStatusDeleted, entity.AnswerStatusDeleted)
for i, word := range words {
if i == 0 {
b.Where(builder.Like{"`answer`.original_text", word})
args = append(args, "%"+word+"%")
} else {
b.Or(builder.Like{"`answer`.original_text", word})
args = append(args, "%"+word+"%")
}
likeConA := builder.NewCond()
for _, word := range words {
likeConA = likeConA.Or(builder.Like{"`answer`.original_text", word})
args = append(args, "%"+word+"%")
}
b.Where(likeConA)
// check tag
if len(tagIDs) > 0 {
for ti, tagID := range tagIDs {