77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
package audiobooksearcher
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"91porn-server/app/appg"
|
|
"91porn-server/app/service/searcher"
|
|
"91porn-server/models/v/audiobookmod"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
// FictionSearcher FictionSearcher
|
|
type FictionSearcher struct {
|
|
uid *uint64
|
|
}
|
|
|
|
// NewFictionSearcher NewFictionSearcher
|
|
func NewFictionSearcher(uid *uint64) searcher.Searcher {
|
|
return &FictionSearcher{
|
|
uid: uid,
|
|
}
|
|
}
|
|
|
|
// Search Search
|
|
func (t *FictionSearcher) Search(keywrod searcher.KeyWorder, opt searcher.Opter) (result searcher.Resulter, err error) {
|
|
kw, ok := keywrod.Get().(string)
|
|
if !ok {
|
|
return nil, errors.New("keyword type error")
|
|
}
|
|
if kw == "" { //关键字为空,热搜
|
|
audiobook, hasNext, err := audiobookmod.GetNew(opt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Result{dat: audiobook, hasNext: hasNext}, nil
|
|
}
|
|
if appg.Conf.Elastic.IsActive {
|
|
audiobook, _ := esSearch(kw, opt)
|
|
hasNext := false
|
|
if len(audiobook) > int(opt.Limit()) {
|
|
hasNext = true
|
|
audiobook = audiobook[:opt.Limit()]
|
|
}
|
|
ab, err := fillAudioBook(*t.uid, audiobook)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Result{dat: ab, hasNext: hasNext}, nil
|
|
}
|
|
audiobook, hasNext, err := audiobookmod.GetNew(opt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Result{dat: audiobook, hasNext: hasNext}, nil
|
|
}
|
|
|
|
func fillAudioBook(uid uint64, data []audiobookmod.AudioBookBase) (res []audiobookmod.AudioBookAppRes, err error) {
|
|
ids := make([]primitive.ObjectID, len(data))
|
|
for i := range data {
|
|
ids[i] = data[i].ID
|
|
}
|
|
return audiobookmod.GetByIds(ids)
|
|
}
|
|
|
|
func esSearch(keyword string, opt searcher.Opter) (data []audiobookmod.AudioBookBase, err error) {
|
|
audiobook, err := audiobookmod.Search(keyword, opt.Skip(), opt.Limit()+1)
|
|
if err != nil {
|
|
return
|
|
}
|
|
data = make([]audiobookmod.AudioBookBase, len(audiobook))
|
|
for i := range audiobook {
|
|
data[i] = audiobook[i].Source
|
|
}
|
|
return
|
|
}
|