answer/internal/service/search_service.go

72 lines
2.3 KiB
Go
Raw Normal View History

2022-09-27 17:59:05 +08:00
package service
import (
"context"
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/internal/service/search_common"
2022-11-14 18:47:22 +08:00
"github.com/answerdev/answer/internal/service/search_parser"
2023-05-17 11:23:52 +08:00
"github.com/answerdev/answer/plugin"
2022-09-27 17:59:05 +08:00
)
type SearchService struct {
2022-11-14 18:47:22 +08:00
searchParser *search_parser.SearchParser
searchRepo search_common.SearchRepo
2022-09-27 17:59:05 +08:00
}
func NewSearchService(
2022-11-14 18:47:22 +08:00
searchParser *search_parser.SearchParser,
2022-09-27 17:59:05 +08:00
searchRepo search_common.SearchRepo,
) *SearchService {
return &SearchService{
2022-11-14 18:47:22 +08:00
searchParser: searchParser,
searchRepo: searchRepo,
2022-09-27 17:59:05 +08:00
}
}
2022-11-14 18:47:22 +08:00
// Search search contents
2023-07-20 18:48:35 +08:00
func (ss *SearchService) Search(ctx context.Context, dto *schema.SearchDTO) (resp []schema.SearchResp, total int64, err error) {
2022-10-12 16:09:20 +08:00
if dto.Page < 1 {
dto.Page = 1
}
2022-11-14 18:47:22 +08:00
// search type
2023-07-20 18:48:35 +08:00
cond := ss.searchParser.ParseStructure(ctx, dto)
2022-11-14 18:47:22 +08:00
2023-05-17 11:23:52 +08:00
// check search plugin
2023-07-20 18:48:35 +08:00
var s plugin.Search
2023-05-17 11:23:52 +08:00
_ = plugin.CallSearch(func(search plugin.Search) error {
s = search
return nil
})
// search plugin is not found, call system search
if s == nil {
2023-07-20 18:48:35 +08:00
if cond.SearchAll() {
resp, total, err = ss.searchRepo.SearchContents(ctx, cond.Words, cond.Tags, cond.UserID, cond.VoteAmount, dto.Page, dto.Size, dto.Order)
} else if cond.SearchQuestion() {
resp, total, err = ss.searchRepo.SearchQuestions(ctx, cond.Words, cond.Tags, cond.NotAccepted, cond.Views, cond.AnswerAmount, dto.Page, dto.Size, dto.Order)
} else if cond.SearchAnswer() {
resp, total, err = ss.searchRepo.SearchAnswers(ctx, cond.Words, cond.Tags, cond.Accepted, cond.QuestionID, dto.Page, dto.Size, dto.Order)
2023-05-17 11:23:52 +08:00
}
return
}
2023-07-20 18:48:35 +08:00
return ss.searchByPlugin(ctx, s, cond, dto)
}
2023-05-17 11:23:52 +08:00
2023-07-20 18:48:35 +08:00
func (ss *SearchService) searchByPlugin(ctx context.Context, finder plugin.Search, cond *schema.SearchCondition, dto *schema.SearchDTO) (resp []schema.SearchResp, total int64, err error) {
var res []plugin.SearchResult
if cond.SearchAll() {
2023-07-21 17:27:01 +08:00
res, total, err = finder.SearchContents(ctx, cond.Convert2PluginSearchCond(dto.Page, dto.Size, dto.Order))
2023-07-20 18:48:35 +08:00
} else if cond.SearchQuestion() {
2023-07-21 17:27:01 +08:00
res, total, err = finder.SearchQuestions(ctx, cond.Convert2PluginSearchCond(dto.Page, dto.Size, dto.Order))
2023-07-20 18:48:35 +08:00
} else if cond.SearchAnswer() {
2023-07-21 17:27:01 +08:00
res, total, err = finder.SearchAnswers(ctx, cond.Convert2PluginSearchCond(dto.Page, dto.Size, dto.Order))
2023-05-17 11:23:52 +08:00
}
2023-07-20 18:48:35 +08:00
resp, err = ss.searchRepo.ParseSearchPluginResult(ctx, res)
2023-05-17 11:23:52 +08:00
if err != nil {
2023-07-20 18:48:35 +08:00
return nil, 0, err
2022-09-27 17:59:05 +08:00
}
2022-11-14 18:47:22 +08:00
return
2022-09-27 17:59:05 +08:00
}