A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
fix(issues/136): fixed tags filter search result
kumfo7e8906ebb120
diff --git a/docs/docs.go b/docs/docs.goindex 05b8d73a..5ff61d6e 100644--- a/docs/docs.go+++ b/docs/docs.go@@ -6999,7 +6999,7 @@ const docTemplate = `{"login": {"$ref": "#/definitions/schema.SiteLoginResp"},- "site__seo": {+ "site_seo": {"$ref": "#/definitions/schema.SiteSeoReq"},"theme": {diff --git a/docs/swagger.json b/docs/swagger.jsonindex 913c2834..d002378e 100644--- a/docs/swagger.json+++ b/docs/swagger.json@@ -6987,7 +6987,7 @@"login": {"$ref": "#/definitions/schema.SiteLoginResp"},- "site__seo": {+ "site_seo": {"$ref": "#/definitions/schema.SiteSeoReq"},"theme": {diff --git a/docs/swagger.yaml b/docs/swagger.yamlindex 06837519..b69af5ff 100644--- a/docs/swagger.yaml+++ b/docs/swagger.yaml@@ -1292,7 +1292,7 @@ definitions:$ref: '#/definitions/schema.SiteInterfaceResp'login:$ref: '#/definitions/schema.SiteLoginResp'- site__seo:+ site_seo:$ref: '#/definitions/schema.SiteSeoReq'theme:$ref: '#/definitions/schema.SiteThemeResp'diff --git a/internal/repo/search_common/search_repo.go b/internal/repo/search_common/search_repo.goindex 5c5bc25f..679e6478 100644--- a/internal/repo/search_common/search_repo.go+++ b/internal/repo/search_common/search_repo.go@@ -3,6 +3,7 @@ package search_commonimport ("context""fmt"+ "strconv""strings""time"@@ -122,10 +123,14 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagIDs// check tagif len(tagIDs) > 0 {- b.Join("INNER", "tag_rel", "question.id = tag_rel.object_id").- Where(builder.In("tag_rel.tag_id", tagIDs))- for _, tagID := range tagIDs {+ for ti, tagID := range tagIDs {+ ast := "tag_rel" + strconv.Itoa(ti)+ b.Join("INNER", "tag_rel as "+ast, "question.id = "+ast+".object_id").+ And(builder.Eq{ast + ".tag_id": tagID})+ ub.Join("INNER", "tag_rel as "+ast, "question_id = "+ast+".object_id").+ And(builder.Eq{ast + ".tag_id": tagID})argsQ = append(argsQ, tagID)+ argsA = append(argsA, tagID)}}@@ -201,7 +206,7 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagIDs}// SearchQuestions search question data-func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, notAccepted bool, views, answers int, page, size int, order string) (resp []schema.SearchResp, total int64, err error) {+func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, tagIDs []string, notAccepted bool, views, answers int, page, size int, order string) (resp []schema.SearchResp, total int64, err error) {words = filterWords(words)var (qfs = qFields@@ -232,6 +237,16 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, notAc}}+ // check tag+ if len(tagIDs) > 0 {+ for ti, tagID := range tagIDs {+ ast := "tag_rel" + strconv.Itoa(ti)+ b.Join("INNER", "tag_rel as "+ast, "question.id = "+ast+".object_id").+ And(builder.Eq{ast + ".tag_id": tagID})+ args = append(args, tagID)+ }+ }+// check need filter has not acceptedif notAccepted {b.And(builder.Eq{"accepted_answer_id": 0})@@ -332,12 +347,12 @@ func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, tagIDs}}- // check tags// check tagif len(tagIDs) > 0 {- b.Join("INNER", "tag_rel", "question.id = tag_rel.object_id").- Where(builder.In("tag_rel.tag_id", tagIDs))- for _, tagID := range tagIDs {+ for ti, tagID := range tagIDs {+ ast := "tag_rel" + strconv.Itoa(ti)+ b.Join("INNER", "tag_rel as "+ast, "question_id = "+ast+".object_id").+ And(builder.Eq{ast + ".tag_id": tagID})args = append(args, tagID)}}diff --git a/internal/service/search_common/search.go b/internal/service/search_common/search.goindex dff57332..b9fc3041 100644--- a/internal/service/search_common/search.go+++ b/internal/service/search_common/search.go@@ -7,6 +7,6 @@ import (type SearchRepo interface {SearchContents(ctx context.Context, words []string, tagIDs []string, userID string, votes, page, size int, order string) (resp []schema.SearchResp, total int64, err error)- SearchQuestions(ctx context.Context, words []string, notAccepted bool, views, answers int, page, size int, order string) (resp []schema.SearchResp, total int64, err error)+ SearchQuestions(ctx context.Context, words []string, tagIDs []string, notAccepted bool, views, answers int, page, size int, order string) (resp []schema.SearchResp, total int64, err error)SearchAnswers(ctx context.Context, words []string, tagIDs []string, accepted bool, questionID string, page, size int, order string) (resp []schema.SearchResp, total int64, err error)}diff --git a/internal/service/search_parser/search_parser.go b/internal/service/search_parser/search_parser.goindex e90ab1f8..2a938d32 100644--- a/internal/service/search_parser/search_parser.go+++ b/internal/service/search_parser/search_parser.go@@ -27,19 +27,19 @@ func NewSearchParser(tagCommonService *tag_common.TagCommonService, userCommon *// but if match two type, it will return falsefunc (sp *SearchParser) ParseStructure(dto *schema.SearchDTO) (searchType string,- // search all+// search alluserID string,votes int,- // search questions+// search questionsnotAccepted bool,isQuestion bool,views,answers int,- // search answers+// search answersaccepted bool,questionID string,isAnswer bool,- // common fields+// common fieldstags,words []string,) {@@ -112,7 +112,12 @@ func (sp *SearchParser) ParseStructure(dto *schema.SearchDTO) (a = 1}- words = strings.Split(query, " ")+ if len(strings.TrimSpace(query)) > 0 {+ words = strings.Split(strings.TrimSpace(query), " ")+ } else {+ words = []string{}+ }+if len(withWords) > 0 {words = append(withWords, words...)}@@ -127,9 +132,16 @@ func (sp *SearchParser) ParseStructure(dto *schema.SearchDTO) (if len(words) > 0 {searchType = "all"all = 1+ } else if isAnswer {+ searchType = "answer"+ a = 1+ all = 0+ q = 0} else {searchType = "question"q = 1+ all = 0+ a = 0}}@@ -160,13 +172,14 @@ func (sp *SearchParser) parseTags(query *string) (tags []string) {if len(res) == 0 {return}- tags = make([]string, len(res))- for i, item := range res {++ tags = []string{}+ for _, item := range res {tag, exists, err := sp.tagCommonService.GetTagBySlugName(context.TODO(), item[1])if err != nil || !exists {continue}- tags[i] = tag.ID+ tags = append(tags, tag.ID)}// limit maximum 5 tags@@ -233,6 +246,9 @@ func (sp *SearchParser) parseWithin(query *string) (words []string) {matches := re.FindAllStringSubmatch(q, -1)words = []string{}for _, match := range matches {+ if len(match[1]) == 0 {+ continue+ }words = append(words, match[1])}q = re.ReplaceAllString(q, "")diff --git a/internal/service/search_service.go b/internal/service/search_service.goindex 61b622fb..66d762ab 100644--- a/internal/service/search_service.go+++ b/internal/service/search_service.go@@ -31,19 +31,19 @@ func (ss *SearchService) Search(ctx context.Context, dto *schema.SearchDTO) (res// search typesearchType,- // search all+ // search alluserID,votes,- // search questions+ // search questionsnotAccepted,_,views,answers,- // search answers+ // search answersaccepted,questionID,_,- // common fields+ // common fieldstags,words := ss.searchParser.ParseStructure(dto)@@ -54,7 +54,7 @@ func (ss *SearchService) Search(ctx context.Context, dto *schema.SearchDTO) (resreturn nil, 0, nil, err}case "question":- resp, total, err = ss.searchRepo.SearchQuestions(ctx, words, notAccepted, views, answers, dto.Page, dto.Size, dto.Order)+ resp, total, err = ss.searchRepo.SearchQuestions(ctx, words, tags, notAccepted, views, answers, dto.Page, dto.Size, dto.Order)case "answer":resp, total, err = ss.searchRepo.SearchAnswers(ctx, words, tags, accepted, questionID, dto.Page, dto.Size, dto.Order)}