feat:优化信息

This commit is contained in:
henry
2021-12-23 10:33:31 +08:00
parent f4c02860b4
commit 741138b3ea
5 changed files with 92 additions and 45 deletions

View File

@ -2,10 +2,7 @@ package service
import (
"SciencesServer/serve/es"
"context"
"encoding/json"
"fmt"
"github.com/olivere/elastic/v7"
)
type Manage struct {
@ -24,34 +21,11 @@ func (c *Manage) Index() string {
func (this *Manage) Create() error {
_bytes, _ := json.Marshal(this)
_, err := es.GetInstance().Index().Index(this.Index()).BodyJson(string(_bytes)).Do(context.Background())
return err
return es.Create(this.Index(), _bytes)
}
func (this *Manage) Search(condition map[string]interface{}) (interface{}, error) {
search := elastic.NewSearchSource()
for k, v := range condition {
search.Query(elastic.NewMatchQuery(k, fmt.Sprintf("%v", v)))
}
service := es.GetInstance().Search().Index(this.Index()).SearchSource(search)
result, err := service.Do(context.Background())
if err != nil {
return nil, err
}
out := make([]*Manage, 0)
for _, hit := range result.Hits.Hits {
data := new(Manage)
if err = json.Unmarshal(hit.Source, data); err != nil {
return nil, err
}
out = append(out, data)
}
return out, nil
func (this *Manage) Search(page, pageSize int, condition map[string]interface{}) (interface{}, error) {
return es.Search(this, this.Index(), condition, page, pageSize)
}
func WithManageID(id uint64) ManageOption {