@@ -0,0 +1,76 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package audiobooksearcher
|
||||
|
||||
import (
|
||||
"91porn-server/app/service/searcher"
|
||||
"91porn-server/models/v/audiobookmod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type ObjectID = primitive.ObjectID
|
||||
|
||||
type Result struct {
|
||||
dat []audiobookmod.AudioBookAppRes
|
||||
hasNext bool
|
||||
}
|
||||
|
||||
func (*Result) Types() searcher.ResultTypes {
|
||||
return searcher.LouFengSearchResult
|
||||
}
|
||||
|
||||
func (r *Result) Data() interface{} {
|
||||
return r.dat
|
||||
}
|
||||
|
||||
func (r *Result) Count() int64 {
|
||||
return int64(len(r.dat))
|
||||
}
|
||||
|
||||
func (r *Result) HasNext() bool {
|
||||
return r.hasNext
|
||||
}
|
||||
Reference in New Issue
Block a user