Skip to content

Commit

Permalink
fix(search): fix the wrong offset when searching
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkinStars committed Dec 9, 2024
1 parent 98eceff commit 5060e08
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions internal/repo/search_common/search_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func NewSearchRepo(
}

// SearchContents search question and answer data
func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagIDs [][]string, userID string, votes int, page, size int, order string) (resp []*schema.SearchResult, total int64, err error) {
func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagIDs [][]string, userID string, votes int, page, pageSize int, order string) (resp []*schema.SearchResult, total int64, err error) {
words = filterWords(words)

var (
Expand Down Expand Up @@ -206,7 +206,8 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagIDs
return
}

querySQL, _, err := builder.MySQL().Select("*").From(sql, "t").OrderBy(sr.parseOrder(ctx, order)).Limit(size, page-1).ToSQL()
startNum := (page - 1) * pageSize
querySQL, _, err := builder.MySQL().Select("*").From(sql, "t").OrderBy(sr.parseOrder(ctx, order)).Limit(pageSize, startNum).ToSQL()
if err != nil {
return
}
Expand Down Expand Up @@ -241,7 +242,7 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagIDs
}

// SearchQuestions search question data
func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, tagIDs [][]string, notAccepted bool, views, answers int, page, size int, order string) (resp []*schema.SearchResult, total int64, err error) {
func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, tagIDs [][]string, notAccepted bool, views, answers int, page, pageSize int, order string) (resp []*schema.SearchResult, total int64, err error) {
words = filterWords(words)
var (
qfs = qFields
Expand Down Expand Up @@ -320,7 +321,8 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, tagID
return
}

querySQL, _, err := b.OrderBy(sr.parseOrder(ctx, order)).Limit(size, page-1).ToSQL()
startNum := (page - 1) * pageSize
querySQL, _, err := b.OrderBy(sr.parseOrder(ctx, order)).Limit(pageSize, startNum).ToSQL()
if err != nil {
return
}
Expand Down Expand Up @@ -351,7 +353,7 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, tagID
}

// 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.SearchResult, total int64, err error) {
func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, tagIDs [][]string, accepted bool, questionID string, page, pageSize int, order string) (resp []*schema.SearchResult, total int64, err error) {
words = filterWords(words)

var (
Expand Down Expand Up @@ -415,7 +417,8 @@ func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, tagIDs
return
}

querySQL, _, err := b.OrderBy(sr.parseOrder(ctx, order)).Limit(size, page-1).ToSQL()
startNum := (page - 1) * pageSize
querySQL, _, err := b.OrderBy(sr.parseOrder(ctx, order)).Limit(pageSize, startNum).ToSQL()
if err != nil {
return
}
Expand Down

0 comments on commit 5060e08

Please sign in to comment.