Skip to content

Commit 0bd5406

Browse files
committed
fix(search): fix incorrect tags when search
1 parent a9540bf commit 0bd5406

6 files changed

Lines changed: 86 additions & 68 deletions

File tree

cmd/wire_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/repo/search_common/search_repo.go

Lines changed: 61 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package search_common
33
import (
44
"context"
55
"fmt"
6+
tagcommon "github.com/answerdev/answer/internal/service/tag_common"
67
"github.com/answerdev/answer/plugin"
78
"strconv"
89
"strings"
@@ -19,7 +20,6 @@ import (
1920
usercommon "github.com/answerdev/answer/internal/service/user_common"
2021
"github.com/answerdev/answer/pkg/converter"
2122
"github.com/answerdev/answer/pkg/obj"
22-
"github.com/jinzhu/copier"
2323
"github.com/segmentfault/pacman/errors"
2424
"xorm.io/builder"
2525
)
@@ -58,19 +58,26 @@ type searchRepo struct {
5858
data *data.Data
5959
userCommon *usercommon.UserCommon
6060
uniqueIDRepo unique.UniqueIDRepo
61+
tagCommon *tagcommon.TagCommonService
6162
}
6263

6364
// NewSearchRepo new repository
64-
func NewSearchRepo(data *data.Data, uniqueIDRepo unique.UniqueIDRepo, userCommon *usercommon.UserCommon) search_common.SearchRepo {
65+
func NewSearchRepo(
66+
data *data.Data,
67+
uniqueIDRepo unique.UniqueIDRepo,
68+
userCommon *usercommon.UserCommon,
69+
tagCommon *tagcommon.TagCommonService,
70+
) search_common.SearchRepo {
6571
return &searchRepo{
6672
data: data,
6773
uniqueIDRepo: uniqueIDRepo,
6874
userCommon: userCommon,
75+
tagCommon: tagCommon,
6976
}
7077
}
7178

7279
// SearchContents search question and answer data
73-
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) {
80+
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) {
7481
words = filterWords(words)
7582

7683
var (
@@ -210,7 +217,7 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagIDs
210217
}
211218

212219
// SearchQuestions search question data
213-
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) {
220+
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) {
214221
words = filterWords(words)
215222
var (
216223
qfs = qFields
@@ -319,7 +326,7 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, tagID
319326
}
320327

321328
// SearchAnswers search answer data
322-
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) {
329+
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) {
323330
words = filterWords(words)
324331

325332
var (
@@ -428,7 +435,7 @@ func (sr *searchRepo) parseOrder(ctx context.Context, order string) (res string)
428435
}
429436

430437
// ParseSearchPluginResult parse search plugin result
431-
func (sr *searchRepo) ParseSearchPluginResult(ctx context.Context, sres []plugin.SearchResult) (resp []schema.SearchResult, err error) {
438+
func (sr *searchRepo) ParseSearchPluginResult(ctx context.Context, sres []plugin.SearchResult) (resp []*schema.SearchResult, err error) {
432439
var (
433440
qres []map[string][]byte
434441
res = make([]map[string][]byte, 0)
@@ -455,82 +462,79 @@ func (sr *searchRepo) ParseSearchPluginResult(ctx context.Context, sres []plugin
455462
}
456463

457464
// parseResult parse search result, return the data structure
458-
func (sr *searchRepo) parseResult(ctx context.Context, res []map[string][]byte) (resp []schema.SearchResult, err error) {
465+
func (sr *searchRepo) parseResult(ctx context.Context, res []map[string][]byte) (resp []*schema.SearchResult, err error) {
466+
questionIDs := make([]string, 0)
467+
userIDs := make([]string, 0)
468+
resultList := make([]*schema.SearchResult, 0)
459469
for _, r := range res {
460-
var (
461-
objectKey,
462-
status string
463-
464-
tags []schema.TagResp
465-
tagsEntity []entity.Tag
466-
object schema.SearchObject
467-
)
468-
objectKey, err = obj.GetObjectTypeStrByObjectID(string(r["id"]))
469-
if err != nil {
470-
continue
471-
}
472-
470+
questionIDs = append(questionIDs, string(r["question_id"]))
471+
userIDs = append(userIDs, string(r["user_id"]))
473472
tp, _ := time.ParseInLocation("2006-01-02 15:04:05", string(r["created_at"]), time.Local)
474-
475-
// get user info
476-
userInfo, _, e := sr.userCommon.GetUserBasicInfoByID(ctx, string(r["user_id"]))
477-
if e != nil {
478-
err = errors.InternalServer(reason.DatabaseError).WithError(e).WithStack()
479-
return
473+
object := &schema.SearchObject{
474+
ID: string(r["id"]),
475+
QuestionID: string(r["question_id"]),
476+
Title: string(r["title"]),
477+
Excerpt: htmltext.FetchExcerpt(string(r["parsed_text"]), "...", 240),
478+
CreatedAtParsed: tp.Unix(),
479+
UserInfo: &schema.SearchObjectUser{
480+
ID: string(r["user_id"]),
481+
},
482+
Tags: make([]*schema.TagResp, 0),
483+
VoteCount: converter.StringToInt(string(r["vote_count"])),
484+
Accepted: string(r["accepted"]) == "2",
485+
AnswerCount: converter.StringToInt(string(r["answer_count"])),
480486
}
481487

482-
// get tags
483-
err = sr.data.DB.Context(ctx).
484-
Select("`display_name`,`slug_name`,`main_tag_slug_name`,`recommend`,`reserved`").
485-
Table("tag").
486-
Join("INNER", "tag_rel", "tag.id = tag_rel.tag_id").
487-
Where(builder.Eq{"tag_rel.object_id": r["question_id"]}).
488-
And(builder.Eq{"tag_rel.status": entity.TagRelStatusAvailable}).
489-
UseBool("recommend", "reserved").
490-
OrderBy("tag.recommend DESC, tag.reserved DESC, tag.id DESC").
491-
Find(&tagsEntity)
492-
488+
objectKey, err := obj.GetObjectTypeStrByObjectID(string(r["id"]))
493489
if err != nil {
494-
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
495-
return
490+
continue
496491
}
497-
_ = copier.Copy(&tags, tagsEntity)
492+
498493
switch objectKey {
499494
case "question":
500495
for k, v := range entity.AdminQuestionSearchStatus {
501496
if v == converter.StringToInt(string(r["status"])) {
502-
status = k
497+
object.StatusStr = k
503498
break
504499
}
505500
}
506501
case "answer":
507502
for k, v := range entity.AdminAnswerSearchStatus {
508503
if v == converter.StringToInt(string(r["status"])) {
509-
status = k
504+
object.StatusStr = k
510505
break
511506
}
512507
}
513508
}
514509

515-
object = schema.SearchObject{
516-
ID: string(r["id"]),
517-
QuestionID: string(r["question_id"]),
518-
Title: string(r["title"]),
519-
Excerpt: htmltext.FetchExcerpt(string(r["parsed_text"]), "...", 240),
520-
CreatedAtParsed: tp.Unix(),
521-
UserInfo: userInfo,
522-
Tags: tags,
523-
VoteCount: converter.StringToInt(string(r["vote_count"])),
524-
Accepted: string(r["accepted"]) == "2",
525-
AnswerCount: converter.StringToInt(string(r["answer_count"])),
526-
StatusStr: status,
527-
}
528-
resp = append(resp, schema.SearchResult{
510+
resultList = append(resultList, &schema.SearchResult{
529511
ObjectType: objectKey,
530512
Object: object,
531513
})
532514
}
533-
return
515+
516+
tagsMap, err := sr.tagCommon.BatchGetObjectTag(ctx, questionIDs)
517+
if err != nil {
518+
return nil, err
519+
}
520+
userInfoMap, err := sr.userCommon.BatchUserBasicInfoByID(ctx, userIDs)
521+
if err != nil {
522+
return nil, err
523+
}
524+
525+
for _, item := range resultList {
526+
tags, ok := tagsMap[item.Object.QuestionID]
527+
if ok {
528+
item.Object.Tags = tags
529+
}
530+
if userInfo := userInfoMap[item.Object.UserInfo.ID]; userInfo != nil {
531+
item.Object.UserInfo.Username = userInfo.Username
532+
item.Object.UserInfo.DisplayName = userInfo.DisplayName
533+
item.Object.UserInfo.Rank = userInfo.Rank
534+
item.Object.UserInfo.Status = userInfo.Status
535+
}
536+
}
537+
return resultList, nil
534538
}
535539

536540
func addRelevanceField(searchFields, words, fields []string) (res []string, args []interface{}) {

internal/schema/search_schema.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,21 @@ type SearchObject struct {
9090
Accepted bool `json:"accepted"`
9191
AnswerCount int `json:"answer_count"`
9292
// user info
93-
UserInfo *UserBasicInfo `json:"user_info"`
93+
UserInfo *SearchObjectUser `json:"user_info"`
9494
// tags
95-
Tags []TagResp `json:"tags"`
95+
Tags []*TagResp `json:"tags"`
9696
// Status
9797
StatusStr string `json:"status"`
9898
}
9999

100+
type SearchObjectUser struct {
101+
ID string `json:"id"`
102+
Username string `json:"username"`
103+
DisplayName string `json:"display_name"`
104+
Rank int `json:"rank"`
105+
Status string `json:"status"`
106+
}
107+
100108
type TagResp struct {
101109
ID string `json:"-"`
102110
SlugName string `json:"slug_name"`
@@ -111,13 +119,13 @@ type SearchResult struct {
111119
// object_type
112120
ObjectType string `json:"object_type"`
113121
// this object
114-
Object SearchObject `json:"object"`
122+
Object *SearchObject `json:"object"`
115123
}
116124

117125
type SearchResp struct {
118126
Total int64 `json:"count"`
119127
// search response
120-
SearchResults []SearchResult `json:"list"`
128+
SearchResults []*SearchResult `json:"list"`
121129
}
122130

123131
type SearchDescResp struct {

internal/service/search_common/search.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
)
88

99
type SearchRepo interface {
10-
SearchContents(ctx context.Context, words []string, tagIDs []string, userID string, votes, page, size int, order string) (resp []schema.SearchResult, total int64, err error)
11-
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)
12-
SearchAnswers(ctx context.Context, words []string, tagIDs []string, accepted bool, questionID string, page, size int, order string) (resp []schema.SearchResult, total int64, err error)
13-
ParseSearchPluginResult(ctx context.Context, sres []plugin.SearchResult) (resp []schema.SearchResult, err error)
10+
SearchContents(ctx context.Context, words []string, tagIDs []string, userID string, votes, page, size int, order string) (resp []*schema.SearchResult, total int64, err error)
11+
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)
12+
SearchAnswers(ctx context.Context, words []string, tagIDs []string, accepted bool, questionID string, page, size int, order string) (resp []*schema.SearchResult, total int64, err error)
13+
ParseSearchPluginResult(ctx context.Context, sres []plugin.SearchResult) (resp []*schema.SearchResult, err error)
1414
}

internal/service/tag_common/tag_common.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,9 @@ func (ts *TagCommonService) tagFormatRecommendAndReserved(ctx context.Context, t
436436
// BatchGetObjectTag batch get object tag
437437
func (ts *TagCommonService) BatchGetObjectTag(ctx context.Context, objectIds []string) (map[string][]*schema.TagResp, error) {
438438
objectIDTagMap := make(map[string][]*schema.TagResp)
439+
if len(objectIds) == 0 {
440+
return objectIDTagMap, nil
441+
}
439442
objectTagRelList, err := ts.tagRelRepo.BatchGetObjectTagRelList(ctx, objectIds)
440443
if err != nil {
441444
return objectIDTagMap, err

internal/service/user_common/user.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,12 @@ func (us *UserCommon) UpdateQuestionCount(ctx context.Context, userID string, nu
106106
return us.userRepo.UpdateQuestionCount(ctx, userID, num)
107107
}
108108

109-
func (us *UserCommon) BatchUserBasicInfoByID(ctx context.Context, IDs []string) (map[string]*schema.UserBasicInfo, error) {
109+
func (us *UserCommon) BatchUserBasicInfoByID(ctx context.Context, userIDs []string) (map[string]*schema.UserBasicInfo, error) {
110110
userMap := make(map[string]*schema.UserBasicInfo)
111-
userList, err := us.userRepo.BatchGetByID(ctx, IDs)
111+
if len(userIDs) == 0 {
112+
return userMap, nil
113+
}
114+
userList, err := us.userRepo.BatchGetByID(ctx, userIDs)
112115
if err != nil {
113116
return userMap, err
114117
}

0 commit comments

Comments
 (0)