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.
79 lines
2.0 KiB
79 lines
2.0 KiB
2 years ago
|
package search
|
||
2 years ago
|
|
||
|
import (
|
||
|
"github.com/rocboss/paopao-ce/internal/core"
|
||
2 years ago
|
"github.com/rocboss/paopao-ce/internal/model"
|
||
2 years ago
|
"github.com/sirupsen/logrus"
|
||
|
)
|
||
|
|
||
2 years ago
|
var (
|
||
|
_ core.TweetSearchService = (*bridgeTweetSearchServant)(nil)
|
||
|
)
|
||
|
|
||
|
type documents struct {
|
||
|
primaryKey []string
|
||
|
docItems core.DocItems
|
||
|
identifiers []string
|
||
|
}
|
||
|
|
||
|
type bridgeTweetSearchServant struct {
|
||
|
ts core.TweetSearchService
|
||
|
updateDocsCh chan *documents
|
||
|
}
|
||
|
|
||
2 years ago
|
func (s *bridgeTweetSearchServant) IndexName() string {
|
||
|
return s.ts.IndexName()
|
||
|
}
|
||
|
|
||
|
func (s *bridgeTweetSearchServant) AddDocuments(data core.DocItems, primaryKey ...string) (bool, error) {
|
||
|
s.updateDocs(&documents{
|
||
|
primaryKey: primaryKey,
|
||
|
docItems: data,
|
||
|
})
|
||
|
return true, nil
|
||
|
}
|
||
|
|
||
|
func (s *bridgeTweetSearchServant) DeleteDocuments(identifiers []string) error {
|
||
|
s.updateDocs(&documents{
|
||
|
identifiers: identifiers,
|
||
|
})
|
||
|
return nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func (s *bridgeTweetSearchServant) Search(user *model.User, q *core.QueryReq, offset, limit int) (*core.QueryResp, error) {
|
||
|
return s.ts.Search(user, q, offset, limit)
|
||
2 years ago
|
}
|
||
|
|
||
|
func (s *bridgeTweetSearchServant) updateDocs(doc *documents) {
|
||
|
select {
|
||
|
case s.updateDocsCh <- doc:
|
||
|
logrus.Debugln("addDocuments send documents by chan")
|
||
|
default:
|
||
2 years ago
|
go func(item *documents) {
|
||
|
if len(item.docItems) > 0 {
|
||
|
if _, err := s.ts.AddDocuments(item.docItems, item.primaryKey...); err != nil {
|
||
|
logrus.Errorf("addDocuments in gorotine occurs error: %v", err)
|
||
|
}
|
||
|
} else if len(item.identifiers) > 0 {
|
||
|
if err := s.ts.DeleteDocuments(item.identifiers); err != nil {
|
||
|
logrus.Errorf("deleteDocuments in gorotine occurs error: %s", err)
|
||
|
}
|
||
|
}
|
||
|
}(doc)
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
|
func (s *bridgeTweetSearchServant) startUpdateDocs() {
|
||
|
for doc := range s.updateDocsCh {
|
||
|
if len(doc.docItems) > 0 {
|
||
|
if _, err := s.ts.AddDocuments(doc.docItems, doc.primaryKey...); err != nil {
|
||
|
logrus.Errorf("addDocuments occurs error: %v", err)
|
||
|
}
|
||
2 years ago
|
} else if len(doc.identifiers) > 0 {
|
||
2 years ago
|
if err := s.ts.DeleteDocuments(doc.identifiers); err != nil {
|
||
|
logrus.Errorf("deleteDocuments occurs error: %s", err)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|