update search

This commit is contained in:
aichy126 2023-04-20 16:18:22 +08:00
parent d02ec9712a
commit a93668d455
1 changed files with 25 additions and 35 deletions

View File

@ -103,25 +103,20 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagIDs
argsQ = append(argsQ, entity.QuestionStatusDeleted, entity.QuestionShow) argsQ = append(argsQ, entity.QuestionStatusDeleted, entity.QuestionShow)
argsA = append(argsA, entity.QuestionStatusDeleted, entity.AnswerStatusDeleted, entity.QuestionShow) argsA = append(argsA, entity.QuestionStatusDeleted, entity.AnswerStatusDeleted, entity.QuestionShow)
for i, word := range words { likeConQ := builder.NewCond()
if i == 0 { likeConA := builder.NewCond()
b.Where(builder.Like{"title", word}). for _, word := range words {
likeConQ = likeConQ.Or(builder.Like{"title", word}).
Or(builder.Like{"original_text", word}) Or(builder.Like{"original_text", word})
argsQ = append(argsQ, "%"+word+"%") argsQ = append(argsQ, "%"+word+"%")
argsQ = append(argsQ, "%"+word+"%") argsQ = append(argsQ, "%"+word+"%")
ub.Where(builder.Like{"`answer`.original_text", word}) likeConA = likeConA.Or(builder.Like{"`answer`.original_text", word})
argsA = append(argsA, "%"+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}) b.Where(likeConQ)
argsA = append(argsA, "%"+word+"%") ub.Where(likeConA)
}
}
// check tag // check tag
if len(tagIDs) > 0 { if len(tagIDs) > 0 {
@ -233,17 +228,14 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, tagID
b.Where(builder.Lt{"`question`.`status`": entity.QuestionStatusDeleted}).And(builder.Eq{"`question`.`show`": entity.QuestionShow}) b.Where(builder.Lt{"`question`.`status`": entity.QuestionStatusDeleted}).And(builder.Eq{"`question`.`show`": entity.QuestionShow})
args = append(args, entity.QuestionStatusDeleted, entity.QuestionShow) args = append(args, entity.QuestionStatusDeleted, entity.QuestionShow)
for i, word := range words { likeConQ := builder.NewCond()
if i == 0 { for _, word := range words {
b.Where(builder.Like{"title", word}). likeConQ = likeConQ.Or(builder.Like{"title", word}).
Or(builder.Like{"original_text", word}) Or(builder.Like{"original_text", word})
args = append(args, "%"+word+"%") args = append(args, "%"+word+"%")
args = append(args, "%"+word+"%") args = append(args, "%"+word+"%")
} else {
b.Or(builder.Like{"original_text", word})
args = append(args, "%"+word+"%")
}
} }
b.Where(likeConQ)
// check tag // check tag
if len(tagIDs) > 0 { if len(tagIDs) > 0 {
@ -348,15 +340,13 @@ func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, tagIDs
And(builder.Lt{"`answer`.`status`": entity.AnswerStatusDeleted}).And(builder.Eq{"`question`.`show`": entity.QuestionShow}) And(builder.Lt{"`answer`.`status`": entity.AnswerStatusDeleted}).And(builder.Eq{"`question`.`show`": entity.QuestionShow})
args = append(args, entity.QuestionStatusDeleted, entity.AnswerStatusDeleted, entity.QuestionShow) args = append(args, entity.QuestionStatusDeleted, entity.AnswerStatusDeleted, entity.QuestionShow)
for i, word := range words { likeConA := builder.NewCond()
if i == 0 { for _, word := range words {
b.Where(builder.Like{"`answer`.original_text", word}) likeConA = likeConA.Or(builder.Like{"`answer`.original_text", word})
args = append(args, "%"+word+"%")
} else {
b.Or(builder.Like{"`answer`.original_text", word})
args = append(args, "%"+word+"%") args = append(args, "%"+word+"%")
} }
}
b.Where(likeConA)
// check tag // check tag
if len(tagIDs) > 0 { if len(tagIDs) > 0 {