Merge branch 'fix/search' into 'test'

fix(search_repo):

See merge request opensource/answer!290
This commit is contained in:
杨光富 2022-11-29 06:22:40 +00:00
commit e13c39cf0d
1 changed files with 25 additions and 16 deletions

View File

@ -157,11 +157,12 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagIDs
} }
sql := fmt.Sprintf("(%s UNION ALL %s)", ubSQL, bSQL) sql := fmt.Sprintf("(%s UNION ALL %s)", ubSQL, bSQL)
querySQL, _, err := builder.MySQL().Select("*").From(sql, "t").OrderBy(sr.parseOrder(ctx, order)).Limit(size, page-1).ToSQL() countSQL, _, err := builder.MySQL().Select("count(*) total").From(sql, "c").ToSQL()
if err != nil { if err != nil {
return return
} }
countSQL, _, err := builder.MySQL().Select("count(*) total").From(sql, "c").ToSQL()
querySQL, _, err := builder.MySQL().Select("*").From(sql, "t").OrderBy(sr.parseOrder(ctx, order)).Limit(size, page-1).ToSQL()
if err != nil { if err != nil {
return return
} }
@ -197,15 +198,17 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagIDs
// SearchQuestions search question data // SearchQuestions search question data
func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, notAccepted bool, views, answers int, page, size int, order string) (resp []schema.SearchResp, total int64, err error) { func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, notAccepted bool, views, answers int, page, size int, order string) (resp []schema.SearchResp, total int64, err error) {
if words = filterWords(words); len(words) == 0 { words = filterWords(words)
return
}
var ( var (
qfs = qFields qfs = qFields
args = []interface{}{} args = []interface{}{}
) )
if order == "relevance" { if order == "relevance" {
if len(words) > 0 {
qfs, args = addRelevanceField([]string{"title", "original_text"}, words, qfs) qfs, args = addRelevanceField([]string{"title", "original_text"}, words, qfs)
} else {
order = "newest"
}
} }
b := builder.MySQL().Select(qfs...).From("question") b := builder.MySQL().Select(qfs...).From("question")
@ -257,11 +260,12 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, notAc
queryArgs := []interface{}{} queryArgs := []interface{}{}
countArgs := []interface{}{} countArgs := []interface{}{}
querySQL, _, err := b.OrderBy(sr.parseOrder(ctx, order)).Limit(size, page-1).ToSQL() countSQL, _, err := builder.MySQL().Select("count(*) total").From(b, "c").ToSQL()
if err != nil { if err != nil {
return return
} }
countSQL, _, err := builder.MySQL().Select("count(*) total").From(b, "c").ToSQL()
querySQL, _, err := b.OrderBy(sr.parseOrder(ctx, order)).Limit(size, page-1).ToSQL()
if err != nil { if err != nil {
return return
} }
@ -293,15 +297,18 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, notAc
// SearchAnswers search answer data // SearchAnswers search answer data
func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, tagIDs []string, accepted bool, questionID string, page, size int, order string) (resp []schema.SearchResp, total int64, err error) { func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, tagIDs []string, accepted bool, questionID string, page, size int, order string) (resp []schema.SearchResp, total int64, err error) {
if words = filterWords(words); len(words) == 0 { words = filterWords(words)
return
}
var ( var (
afs = aFields afs = aFields
args = []interface{}{} args = []interface{}{}
) )
if order == "relevance" { if order == "relevance" {
if len(words) > 0 {
afs, args = addRelevanceField([]string{"`answer`.`original_text`"}, words, afs) afs, args = addRelevanceField([]string{"`answer`.`original_text`"}, words, afs)
} else {
order = "newest"
}
} }
b := builder.MySQL().Select(afs...).From("`answer`"). b := builder.MySQL().Select(afs...).From("`answer`").
@ -346,14 +353,16 @@ func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, tagIDs
queryArgs := []interface{}{} queryArgs := []interface{}{}
countArgs := []interface{}{} countArgs := []interface{}{}
querySQL, _, err := b.OrderBy(sr.parseOrder(ctx, order)).Limit(size, page-1).ToSQL()
if err != nil {
return
}
countSQL, _, err := builder.MySQL().Select("count(*) total").From(b, "c").ToSQL() countSQL, _, err := builder.MySQL().Select("count(*) total").From(b, "c").ToSQL()
if err != nil { if err != nil {
return return
} }
querySQL, _, err := b.OrderBy(sr.parseOrder(ctx, order)).Limit(size, page-1).ToSQL()
if err != nil {
return
}
queryArgs = append(queryArgs, querySQL) queryArgs = append(queryArgs, querySQL)
queryArgs = append(queryArgs, args...) queryArgs = append(queryArgs, args...)