feat:完善网站信息
This commit is contained in:
88
app/service/es.go
Normal file
88
app/service/es.go
Normal file
@ -0,0 +1,88 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"SciencesServer/serve/es"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/olivere/elastic/v7"
|
||||
)
|
||||
|
||||
type Manage struct {
|
||||
ID uint64 `json:"id"` // ID
|
||||
Identity int `json:"identity"` // 身份信息
|
||||
Title string `json:"title"` // 名称
|
||||
Keyword string `json:"keyword"` // 关键词
|
||||
Research string `json:"research"` // 研究方向
|
||||
}
|
||||
|
||||
type ManageOption func(manage *Manage)
|
||||
|
||||
func (c *Manage) Index() string {
|
||||
return "es_manage_index"
|
||||
}
|
||||
|
||||
func (this *Manage) Create() error {
|
||||
_bytes, _ := json.Marshal(this)
|
||||
_, err := es.GetInstance().Index().Index(this.Index()).BodyJson(string(_bytes)).Do(context.Background())
|
||||
return err
|
||||
}
|
||||
|
||||
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 WithManageID(id uint64) ManageOption {
|
||||
return func(manage *Manage) {
|
||||
manage.ID = id
|
||||
}
|
||||
}
|
||||
|
||||
func WithManageTitle(title string) ManageOption {
|
||||
return func(manage *Manage) {
|
||||
manage.Title = title
|
||||
}
|
||||
}
|
||||
|
||||
func WithManageKeyword(keyword string) ManageOption {
|
||||
return func(manage *Manage) {
|
||||
manage.Keyword = keyword
|
||||
}
|
||||
}
|
||||
|
||||
func WithManageResearch(research string) ManageOption {
|
||||
return func(manage *Manage) {
|
||||
manage.Research = research
|
||||
}
|
||||
}
|
||||
|
||||
func NewManage(options ...ManageOption) *Manage {
|
||||
out := new(Manage)
|
||||
|
||||
for _, option := range options {
|
||||
option(out)
|
||||
}
|
||||
return out
|
||||
}
|
Reference in New Issue
Block a user