fix: search sort

This commit is contained in:
kumfo 2022-10-09 18:44:56 +08:00
parent 8a03c804d3
commit f8ea1049b6
21 changed files with 163 additions and 81 deletions

View File

@ -2589,6 +2589,18 @@ const docTemplate = `{
"name": "q",
"in": "query",
"required": true
},
{
"enum": [
"newest",
"active",
"score"
],
"type": "string",
"description": "order",
"name": "order",
"in": "query",
"required": true
}
],
"responses": {
@ -5127,6 +5139,10 @@ const docTemplate = `{
"id": {
"type": "string"
},
"status": {
"description": "Status",
"type": "string"
},
"tags": {
"description": "tags",
"type": "array",

View File

@ -2577,6 +2577,18 @@
"name": "q",
"in": "query",
"required": true
},
{
"enum": [
"newest",
"active",
"score"
],
"type": "string",
"description": "order",
"name": "order",
"in": "query",
"required": true
}
],
"responses": {
@ -5115,6 +5127,10 @@
"id": {
"type": "string"
},
"status": {
"description": "Status",
"type": "string"
},
"tags": {
"description": "tags",
"type": "array",

View File

@ -939,6 +939,9 @@ definitions:
type: string
id:
type: string
status:
description: Status
type: string
tags:
description: tags
items:
@ -2922,6 +2925,15 @@ paths:
name: q
required: true
type: string
- description: order
enum:
- newest
- active
- score
in: query
name: order
required: true
type: string
produces:
- application/json
responses:

2
go.mod
View File

@ -5,7 +5,6 @@ go 1.18
require (
github.com/Chain-Zhang/pinyin v0.1.3
github.com/bwmarrin/snowflake v0.3.0
github.com/davecgh/go-spew v1.1.1
github.com/gin-gonic/gin v1.8.1
github.com/go-playground/locales v0.14.0
github.com/go-playground/universal-translator v0.18.0
@ -37,6 +36,7 @@ require (
require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect

14
go.sum
View File

@ -523,24 +523,14 @@ github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdh
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/segmentfault/pacman v1.0.1 h1:GFdvPtNxvVVjnDM4ty02D/+4unHwG9PmjcOZSc2wRXE=
github.com/segmentfault/pacman v1.0.1/go.mod h1:5lNp5REd8QMThmBUvR3Fi9Y3AsOB4GRq7soCB4QLqOs=
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20220926035018-18f894415e5b h1:jSnRy3z3KVtVuGM2YTZihXwc4zEhW+TvyyJbBm8rjh4=
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20220926035018-18f894415e5b/go.mod h1:rmf1TCwz67dyM+AmTwSd1BxTo2AOYHj262lP93bOZbs=
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20220929065758-260b3093a347 h1:0xWBBXHHuemzMY61KYJXh7F5FW/4K8g98RYKNXodTCc=
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20220929065758-260b3093a347/go.mod h1:rmf1TCwz67dyM+AmTwSd1BxTo2AOYHj262lP93bOZbs=
github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20220926035018-18f894415e5b h1:Gx3Brm+VMAyBJn4aBsxgKl+EIhFHc/YH5cLGeFHAW4g=
github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20220926035018-18f894415e5b/go.mod h1:prPjFam7MyZ5b3S9dcDOt2tMPz6kf7C9c243s9zSwPY=
github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20220929065758-260b3093a347 h1:WpnEbmZFE8FYIgvseX+NJtDgGJlM1KSaKJhoxJywUgo=
github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20220929065758-260b3093a347/go.mod h1:prPjFam7MyZ5b3S9dcDOt2tMPz6kf7C9c243s9zSwPY=
github.com/segmentfault/pacman/contrib/i18n v0.0.0-20220926035018-18f894415e5b h1:uQmSgcV2w4OVXU6l3bQb9O+cSAVuzDQ9adJArQyFBa4=
github.com/segmentfault/pacman/contrib/i18n v0.0.0-20220926035018-18f894415e5b/go.mod h1:5Afm+OQdau/HQqSOp/ALlSUp0vZsMMMbv//kJhxuoi8=
github.com/segmentfault/pacman/contrib/i18n v0.0.0-20220929065758-260b3093a347 h1:Q29Ky9ZUGhdLIygfX6jwPYeEa7Wqn8o3f1NJWb8LvvE=
github.com/segmentfault/pacman/contrib/i18n v0.0.0-20220929065758-260b3093a347/go.mod h1:5Afm+OQdau/HQqSOp/ALlSUp0vZsMMMbv//kJhxuoi8=
github.com/segmentfault/pacman/contrib/log/zap v0.0.0-20220926035018-18f894415e5b h1:TaOBmAglooq+qKdnNTK2sy11t26ud7psHFB7/AV7l5U=
github.com/segmentfault/pacman/contrib/log/zap v0.0.0-20220926035018-18f894415e5b/go.mod h1:L4GqtXLoR73obTYqUQIzfkm8NG8pvZafxFb6KZFSSHk=
github.com/segmentfault/pacman/contrib/log/zap v0.0.0-20220929065758-260b3093a347 h1:7Adjc296AKv32dg88S0T8t9K3+N+PFYLSCctpPnCUr0=
github.com/segmentfault/pacman/contrib/log/zap v0.0.0-20220929065758-260b3093a347/go.mod h1:L4GqtXLoR73obTYqUQIzfkm8NG8pvZafxFb6KZFSSHk=
github.com/segmentfault/pacman/contrib/server/http v0.0.0-20220926035018-18f894415e5b h1:n5n5VPeYGuZCmVppKPgWR/CaINHnL+ipEp9iE1XkcQc=
github.com/segmentfault/pacman/contrib/server/http v0.0.0-20220926035018-18f894415e5b/go.mod h1:UjNiOFYv1uGCq1ZCcONaKq4eE7MW3nbgpLqgl8f9N40=
github.com/segmentfault/pacman/contrib/server/http v0.0.0-20220929065758-260b3093a347 h1:CfuRhTPK2CBQIZruq5ceuTVthspe8U1FDjWXXI2RWdo=
github.com/segmentfault/pacman/contrib/server/http v0.0.0-20220929065758-260b3093a347/go.mod h1:UjNiOFYv1uGCq1ZCcONaKq4eE7MW3nbgpLqgl8f9N40=
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
@ -740,8 +730,6 @@ golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220926192436-02166a98028e h1:I51lVG9ykW5AQeTE50sJ0+gJCAF0J78Hf1+1VUCGxDI=
golang.org/x/net v0.0.0-20220926192436-02166a98028e/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.0.0-20220927171203-f486391704dc h1:FxpXZdoBqT8RjqTy6i1E8nXHhW21wK7ptQ/EPIGxzPQ=
golang.org/x/net v0.0.0-20220927171203-f486391704dc/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
@ -824,8 +812,6 @@ golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220926163933-8cfa568d3c25 h1:nwzwVf0l2Y/lkov/+IYgMMbFyI+QypZDds9RxlSmsFQ=
golang.org/x/sys v0.0.0-20220926163933-8cfa568d3c25/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec h1:BkDtF2Ih9xZ7le9ndzTA7KJow28VbQW3odyk/8drmuI=
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=

View File

@ -28,15 +28,17 @@ func NewSearchController(searchService *service.SearchService) *SearchController
// @Produce json
// @Security ApiKeyAuth
// @Param q query string true "query string"
// @Param order query string true "order" Enums(newest,active,score)
// @Success 200 {object} handler.RespBody{data=schema.SearchListResp}
// @Router /answer/api/v1/search [get]
func (sc *SearchController) Search(ctx *gin.Context) {
var (
q string
page string
q,
order,
page,
size string
ok bool
dto schema.SearchDTO
ok bool
dto schema.SearchDTO
)
q, ok = ctx.GetQuery("q")
if len(q) == 0 || !ok {
@ -51,12 +53,17 @@ func (sc *SearchController) Search(ctx *gin.Context) {
if !ok {
size = "30"
}
order, ok = ctx.GetQuery("order")
if !ok || (order != "newest" && order != "active" && order != "score") {
order = "newest"
}
dto = schema.SearchDTO{
Query: q,
Page: converter.StringToInt(page),
Size: converter.StringToInt(size),
UserID: middleware.GetLoginUserIDFromContext(ctx),
Order: order,
}
resp, total, extra, err := sc.searchService.Search(ctx, &dto)

View File

@ -2,6 +2,7 @@ package repo
import (
"context"
"github.com/segmentfault/pacman/log"
"strings"
"time"
@ -35,7 +36,7 @@ func NewSearchRepo(data *data.Data, uniqueIDRepo unique.UniqueIDRepo, userCommon
}
}
func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagID, userID string, votes int, page, size int) (resp []schema.SearchResp, total int64, err error) {
func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagID, userID string, votes int, page, size int, order string) (resp []schema.SearchResp, total int64, err error) {
var (
b *builder.Builder
ub *builder.Builder
@ -51,6 +52,7 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagID,
"`answer_count`",
"0 as `accepted`",
"`question`.`status` as `status`",
"`post_update_time`",
).From("`question`")
ub = builder.Select(
"`answer`.`id` as `id`",
@ -63,6 +65,7 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagID,
"0 as `answer_count`",
"`adopted` as `accepted`",
"`answer`.`status` as `status`",
"`answer`.`created_at` as `post_update_time`",
).From("`answer`").
LeftJoin("`question`", "`question`.id = `answer`.question_id")
@ -103,7 +106,7 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagID,
return
}
res, err := sr.data.DB.OrderBy("created_at DESC").Limit(size, page).Query(b)
res, err := sr.data.DB.OrderBy(sr.parseOrder(ctx, order)).Limit(size, page).Query(b)
tr, err := sr.data.DB.Query(builder.Select("count(*) total").From(b, "c"))
if len(tr) != 0 {
@ -118,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) (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(
"`id`",
"`id` as `question_id`",
@ -130,6 +133,7 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, limit
"`answer_count`",
"0 as `accepted`",
"`status`",
"`post_update_time`",
).From("question")
for i, word := range words {
@ -151,7 +155,7 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, limit
b.And(builder.Gte{"answer_count": answers})
}
res, err := sr.data.DB.OrderBy("created_at DESC").Limit(size, page).Query(b)
res, err := sr.data.DB.OrderBy(sr.parseOrder(ctx, order)).Limit(size, page).Query(b)
tr, err := sr.data.DB.Query(builder.Select("count(*) total").From(b, "c"))
if len(tr) != 0 {
@ -168,7 +172,7 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, limit
return
}
func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, limitAccepted bool, questionID string, page, size int) (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(
"`answer`.`id` as `id`",
"`question_id`",
@ -180,6 +184,7 @@ func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, limitAc
"0 as `answer_count`",
"`adopted` as `accepted`",
"`answer`.`status` as `status`",
"`answer`.`created_at` as `post_update_time`",
).From("`answer`").
LeftJoin("`question`", "`question`.id = `answer`.question_id")
@ -199,7 +204,7 @@ func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, limitAc
b.Where(builder.Eq{"question_id": questionID})
}
res, err := sr.data.DB.OrderBy("created_at DESC").Limit(size, page).Query(b)
res, err := sr.data.DB.OrderBy(sr.parseOrder(ctx, order)).Limit(size, page).Query(b)
tr, err := sr.data.DB.Query(builder.Select("count(*) total").From(b, "c"))
total = converter.StringToInt64(string(tr[0]["total"]))
@ -214,6 +219,21 @@ func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, limitAc
return
}
func (sr *searchRepo) parseOrder(ctx context.Context, order string) (res string) {
switch order {
case "newest":
res = "created_at desc"
case "active":
res = "post_update_time desc"
case "score":
res = "vote_count desc"
default:
res = "created_at desc"
}
log.Error(order, "\n", res)
return
}
func (sr *searchRepo) parseResult(ctx context.Context, res []map[string][]byte) (resp []schema.SearchResp, err error) {
for _, r := range res {
var (

View File

@ -7,6 +7,7 @@ type SearchDTO struct {
UserID string
Page int
Size int
Order string
}
type SearchObject struct {

View File

@ -9,10 +9,11 @@ import (
)
type AcceptedAnswerSearch struct {
repo search_common.SearchRepo
w string
page int
size int
repo search_common.SearchRepo
w string
page int
size int
order string
}
func NewAcceptedAnswerSearch(repo search_common.SearchRepo) *AcceptedAnswerSearch {
@ -40,6 +41,7 @@ func (s *AcceptedAnswerSearch) Parse(dto *schema.SearchDTO) (ok bool) {
s.w = strings.TrimSpace(w)
s.page = dto.Page
s.size = dto.Size
s.order = dto.Order
return
}
func (s *AcceptedAnswerSearch) Search(ctx context.Context) (resp []schema.SearchResp, total int64, err error) {
@ -49,5 +51,5 @@ func (s *AcceptedAnswerSearch) Search(ctx context.Context) (resp []schema.Search
words = words[:4]
}
return s.repo.SearchAnswers(ctx, words, true, "", s.page, s.size)
return s.repo.SearchAnswers(ctx, words, true, "", s.page, s.size, s.order)
}

View File

@ -8,10 +8,11 @@ import (
)
type AnswerSearch struct {
repo search_common.SearchRepo
w string
page int
size int
repo search_common.SearchRepo
w string
page int
size int
order string
}
func NewAnswerSearch(repo search_common.SearchRepo) *AnswerSearch {
@ -39,6 +40,7 @@ func (s *AnswerSearch) Parse(dto *schema.SearchDTO) (ok bool) {
s.w = strings.TrimSpace(w)
s.page = dto.Page
s.size = dto.Size
s.order = dto.Order
return
}
func (s *AnswerSearch) Search(ctx context.Context) (resp []schema.SearchResp, total int64, err error) {
@ -48,5 +50,5 @@ func (s *AnswerSearch) Search(ctx context.Context) (resp []schema.SearchResp, to
words = words[:4]
}
return s.repo.SearchAnswers(ctx, words, false, "", s.page, s.size)
return s.repo.SearchAnswers(ctx, words, false, "", s.page, s.size, s.order)
}

View File

@ -11,11 +11,12 @@ import (
)
type AnswersSearch struct {
repo search_common.SearchRepo
exp int
w string
page int
size int
repo search_common.SearchRepo
exp int
w string
page int
size int
order string
}
func NewAnswersSearch(repo search_common.SearchRepo) *AnswersSearch {
@ -49,6 +50,7 @@ func (s *AnswersSearch) Parse(dto *schema.SearchDTO) (ok bool) {
s.w = strings.TrimSpace(w)
s.page = dto.Page
s.size = dto.Size
s.order = dto.Order
return
}
@ -59,5 +61,5 @@ func (s *AnswersSearch) Search(ctx context.Context) (resp []schema.SearchResp, t
words = words[:4]
}
return s.repo.SearchQuestions(ctx, words, false, s.exp, s.page, s.size)
return s.repo.SearchQuestions(ctx, words, false, s.exp, s.page, s.size, s.order)
}

View File

@ -17,6 +17,7 @@ type AuthorSearch struct {
w string
page int
size int
order string
}
func NewAuthorSearch(repo search_common.SearchRepo, userCommon *usercommon.UserCommon) *AuthorSearch {
@ -65,6 +66,7 @@ func (s *AuthorSearch) Parse(dto *schema.SearchDTO) (ok bool) {
s.w = w
s.page = dto.Page
s.size = dto.Size
s.order = dto.Order
return
}
@ -82,7 +84,7 @@ func (s *AuthorSearch) Search(ctx context.Context) (resp []schema.SearchResp, to
words = words[:4]
}
resp, total, err = s.repo.SearchContents(ctx, words, "", s.exp, -1, s.page, s.size)
resp, total, err = s.repo.SearchContents(ctx, words, "", s.exp, -1, s.page, s.size, s.order)
return
}

View File

@ -9,11 +9,12 @@ import (
)
type InQuestionSearch struct {
repo search_common.SearchRepo
w string
exp string
page int
size int
repo search_common.SearchRepo
w string
exp string
page int
size int
order string
}
func NewInQuestionSearch(repo search_common.SearchRepo) *InQuestionSearch {
@ -47,6 +48,7 @@ func (s *InQuestionSearch) Parse(dto *schema.SearchDTO) (ok bool) {
s.w = strings.TrimSpace(w)
s.page = dto.Page
s.size = dto.Size
s.order = dto.Order
return
}
func (s *InQuestionSearch) Search(ctx context.Context) (resp []schema.SearchResp, total int64, err error) {
@ -59,5 +61,5 @@ func (s *InQuestionSearch) Search(ctx context.Context) (resp []schema.SearchResp
words = words[:4]
}
return s.repo.SearchAnswers(ctx, words, false, s.exp, s.page, s.size)
return s.repo.SearchAnswers(ctx, words, false, s.exp, s.page, s.size, s.order)
}

View File

@ -9,10 +9,11 @@ import (
)
type NotAcceptedQuestion struct {
repo search_common.SearchRepo
w string
page int
size int
repo search_common.SearchRepo
w string
page int
size int
order string
}
func NewNotAcceptedQuestion(repo search_common.SearchRepo) *NotAcceptedQuestion {
@ -40,6 +41,7 @@ func (s *NotAcceptedQuestion) Parse(dto *schema.SearchDTO) (ok bool) {
s.w = strings.TrimSpace(w)
s.page = dto.Page
s.size = dto.Size
s.order = dto.Order
return
}
func (s *NotAcceptedQuestion) Search(ctx context.Context) (resp []schema.SearchResp, total int64, err error) {
@ -52,5 +54,5 @@ func (s *NotAcceptedQuestion) Search(ctx context.Context) (resp []schema.SearchR
words = words[:4]
}
return s.repo.SearchQuestions(ctx, words, true, -1, s.page, s.size)
return s.repo.SearchQuestions(ctx, words, true, -1, s.page, s.size, s.order)
}

View File

@ -9,10 +9,11 @@ import (
)
type ObjectSearch struct {
repo search_common.SearchRepo
w string
page int
size int
repo search_common.SearchRepo
w string
page int
size int
order string
}
func NewObjectSearch(repo search_common.SearchRepo) *ObjectSearch {
@ -33,6 +34,7 @@ func (s *ObjectSearch) Parse(dto *schema.SearchDTO) (ok bool) {
s.w = w
s.page = dto.Page
s.size = dto.Size
s.order = dto.Order
return
}
func (s *ObjectSearch) Search(ctx context.Context) (resp []schema.SearchResp, total int64, err error) {
@ -41,5 +43,5 @@ func (s *ObjectSearch) Search(ctx context.Context) (resp []schema.SearchResp, to
if len(words) > 3 {
words = words[:4]
}
return s.repo.SearchContents(ctx, words, "", "", -1, s.page, s.size)
return s.repo.SearchContents(ctx, words, "", "", -1, s.page, s.size, s.order)
}

View File

@ -8,10 +8,11 @@ import (
)
type QuestionSearch struct {
repo search_common.SearchRepo
w string
page int
size int
repo search_common.SearchRepo
w string
page int
size int
order string
}
func NewQuestionSearch(repo search_common.SearchRepo) *QuestionSearch {
@ -39,6 +40,7 @@ func (s *QuestionSearch) Parse(dto *schema.SearchDTO) (ok bool) {
s.w = strings.TrimSpace(w)
s.page = dto.Page
s.size = dto.Size
s.order = dto.Order
return
}
@ -49,5 +51,5 @@ func (s *QuestionSearch) Search(ctx context.Context) (resp []schema.SearchResp,
words = words[:4]
}
return s.repo.SearchQuestions(ctx, words, false, -1, s.page, s.size)
return s.repo.SearchQuestions(ctx, words, false, -1, s.page, s.size, s.order)
}

View File

@ -11,11 +11,12 @@ import (
)
type ScoreSearch struct {
repo search_common.SearchRepo
exp int
w string
page int
size int
repo search_common.SearchRepo
exp int
w string
page int
size int
order string
}
func NewScoreSearch(repo search_common.SearchRepo) *ScoreSearch {
@ -44,6 +45,7 @@ func (s *ScoreSearch) Parse(dto *schema.SearchDTO) (ok bool) {
s.w = w
s.page = dto.Page
s.size = dto.Size
s.order = dto.Order
return
}
func (s *ScoreSearch) Search(ctx context.Context) (resp []schema.SearchResp, total int64, err error) {
@ -56,6 +58,6 @@ func (s *ScoreSearch) Search(ctx context.Context) (resp []schema.SearchResp, tot
words = words[:4]
}
resp, total, err = s.repo.SearchContents(ctx, words, "", "", s.exp, s.page, s.size)
resp, total, err = s.repo.SearchContents(ctx, words, "", "", s.exp, s.page, s.size, s.order)
return
}

View File

@ -21,6 +21,7 @@ type TagSearch struct {
w string
userID string
Extra schema.GetTagPageResp
order string
}
func NewTagSearch(repo search_common.SearchRepo, tagRepo tagcommon.TagRepo, followCommon activity_common.FollowRepo) *TagSearch {
@ -53,6 +54,7 @@ func (ts *TagSearch) Parse(dto *schema.SearchDTO) (ok bool) {
ts.page = dto.Page
ts.size = dto.Size
ts.userID = dto.UserID
ts.order = dto.Order
return ok
}
@ -90,7 +92,7 @@ func (ts *TagSearch) Search(ctx context.Context) (resp []schema.SearchResp, tota
words = words[:4]
}
resp, total, err = ts.repo.SearchContents(ctx, words, tag.ID, "", -1, ts.page, ts.size)
resp, total, err = ts.repo.SearchContents(ctx, words, tag.ID, "", -1, ts.page, ts.size, ts.order)
return
}

View File

@ -9,9 +9,10 @@ import (
)
type ViewsSearch struct {
repo search_common.SearchRepo
exp string
q string
repo search_common.SearchRepo
exp string
q string
order string
}
func NewViewsSearch(repo search_common.SearchRepo) *ViewsSearch {
@ -38,6 +39,7 @@ func (s *ViewsSearch) Parse(dto *schema.SearchDTO) (ok bool) {
q = strings.TrimSpace(q)
s.exp = exp
s.q = q
s.order = dto.Order
return
}
func (s *ViewsSearch) Search(ctx context.Context) (resp []schema.SearchResp, total int64, err error) {

View File

@ -8,10 +8,11 @@ import (
)
type WithinSearch struct {
repo search_common.SearchRepo
w string
page int
size int
repo search_common.SearchRepo
w string
page int
size int
order string
}
func NewWithinSearch(repo search_common.SearchRepo) *WithinSearch {
@ -49,9 +50,10 @@ func (s *WithinSearch) Parse(dto *schema.SearchDTO) (ok bool) {
s.w = string(w)
s.page = dto.Page
s.size = dto.Size
s.order = dto.Order
return
}
func (s *WithinSearch) Search(ctx context.Context) (resp []schema.SearchResp, total int64, err error) {
return s.repo.SearchContents(ctx, []string{s.w}, "", "", -1, s.page, s.size)
return s.repo.SearchContents(ctx, []string{s.w}, "", "", -1, s.page, s.size, s.order)
}

View File

@ -6,7 +6,7 @@ import (
)
type SearchRepo interface {
SearchContents(ctx context.Context, words []string, tagID, userID string, votes int, page, size int) (resp []schema.SearchResp, total int64, err error)
SearchQuestions(ctx context.Context, words []string, limitNoAccepted bool, answers, page, size int) (resp []schema.SearchResp, total int64, err error)
SearchAnswers(ctx context.Context, words []string, limitAccepted bool, questionID string, page, size int) (resp []schema.SearchResp, total int64, err error)
SearchContents(ctx context.Context, words []string, tagID, userID string, votes, page, size int, order string) (resp []schema.SearchResp, total int64, err error)
SearchQuestions(ctx context.Context, words []string, limitNoAccepted bool, answers, page, size int, order string) (resp []schema.SearchResp, total int64, err error)
SearchAnswers(ctx context.Context, words []string, limitAccepted bool, questionID string, page, size int, order string) (resp []schema.SearchResp, total int64, err error)
}