Skip to content

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(tags): tags in search result should be sort

LinkinStar302d1362b0f5
diff --git a/internal/repo/search_common/search_repo.go b/internal/repo/search_common/search_repo.go
index c94690a2..7719d373 100644
--- a/internal/repo/search_common/search_repo.go
+++ b/internal/repo/search_common/search_repo.go
@@ -466,6 +466,7 @@ func (sr *searchRepo) parseResult(ctx context.Context, res []map[string][]byte)
Where(builder.Eq{"tag_rel.object_id": r["question_id"]}).
And(builder.Eq{"tag_rel.status": entity.TagRelStatusAvailable}).
UseBool("recommend", "reserved").
+ OrderBy("tag.recommend DESC, tag.reserved DESC, tag.id DESC").
Find(&tagsEntity)
if err != nil {
diff --git a/internal/schema/search_schema.go b/internal/schema/search_schema.go
index ecb3ae32..51ec0f6a 100644
--- a/internal/schema/search_schema.go
+++ b/internal/schema/search_schema.go
@@ -26,6 +26,7 @@ type SearchObject struct {
}
type TagResp struct {
+ ID string `json:"-"`
SlugName string `json:"slug_name"`
DisplayName string `json:"display_name"`
// if main tag slug name is not empty, this tag is synonymous with the main tag
diff --git a/internal/service/tag_common/tag_common.go b/internal/service/tag_common/tag_common.go
index d4531329..877e21bb 100644
--- a/internal/service/tag_common/tag_common.go
+++ b/internal/service/tag_common/tag_common.go
@@ -330,28 +330,31 @@ func (ts *TagCommonService) tagFormatRecommendAndReserved(ctx context.Context, t
// BatchGetObjectTag batch get object tag
func (ts *TagCommonService) BatchGetObjectTag(ctx context.Context, objectIds []string) (map[string][]*schema.TagResp, error) {
objectIDTagMap := make(map[string][]*schema.TagResp)
- tagIDList := make([]string, 0)
- tagsInfoMap := make(map[string]*entity.Tag)
-
- tagList, err := ts.tagRelRepo.BatchGetObjectTagRelList(ctx, objectIds)
+ objectTagRelList, err := ts.tagRelRepo.BatchGetObjectTagRelList(ctx, objectIds)
if err != nil {
return objectIDTagMap, err
}
- for _, tag := range tagList {
+ tagIDList := make([]string, 0)
+ for _, tag := range objectTagRelList {
tagIDList = append(tagIDList, tag.TagID)
}
tagsInfoList, err := ts.GetTagListByIDs(ctx, tagIDList)
if err != nil {
return objectIDTagMap, err
}
- for _, item := range tagsInfoList {
- tagsInfoMap[item.ID] = item
+ tagsInfoMapping := make(map[string]*entity.Tag)
+ tagsRank := make(map[string]int) // Used for sorting
+ for idx, item := range tagsInfoList {
+ tagsInfoMapping[item.ID] = item
+ tagsRank[item.ID] = idx
}
- for _, item := range tagList {
- _, ok := tagsInfoMap[item.TagID]
+
+ for _, item := range objectTagRelList {
+ _, ok := tagsInfoMapping[item.TagID]
if ok {
- tagInfo := tagsInfoMap[item.TagID]
+ tagInfo := tagsInfoMapping[item.TagID]
t := &schema.TagResp{
+ ID: tagInfo.ID,
SlugName: tagInfo.SlugName,
DisplayName: tagInfo.DisplayName,
MainTagSlugName: tagInfo.MainTagSlugName,
@@ -361,12 +364,10 @@ func (ts *TagCommonService) BatchGetObjectTag(ctx context.Context, objectIds []s
objectIDTagMap[item.ObjectID] = append(objectIDTagMap[item.ObjectID], t)
}
}
- for _, taglist := range objectIDTagMap {
- sort.SliceStable(taglist, func(i, j int) bool {
- return taglist[i].Reserved
- })
- sort.SliceStable(taglist, func(i, j int) bool {
- return taglist[i].Recommend
+ // The sorting in tagsRank is correct, object tags should be sorted by tagsRank
+ for _, objectTags := range objectIDTagMap {
+ sort.SliceStable(objectTags, func(i, j int) bool {
+ return tagsRank[objectTags[i].ID] < tagsRank[objectTags[j].ID]
})
}
return objectIDTagMap, nil