@@ -3,6 +3,7 @@ package search_common
33import (
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
536540func addRelevanceField (searchFields , words , fields []string ) (res []string , args []interface {}) {
0 commit comments