Merge branch 'fix/search' into 'main'

Fix/search

See merge request opensource/answer!49
This commit is contained in:
杨光富 2022-10-10 04:41:10 +00:00
commit f42ebd3032
1 changed files with 10 additions and 12 deletions

View File

@ -2,7 +2,6 @@ package repo
import ( import (
"context" "context"
"github.com/segmentfault/pacman/log"
"strings" "strings"
"time" "time"
@ -41,7 +40,7 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagID,
b *builder.Builder b *builder.Builder
ub *builder.Builder ub *builder.Builder
) )
b = builder.Select( b = builder.MySQL().Select(
"`question`.`id`", "`question`.`id`",
"`question`.`id` as `question_id`", "`question`.`id` as `question_id`",
"`title`", "`title`",
@ -54,7 +53,7 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagID,
"`question`.`status` as `status`", "`question`.`status` as `status`",
"`post_update_time`", "`post_update_time`",
).From("`question`") ).From("`question`")
ub = builder.Select( ub = builder.MySQL().Select(
"`answer`.`id` as `id`", "`answer`.`id` as `id`",
"`question_id`", "`question_id`",
"`question`.`title` as `title`", "`question`.`title` as `title`",
@ -106,9 +105,9 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagID,
return return
} }
res, err := sr.data.DB.OrderBy(sr.parseOrder(ctx, order)).Limit(size, page).Query(b) res, err := sr.data.DB.Query(builder.MySQL().Select("*").From(b, "t").OrderBy(sr.parseOrder(ctx, order)).Limit(size, page))
tr, err := sr.data.DB.Query(builder.Select("count(*) total").From(b, "c")) tr, err := sr.data.DB.Query(builder.MySQL().Select("count(*) total").From(b, "c"))
if len(tr) != 0 { if len(tr) != 0 {
total = converter.StringToInt64(string(tr[0]["total"])) total = converter.StringToInt64(string(tr[0]["total"]))
} }
@ -122,7 +121,7 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagID,
} }
func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, limitNoAccepted bool, answers, page, size int, order string) (resp []schema.SearchResp, total int64, err error) { func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, limitNoAccepted bool, answers, page, size int, order string) (resp []schema.SearchResp, total int64, err error) {
b := builder.Select( b := builder.MySQL().Select(
"`id`", "`id`",
"`id` as `question_id`", "`id` as `question_id`",
"`title`", "`title`",
@ -154,10 +153,10 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, limit
} else if answers > 0 { } else if answers > 0 {
b.And(builder.Gte{"answer_count": answers}) b.And(builder.Gte{"answer_count": answers})
} }
res, err := sr.data.DB.Query(b.OrderBy(sr.parseOrder(ctx, order)).Limit(size, page))
res, err := sr.data.DB.OrderBy(sr.parseOrder(ctx, order)).Limit(size, page).Query(b) tr, err := sr.data.DB.Query(builder.MySQL().Select("count(*) total").From(b, "c"))
tr, err := sr.data.DB.Query(builder.Select("count(*) total").From(b, "c"))
if len(tr) != 0 { if len(tr) != 0 {
total = converter.StringToInt64(string(tr[0]["total"])) total = converter.StringToInt64(string(tr[0]["total"]))
} }
@ -173,7 +172,7 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, limit
} }
func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, limitAccepted bool, questionID string, page, size int, order string) (resp []schema.SearchResp, total int64, err error) { func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, limitAccepted bool, questionID string, page, size int, order string) (resp []schema.SearchResp, total int64, err error) {
b := builder.Select( b := builder.MySQL().Select(
"`answer`.`id` as `id`", "`answer`.`id` as `id`",
"`question_id`", "`question_id`",
"`question`.`title` as `title`", "`question`.`title` as `title`",
@ -204,9 +203,9 @@ func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, limitAc
b.Where(builder.Eq{"question_id": questionID}) b.Where(builder.Eq{"question_id": questionID})
} }
res, err := sr.data.DB.OrderBy(sr.parseOrder(ctx, order)).Limit(size, page).Query(b) res, err := sr.data.DB.Query(b.OrderBy(sr.parseOrder(ctx, order)).Limit(size, page))
tr, err := sr.data.DB.Query(builder.Select("count(*) total").From(b, "c")) tr, err := sr.data.DB.Query(builder.MySQL().Select("count(*) total").From(b, "c"))
total = converter.StringToInt64(string(tr[0]["total"])) total = converter.StringToInt64(string(tr[0]["total"]))
if err != nil { if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
@ -230,7 +229,6 @@ func (sr *searchRepo) parseOrder(ctx context.Context, order string) (res string)
default: default:
res = "created_at desc" res = "created_at desc"
} }
log.Error(order, "\n", res)
return return
} }