mirror of https://gitee.com/answerdev/answer.git
feat(search): convert answer and question to search content
This commit is contained in:
parent
a5f74a656f
commit
0d2c9cfd45
|
@ -391,6 +391,6 @@ func (ar *answerRepo) updateSearch(ctx context.Context, answerID string) (err er
|
||||||
Score: int64(answer.VoteCount),
|
Score: int64(answer.VoteCount),
|
||||||
HasAccepted: answer.Accepted == schema.AnswerAcceptedEnable,
|
HasAccepted: answer.Accepted == schema.AnswerAcceptedEnable,
|
||||||
}
|
}
|
||||||
err = s.UpdateContent(ctx, answerID, content)
|
err = s.UpdateContent(ctx, content)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -467,6 +467,6 @@ func (qr *questionRepo) updateSearch(ctx context.Context, questionID string) (er
|
||||||
Score: int64(question.VoteCount),
|
Score: int64(question.VoteCount),
|
||||||
HasAccepted: question.AcceptedAnswerID != "" && question.AcceptedAnswerID != "0",
|
HasAccepted: question.AcceptedAnswerID != "" && question.AcceptedAnswerID != "0",
|
||||||
}
|
}
|
||||||
err = s.UpdateContent(ctx, questionID, content)
|
err = s.UpdateContent(ctx, content)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,13 @@ package search_sync
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/answerdev/answer/internal/base/constant"
|
||||||
"github.com/answerdev/answer/internal/base/data"
|
"github.com/answerdev/answer/internal/base/data"
|
||||||
"github.com/answerdev/answer/internal/entity"
|
"github.com/answerdev/answer/internal/entity"
|
||||||
|
"github.com/answerdev/answer/internal/schema"
|
||||||
|
"github.com/answerdev/answer/pkg/uid"
|
||||||
"github.com/answerdev/answer/plugin"
|
"github.com/answerdev/answer/plugin"
|
||||||
|
"github.com/segmentfault/pacman/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewPluginSyncer(data *data.Data) plugin.SearchSyncer {
|
func NewPluginSyncer(data *data.Data) plugin.SearchSyncer {
|
||||||
|
@ -15,16 +19,103 @@ type PluginSyncer struct {
|
||||||
data *data.Data
|
data *data.Data
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PluginSyncer) GetAnswersPage(ctx context.Context, page, pageSize int) (answerList []*entity.Answer, err error) {
|
func (p *PluginSyncer) GetAnswersPage(ctx context.Context, page, pageSize int) (
|
||||||
answerList = make([]*entity.Answer, 0)
|
answerList []*plugin.SearchContent, err error) {
|
||||||
|
answers := make([]*entity.Answer, 0)
|
||||||
startNum := (page - 1) * pageSize
|
startNum := (page - 1) * pageSize
|
||||||
err = p.data.DB.Context(ctx).Limit(pageSize, startNum).Find(&answerList)
|
err = p.data.DB.Context(ctx).Limit(pageSize, startNum).Find(&answers)
|
||||||
return answerList, err
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return p.convertAnswers(ctx, answers)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PluginSyncer) GetQuestionsPage(ctx context.Context, page, pageSize int) (questionList []*entity.Question, err error) {
|
func (p *PluginSyncer) GetQuestionsPage(ctx context.Context, page, pageSize int) (
|
||||||
questionList = make([]*entity.Question, 0)
|
questionList []*plugin.SearchContent, err error) {
|
||||||
|
questions := make([]*entity.Question, 0)
|
||||||
startNum := (page - 1) * pageSize
|
startNum := (page - 1) * pageSize
|
||||||
err = p.data.DB.Context(ctx).Limit(pageSize, startNum).Find(&questionList)
|
err = p.data.DB.Context(ctx).Limit(pageSize, startNum).Find(&questions)
|
||||||
return questionList, err
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return p.convertQuestions(ctx, questions)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PluginSyncer) convertAnswers(ctx context.Context, answers []*entity.Answer) (
|
||||||
|
answerList []*plugin.SearchContent, err error) {
|
||||||
|
for _, answer := range answers {
|
||||||
|
question := &entity.Question{}
|
||||||
|
exist, err := p.data.DB.Context(ctx).Where("id = ?", answer.QuestionID).Get(question)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("get question failed %s", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !exist {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
tagListList := make([]*entity.TagRel, 0)
|
||||||
|
tags := make([]string, 0)
|
||||||
|
err = p.data.DB.Context(ctx).Where("object_id = ?", uid.DeShortID(question.ID)).
|
||||||
|
Where("status = ?", entity.TagRelStatusAvailable).Find(&tagListList)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("get tag list failed %s", err)
|
||||||
|
}
|
||||||
|
for _, tag := range tagListList {
|
||||||
|
tags = append(tags, tag.TagID)
|
||||||
|
}
|
||||||
|
|
||||||
|
content := &plugin.SearchContent{
|
||||||
|
ObjectID: answer.ID,
|
||||||
|
Title: question.Title,
|
||||||
|
Type: constant.AnswerObjectType,
|
||||||
|
Content: answer.ParsedText,
|
||||||
|
Answers: 0,
|
||||||
|
Status: plugin.SearchContentStatus(answer.Status),
|
||||||
|
Tags: tags,
|
||||||
|
QuestionID: answer.QuestionID,
|
||||||
|
UserID: answer.UserID,
|
||||||
|
Views: int64(question.ViewCount),
|
||||||
|
Created: answer.CreatedAt.Unix(),
|
||||||
|
Active: answer.UpdatedAt.Unix(),
|
||||||
|
Score: int64(answer.VoteCount),
|
||||||
|
HasAccepted: answer.Accepted == schema.AnswerAcceptedEnable,
|
||||||
|
}
|
||||||
|
answerList = append(answerList, content)
|
||||||
|
}
|
||||||
|
return answerList, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PluginSyncer) convertQuestions(ctx context.Context, questions []*entity.Question) (
|
||||||
|
questionList []*plugin.SearchContent, err error) {
|
||||||
|
for _, question := range questions {
|
||||||
|
tagListList := make([]*entity.TagRel, 0)
|
||||||
|
tags := make([]string, 0)
|
||||||
|
err := p.data.DB.Context(ctx).Where("object_id = ?", question.ID).
|
||||||
|
Where("status = ?", entity.TagRelStatusAvailable).Find(&tagListList)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("get tag list failed %s", err)
|
||||||
|
}
|
||||||
|
for _, tag := range tagListList {
|
||||||
|
tags = append(tags, tag.TagID)
|
||||||
|
}
|
||||||
|
content := &plugin.SearchContent{
|
||||||
|
ObjectID: question.ID,
|
||||||
|
Title: question.Title,
|
||||||
|
Type: constant.QuestionObjectType,
|
||||||
|
Content: question.ParsedText,
|
||||||
|
Answers: int64(question.AnswerCount),
|
||||||
|
Status: plugin.SearchContentStatus(question.Status),
|
||||||
|
Tags: tags,
|
||||||
|
QuestionID: question.ID,
|
||||||
|
UserID: question.UserID,
|
||||||
|
Views: int64(question.ViewCount),
|
||||||
|
Created: question.CreatedAt.Unix(),
|
||||||
|
Active: question.UpdatedAt.Unix(),
|
||||||
|
Score: int64(question.VoteCount),
|
||||||
|
HasAccepted: question.AcceptedAnswerID != "" && question.AcceptedAnswerID != "0",
|
||||||
|
}
|
||||||
|
questionList = append(questionList, content)
|
||||||
|
}
|
||||||
|
return questionList, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package plugin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/answerdev/answer/internal/entity"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type SearchResult struct {
|
type SearchResult struct {
|
||||||
|
@ -89,8 +88,8 @@ type Search interface {
|
||||||
SearchContents(ctx context.Context, cond *SearchBasicCond) (res []SearchResult, total int64, err error)
|
SearchContents(ctx context.Context, cond *SearchBasicCond) (res []SearchResult, total int64, err error)
|
||||||
SearchQuestions(ctx context.Context, cond *SearchBasicCond) (res []SearchResult, total int64, err error)
|
SearchQuestions(ctx context.Context, cond *SearchBasicCond) (res []SearchResult, total int64, err error)
|
||||||
SearchAnswers(ctx context.Context, cond *SearchBasicCond) (res []SearchResult, total int64, err error)
|
SearchAnswers(ctx context.Context, cond *SearchBasicCond) (res []SearchResult, total int64, err error)
|
||||||
UpdateContent(ctx context.Context, contentID string, content *SearchContent) error
|
UpdateContent(ctx context.Context, content *SearchContent) (err error)
|
||||||
DeleteContent(ctx context.Context, contentID string) error
|
DeleteContent(ctx context.Context, objectID string) (err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type SearchDesc struct {
|
type SearchDesc struct {
|
||||||
|
@ -99,8 +98,8 @@ type SearchDesc struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type SearchSyncer interface {
|
type SearchSyncer interface {
|
||||||
GetAnswersPage(ctx context.Context, page, pageSize int) (answerList []*entity.Answer, err error)
|
GetAnswersPage(ctx context.Context, page, pageSize int) (answerList []*SearchContent, err error)
|
||||||
GetQuestionsPage(ctx context.Context, page, pageSize int) (questionList []*entity.Question, err error)
|
GetQuestionsPage(ctx context.Context, page, pageSize int) (questionList []*SearchContent, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
Loading…
Reference in New Issue