From a331cad4bfc2e6d6707f9eace1defd0074fb11c0 Mon Sep 17 00:00:00 2001 From: kumfo Date: Tue, 29 Nov 2022 14:16:41 +0800 Subject: [PATCH] fix(search_repo): 1. is:question and is:answer can be return list without keywords. 2. fixed search result count is limited page_size --- internal/repo/search_common/search_repo.go | 41 +++++++++++++--------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/internal/repo/search_common/search_repo.go b/internal/repo/search_common/search_repo.go index f1061300..5b1487be 100644 --- a/internal/repo/search_common/search_repo.go +++ b/internal/repo/search_common/search_repo.go @@ -157,11 +157,12 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagIDs } 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 { 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 { return } @@ -197,15 +198,17 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagIDs // 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) { - if words = filterWords(words); len(words) == 0 { - return - } + words = filterWords(words) var ( qfs = qFields args = []interface{}{} ) if order == "relevance" { - qfs, args = addRelevanceField([]string{"title", "original_text"}, words, qfs) + if len(words) > 0 { + qfs, args = addRelevanceField([]string{"title", "original_text"}, words, qfs) + } else { + order = "newest" + } } b := builder.MySQL().Select(qfs...).From("question") @@ -257,11 +260,12 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, notAc queryArgs := []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 { 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 { return } @@ -293,15 +297,18 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, notAc // 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) { - if words = filterWords(words); len(words) == 0 { - return - } + words = filterWords(words) + var ( afs = aFields args = []interface{}{} ) if order == "relevance" { - afs, args = addRelevanceField([]string{"`answer`.`original_text`"}, words, afs) + if len(words) > 0 { + afs, args = addRelevanceField([]string{"`answer`.`original_text`"}, words, afs) + } else { + order = "newest" + } } b := builder.MySQL().Select(afs...).From("`answer`"). @@ -346,14 +353,16 @@ func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, tagIDs queryArgs := []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() if err != nil { 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, args...)