Skip to content

Commit

Permalink
update for synonym tag logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hgaol committed Dec 18, 2023
1 parent ec87e6d commit b9c840a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 15 deletions.
14 changes: 9 additions & 5 deletions internal/repo/search_common/search_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,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, size int, order string) (resp []*schema.SearchResult, total int64, err error) {
words = filterWords(words)

var (
Expand Down Expand Up @@ -160,8 +160,12 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagIDs
ast + ".tag_id": tagID,
ast + ".status": entity.TagRelStatusAvailable,
})
argsQ = append(argsQ, entity.TagRelStatusAvailable, tagID)
argsA = append(argsA, entity.TagRelStatusAvailable, tagID)
argsQ = append(argsQ, entity.TagRelStatusAvailable)
argsA = append(argsA, entity.TagRelStatusAvailable)
for _, t := range tagID {
argsQ = append(argsQ, t)
argsA = append(argsA, t)
}
}

// check user
Expand Down Expand Up @@ -236,7 +240,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, size int, order string) (resp []*schema.SearchResult, total int64, err error) {
words = filterWords(words)
var (
qfs = qFields
Expand Down Expand Up @@ -343,7 +347,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, size int, order string) (resp []*schema.SearchResult, total int64, err error) {
words = filterWords(words)

var (
Expand Down
2 changes: 1 addition & 1 deletion internal/schema/search_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type SearchCondition struct {
// only show this question's answer
QuestionID string
// search query tags
Tags []string
Tags [][]string
// search query keywords
Words []string
}
Expand Down
6 changes: 3 additions & 3 deletions internal/service/search_common/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
)

type SearchRepo interface {
SearchContents(ctx context.Context, words []string, tagIDs []string, userID string, votes, page, size int, order string) (resp []*schema.SearchResult, total int64, err error)
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)
SearchAnswers(ctx context.Context, words []string, tagIDs []string, accepted bool, questionID string, page, size int, order string) (resp []*schema.SearchResult, total int64, err error)
SearchContents(ctx context.Context, words []string, tagIDs [][]string, userID string, votes, page, size int, order string) (resp []*schema.SearchResult, total int64, err error)
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)
SearchAnswers(ctx context.Context, words []string, tagIDs [][]string, accepted bool, questionID string, page, size int, order string) (resp []*schema.SearchResult, total int64, err error)
ParseSearchPluginResult(ctx context.Context, sres []plugin.SearchResult) (resp []*schema.SearchResult, err error)
}
18 changes: 13 additions & 5 deletions internal/service/search_parser/search_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (sp *SearchParser) ParseStructure(ctx context.Context, dto *schema.SearchDT
}

// parseTags parse search tags, return tag ids array
func (sp *SearchParser) parseTags(ctx context.Context, query *string) (tags []string) {
func (sp *SearchParser) parseTags(ctx context.Context, query *string) (tags [][]string) {
var (
// expire tag pattern
exprTag = `\[(.*?)\]`
Expand All @@ -119,17 +119,25 @@ func (sp *SearchParser) parseTags(ctx context.Context, query *string) (tags []st
return
}

tags = []string{}
tags = make([][]string, 0)
for _, item := range res {
tagGroup := make([]string, 0)
tag, exists, err := sp.tagCommonService.GetTagBySlugName(ctx, item[1])
if err != nil || !exists {
continue
}
tagGroup = append(tagGroup, tag.ID)
if tag.MainTagID > 0 {
tags = append(tags, fmt.Sprintf("%d", tag.MainTagID))
} else {
tags = append(tags, tag.ID)
tagGroup = append(tagGroup, fmt.Sprintf("%d", tag.MainTagID))
}
synIDs, err := sp.tagCommonService.GetTagIDsByMainTagID(ctx, tag.ID)
if err != nil || !exists {
continue
}
tagGroup = append(tagGroup, tag.ID)
tagGroup = append(tagGroup, synIDs...)
tagGroup = converter.UniqueArray(tagGroup)
tags = append(tags, tagGroup)
}

// limit maximum 5 tags
Expand Down
12 changes: 12 additions & 0 deletions pkg/converter/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ func ArrayNotInArray(original []string, search []string) []string {
}
return result
}

func UniqueArray[T comparable](input []T) []T {
result := make([]T, 0, len(input))
seen := make(map[T]bool, len(input))
for _, element := range input {
if !seen[element] {
result = append(result, element)
seen[element] = true
}
}
return result
}
2 changes: 1 addition & 1 deletion plugin/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type SearchBasicCond struct {
// The keywords for search.
Words []string
// TagIDs is a list of tag IDs.
TagIDs []string
TagIDs [][]string
// The object's owner user ID.
UserID string
// The order of the search result.
Expand Down

0 comments on commit b9c840a

Please sign in to comment.