Skip to content

Commit 0d2c9cf

Browse files
committed
feat(search): convert answer and question to search content
1 parent a5f74a6 commit 0d2c9cf

4 files changed

Lines changed: 105 additions & 15 deletions

File tree

internal/repo/answer/answer_repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,6 @@ func (ar *answerRepo) updateSearch(ctx context.Context, answerID string) (err er
391391
Score: int64(answer.VoteCount),
392392
HasAccepted: answer.Accepted == schema.AnswerAcceptedEnable,
393393
}
394-
err = s.UpdateContent(ctx, answerID, content)
394+
err = s.UpdateContent(ctx, content)
395395
return
396396
}

internal/repo/question/question_repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,6 @@ func (qr *questionRepo) updateSearch(ctx context.Context, questionID string) (er
467467
Score: int64(question.VoteCount),
468468
HasAccepted: question.AcceptedAnswerID != "" && question.AcceptedAnswerID != "0",
469469
}
470-
err = s.UpdateContent(ctx, questionID, content)
470+
err = s.UpdateContent(ctx, content)
471471
return
472472
}

internal/repo/search_sync/search_sync.go

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ package search_sync
22

33
import (
44
"context"
5+
"github.com/answerdev/answer/internal/base/constant"
56
"github.com/answerdev/answer/internal/base/data"
67
"github.com/answerdev/answer/internal/entity"
8+
"github.com/answerdev/answer/internal/schema"
9+
"github.com/answerdev/answer/pkg/uid"
710
"github.com/answerdev/answer/plugin"
11+
"github.com/segmentfault/pacman/log"
812
)
913

1014
func NewPluginSyncer(data *data.Data) plugin.SearchSyncer {
@@ -15,16 +19,103 @@ type PluginSyncer struct {
1519
data *data.Data
1620
}
1721

18-
func (p *PluginSyncer) GetAnswersPage(ctx context.Context, page, pageSize int) (answerList []*entity.Answer, err error) {
19-
answerList = make([]*entity.Answer, 0)
22+
func (p *PluginSyncer) GetAnswersPage(ctx context.Context, page, pageSize int) (
23+
answerList []*plugin.SearchContent, err error) {
24+
answers := make([]*entity.Answer, 0)
2025
startNum := (page - 1) * pageSize
21-
err = p.data.DB.Context(ctx).Limit(pageSize, startNum).Find(&answerList)
22-
return answerList, err
26+
err = p.data.DB.Context(ctx).Limit(pageSize, startNum).Find(&answers)
27+
if err != nil {
28+
return nil, err
29+
}
30+
return p.convertAnswers(ctx, answers)
2331
}
2432

25-
func (p *PluginSyncer) GetQuestionsPage(ctx context.Context, page, pageSize int) (questionList []*entity.Question, err error) {
26-
questionList = make([]*entity.Question, 0)
33+
func (p *PluginSyncer) GetQuestionsPage(ctx context.Context, page, pageSize int) (
34+
questionList []*plugin.SearchContent, err error) {
35+
questions := make([]*entity.Question, 0)
2736
startNum := (page - 1) * pageSize
28-
err = p.data.DB.Context(ctx).Limit(pageSize, startNum).Find(&questionList)
29-
return questionList, err
37+
err = p.data.DB.Context(ctx).Limit(pageSize, startNum).Find(&questions)
38+
if err != nil {
39+
return nil, err
40+
}
41+
return p.convertQuestions(ctx, questions)
42+
}
43+
44+
func (p *PluginSyncer) convertAnswers(ctx context.Context, answers []*entity.Answer) (
45+
answerList []*plugin.SearchContent, err error) {
46+
for _, answer := range answers {
47+
question := &entity.Question{}
48+
exist, err := p.data.DB.Context(ctx).Where("id = ?", answer.QuestionID).Get(question)
49+
if err != nil {
50+
log.Errorf("get question failed %s", err)
51+
continue
52+
}
53+
if !exist {
54+
continue
55+
}
56+
57+
tagListList := make([]*entity.TagRel, 0)
58+
tags := make([]string, 0)
59+
err = p.data.DB.Context(ctx).Where("object_id = ?", uid.DeShortID(question.ID)).
60+
Where("status = ?", entity.TagRelStatusAvailable).Find(&tagListList)
61+
if err != nil {
62+
log.Errorf("get tag list failed %s", err)
63+
}
64+
for _, tag := range tagListList {
65+
tags = append(tags, tag.TagID)
66+
}
67+
68+
content := &plugin.SearchContent{
69+
ObjectID: answer.ID,
70+
Title: question.Title,
71+
Type: constant.AnswerObjectType,
72+
Content: answer.ParsedText,
73+
Answers: 0,
74+
Status: plugin.SearchContentStatus(answer.Status),
75+
Tags: tags,
76+
QuestionID: answer.QuestionID,
77+
UserID: answer.UserID,
78+
Views: int64(question.ViewCount),
79+
Created: answer.CreatedAt.Unix(),
80+
Active: answer.UpdatedAt.Unix(),
81+
Score: int64(answer.VoteCount),
82+
HasAccepted: answer.Accepted == schema.AnswerAcceptedEnable,
83+
}
84+
answerList = append(answerList, content)
85+
}
86+
return answerList, nil
87+
}
88+
89+
func (p *PluginSyncer) convertQuestions(ctx context.Context, questions []*entity.Question) (
90+
questionList []*plugin.SearchContent, err error) {
91+
for _, question := range questions {
92+
tagListList := make([]*entity.TagRel, 0)
93+
tags := make([]string, 0)
94+
err := p.data.DB.Context(ctx).Where("object_id = ?", question.ID).
95+
Where("status = ?", entity.TagRelStatusAvailable).Find(&tagListList)
96+
if err != nil {
97+
log.Errorf("get tag list failed %s", err)
98+
}
99+
for _, tag := range tagListList {
100+
tags = append(tags, tag.TagID)
101+
}
102+
content := &plugin.SearchContent{
103+
ObjectID: question.ID,
104+
Title: question.Title,
105+
Type: constant.QuestionObjectType,
106+
Content: question.ParsedText,
107+
Answers: int64(question.AnswerCount),
108+
Status: plugin.SearchContentStatus(question.Status),
109+
Tags: tags,
110+
QuestionID: question.ID,
111+
UserID: question.UserID,
112+
Views: int64(question.ViewCount),
113+
Created: question.CreatedAt.Unix(),
114+
Active: question.UpdatedAt.Unix(),
115+
Score: int64(question.VoteCount),
116+
HasAccepted: question.AcceptedAnswerID != "" && question.AcceptedAnswerID != "0",
117+
}
118+
questionList = append(questionList, content)
119+
}
120+
return questionList, nil
30121
}

plugin/search.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package plugin
22

33
import (
44
"context"
5-
"github.com/answerdev/answer/internal/entity"
65
)
76

87
type SearchResult struct {
@@ -89,8 +88,8 @@ type Search interface {
8988
SearchContents(ctx context.Context, cond *SearchBasicCond) (res []SearchResult, total int64, err error)
9089
SearchQuestions(ctx context.Context, cond *SearchBasicCond) (res []SearchResult, total int64, err error)
9190
SearchAnswers(ctx context.Context, cond *SearchBasicCond) (res []SearchResult, total int64, err error)
92-
UpdateContent(ctx context.Context, contentID string, content *SearchContent) error
93-
DeleteContent(ctx context.Context, contentID string) error
91+
UpdateContent(ctx context.Context, content *SearchContent) (err error)
92+
DeleteContent(ctx context.Context, objectID string) (err error)
9493
}
9594

9695
type SearchDesc struct {
@@ -99,8 +98,8 @@ type SearchDesc struct {
9998
}
10099

101100
type SearchSyncer interface {
102-
GetAnswersPage(ctx context.Context, page, pageSize int) (answerList []*entity.Answer, err error)
103-
GetQuestionsPage(ctx context.Context, page, pageSize int) (questionList []*entity.Question, err error)
101+
GetAnswersPage(ctx context.Context, page, pageSize int) (answerList []*SearchContent, err error)
102+
GetQuestionsPage(ctx context.Context, page, pageSize int) (questionList []*SearchContent, err error)
104103
}
105104

106105
var (

0 commit comments

Comments
 (0)