answer/plugin/search.go

110 lines
3.1 KiB
Go
Raw Normal View History

2023-05-17 11:23:52 +08:00
package plugin
import (
"context"
)
type SearchResult struct {
// ID content ID
ID string
// Type content type, example: "answer", "question"
Type string
}
type SearchContent struct {
2023-07-26 16:35:33 +08:00
ObjectID string `json:"objectID"`
Title string `json:"title"`
Type string `json:"type"`
Content string `json:"content"`
Answers int64 `json:"answers"`
Status SearchContentStatus `json:"status"`
Tags []string `json:"tags"`
QuestionID string `json:"questionID"`
UserID string `json:"userID"`
Views int64 `json:"views"`
Created int64 `json:"created"`
Active int64 `json:"active"`
Score int64 `json:"score"`
HasAccepted bool `json:"hasAccepted"`
2023-05-17 11:23:52 +08:00
}
2023-07-21 17:27:01 +08:00
type SearchBasicCond struct {
// From zero-based page number
Page int
// Page size
PageSize int
// The keywords for search.
Words []string
// TagIDs is a list of tag IDs.
TagIDs []string
// The object's owner user ID.
UserID string
// The order of the search result.
2023-07-26 16:35:33 +08:00
Order SearchOrderCond
2023-07-21 17:27:01 +08:00
// Weathers the question is accepted or not. Only support search question.
2023-07-26 16:35:33 +08:00
QuestionAccepted SearchAcceptedCond
2023-07-21 17:27:01 +08:00
// Weathers the answer is accepted or not. Only support search answer.
2023-07-26 16:35:33 +08:00
AnswerAccepted SearchAcceptedCond
2023-07-21 17:27:01 +08:00
// Only support search answer.
QuestionID string
// greater than or equal to the number of votes.
VoteAmount int
// greater than or equal to the number of views.
ViewAmount int
// greater than or equal to the number of answers. Only support search question.
AnswerAmount int
}
2023-07-26 16:35:33 +08:00
type SearchAcceptedCond int
type SearchContentStatus int
type SearchOrderCond string
2023-07-21 17:27:01 +08:00
const (
2023-07-26 16:35:33 +08:00
AcceptedCondAll SearchAcceptedCond = iota
2023-07-21 17:27:01 +08:00
AcceptedCondTrue
AcceptedCondFalse
)
2023-07-26 16:35:33 +08:00
const (
SearchContentStatusAvailable = 1
SearchContentStatusDeleted = 10
)
const (
SearchNewestOrder SearchOrderCond = "newest"
SearchActiveOrder SearchOrderCond = "active"
SearchScoreOrder SearchOrderCond = "score"
SearchRelevanceOrder SearchOrderCond = "relevance"
)
2023-05-17 11:23:52 +08:00
type Search interface {
Base
Description() SearchDesc
2023-08-17 11:30:44 +08:00
RegisterSyncer(ctx context.Context, syncer SearchSyncer)
2023-07-21 17:27:01 +08:00
SearchContents(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)
UpdateContent(ctx context.Context, content *SearchContent) (err error)
DeleteContent(ctx context.Context, objectID string) (err error)
2023-05-17 11:23:52 +08:00
}
type SearchDesc struct {
// A svg icon it wil be display in search result page
Icon string `json:"icon"`
}
2023-08-17 11:30:44 +08:00
type SearchSyncer interface {
GetAnswersPage(ctx context.Context, page, pageSize int) (answerList []*SearchContent, err error)
GetQuestionsPage(ctx context.Context, page, pageSize int) (questionList []*SearchContent, err error)
2023-08-17 11:30:44 +08:00
}
2023-05-17 11:23:52 +08:00
var (
// CallUserCenter is a function that calls all registered parsers
CallSearch,
registerSearch = MakePlugin[Search](false)
)