mirror of https://github.com/rocboss/paopao-ce
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
173 lines
4.6 KiB
173 lines
4.6 KiB
2 years ago
|
package search
|
||
2 years ago
|
|
||
|
import (
|
||
2 years ago
|
"fmt"
|
||
|
|
||
2 years ago
|
"github.com/Masterminds/semver/v3"
|
||
|
"github.com/meilisearch/meilisearch-go"
|
||
|
"github.com/rocboss/paopao-ce/internal/core"
|
||
|
"github.com/rocboss/paopao-ce/internal/model"
|
||
|
"github.com/rocboss/paopao-ce/pkg/json"
|
||
|
"github.com/sirupsen/logrus"
|
||
|
)
|
||
|
|
||
2 years ago
|
var (
|
||
|
_ core.TweetSearchService = (*meiliTweetSearchServant)(nil)
|
||
|
_ core.VersionInfo = (*meiliTweetSearchServant)(nil)
|
||
|
)
|
||
2 years ago
|
|
||
2 years ago
|
type meiliTweetSearchServant struct {
|
||
|
tweetSearchFilter
|
||
2 years ago
|
|
||
2 years ago
|
client *meilisearch.Client
|
||
|
index *meilisearch.Index
|
||
|
publicFilter string
|
||
|
privateFilter string
|
||
|
friendFilter string
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (s *meiliTweetSearchServant) Name() string {
|
||
2 years ago
|
return "Meili"
|
||
|
}
|
||
|
|
||
2 years ago
|
func (s *meiliTweetSearchServant) Version() *semver.Version {
|
||
2 years ago
|
return semver.MustParse("v0.2.0")
|
||
2 years ago
|
}
|
||
|
|
||
|
func (s *meiliTweetSearchServant) IndexName() string {
|
||
|
return s.index.UID
|
||
|
}
|
||
|
|
||
|
func (s *meiliTweetSearchServant) AddDocuments(data core.DocItems, primaryKey ...string) (bool, error) {
|
||
2 years ago
|
if _, err := s.index.AddDocuments(data, primaryKey...); err != nil {
|
||
2 years ago
|
logrus.Errorf("meiliTweetSearchServant.AddDocuments error: %v", err)
|
||
|
return false, err
|
||
|
}
|
||
|
return true, nil
|
||
|
}
|
||
|
|
||
|
func (s *meiliTweetSearchServant) DeleteDocuments(identifiers []string) error {
|
||
|
task, err := s.index.DeleteDocuments(identifiers)
|
||
|
if err != nil {
|
||
|
logrus.Errorf("meiliTweetSearchServant.DeleteDocuments error: %v", err)
|
||
|
return err
|
||
|
}
|
||
|
logrus.Debugf("meiliTweetSearchServant.DeleteDocuments task: %+v", task.Details)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func (s *meiliTweetSearchServant) Search(user *model.User, q *core.QueryReq, offset, limit int) (resp *core.QueryResp, err error) {
|
||
2 years ago
|
if q.Type == core.SearchTypeDefault && q.Query != "" {
|
||
2 years ago
|
resp, err = s.queryByContent(user, q, offset, limit)
|
||
2 years ago
|
} else if q.Type == core.SearchTypeTag && q.Query != "" {
|
||
2 years ago
|
resp, err = s.queryByTag(user, q, offset, limit)
|
||
|
} else {
|
||
|
resp, err = s.queryAny(user, offset, limit)
|
||
|
}
|
||
|
if err != nil {
|
||
|
logrus.Errorf("meiliTweetSearchServant.search searchType:%s query:%s error:%v", q.Type, q.Query, err)
|
||
|
return
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
logrus.Debugf("meiliTweetSearchServant.Search type:%s query:%s resp Hits:%d NbHits:%d offset: %d limit:%d ", q.Type, q.Query, len(resp.Items), resp.Total, offset, limit)
|
||
|
s.filterResp(user, resp)
|
||
|
return
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (s *meiliTweetSearchServant) queryByContent(user *model.User, q *core.QueryReq, offset, limit int) (*core.QueryResp, error) {
|
||
|
request := &meilisearch.SearchRequest{
|
||
2 years ago
|
Offset: int64(offset),
|
||
|
Limit: int64(limit),
|
||
|
Sort: []string{"is_top:desc", "latest_replied_on:desc"},
|
||
2 years ago
|
}
|
||
|
|
||
|
filter := s.filterList(user)
|
||
|
if len(filter) > 0 {
|
||
|
request.Filter = filter
|
||
|
}
|
||
|
|
||
|
// logrus.Debugf("meiliTweetSearchServant.queryByContent query:%s request%+v", q.Query, request)
|
||
|
resp, err := s.index.Search(q.Query, request)
|
||
2 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return s.postsFrom(resp)
|
||
|
}
|
||
|
|
||
2 years ago
|
func (s *meiliTweetSearchServant) queryByTag(user *model.User, q *core.QueryReq, offset, limit int) (*core.QueryResp, error) {
|
||
|
request := &meilisearch.SearchRequest{
|
||
2 years ago
|
Offset: int64(offset),
|
||
|
Limit: int64(limit),
|
||
|
Sort: []string{"is_top:desc", "latest_replied_on:desc"},
|
||
2 years ago
|
}
|
||
|
|
||
|
filter := s.filterList(user)
|
||
|
tagFilter := []string{"tags." + q.Query + "=1"}
|
||
|
if len(filter) > 0 {
|
||
|
request.Filter = [][]string{tagFilter, {filter}}
|
||
|
} else {
|
||
|
request.Filter = tagFilter
|
||
|
}
|
||
|
|
||
|
// logrus.Debugf("meiliTweetSearchServant.queryByTag query:%s request%+v", q.Query, request)
|
||
|
resp, err := s.index.Search("#"+q.Query, request)
|
||
2 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return s.postsFrom(resp)
|
||
|
}
|
||
|
|
||
2 years ago
|
func (s *meiliTweetSearchServant) queryAny(user *model.User, offset, limit int) (*core.QueryResp, error) {
|
||
|
request := &meilisearch.SearchRequest{
|
||
2 years ago
|
Offset: int64(offset),
|
||
|
Limit: int64(limit),
|
||
|
Sort: []string{"is_top:desc", "latest_replied_on:desc"},
|
||
2 years ago
|
}
|
||
|
|
||
|
filter := s.filterList(user)
|
||
|
if len(filter) > 0 {
|
||
|
request.Filter = filter
|
||
|
}
|
||
|
|
||
|
resp, err := s.index.Search("", request)
|
||
2 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return s.postsFrom(resp)
|
||
|
}
|
||
|
|
||
2 years ago
|
func (s *meiliTweetSearchServant) filterList(user *model.User) string {
|
||
|
if user == nil {
|
||
|
return s.publicFilter
|
||
|
}
|
||
|
|
||
|
if user.IsAdmin {
|
||
|
return ""
|
||
|
}
|
||
|
|
||
|
return fmt.Sprintf("%s OR %s OR (%s%d)", s.publicFilter, s.friendFilter, s.privateFilter, user.ID)
|
||
|
}
|
||
|
|
||
2 years ago
|
func (s *meiliTweetSearchServant) postsFrom(resp *meilisearch.SearchResponse) (*core.QueryResp, error) {
|
||
|
posts := make([]*model.PostFormated, 0, len(resp.Hits))
|
||
|
for _, hit := range resp.Hits {
|
||
|
item := &model.PostFormated{}
|
||
|
raw, err := json.Marshal(hit)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if err = json.Unmarshal(raw, item); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
posts = append(posts, item)
|
||
|
}
|
||
|
|
||
|
return &core.QueryResp{
|
||
|
Items: posts,
|
||
|
Total: resp.NbHits,
|
||
|
}, nil
|
||
|
}
|