package service import ( "SciencesServer/serve/es" "encoding/json" "fmt" ) type ESManage struct { ID uint64 `json:"id"` // ID Identity int `json:"identity"` // 身份信息 Industry string `json:"industry"` // 行业领域 Title string `json:"title"` // 名称 Keyword string `json:"keyword"` // 关键词 Research string `json:"research"` // 研究方向 } type ESManageOption func(manage *ESManage) func (this *ESManage) Index() string { if this.Identity > 0 { return fmt.Sprintf("es_manage_index_%d", this.Identity) } return "es_manage_index" } func (this *ESManage) Create() error { _bytes, _ := json.Marshal(this) return es.Create(this.Index(), fmt.Sprintf("%d", this.ID), _bytes) } func (this *ESManage) Update() error { _map := make(map[string]interface{}, 0) if this.Title != "" { _map["title"] = this.Title } if this.Industry != "" { _map["industry"] = this.Industry } if this.Keyword != "" { _map["keyword"] = this.Keyword } if this.Research != "" { _map["research"] = this.Research } return es.Update(this.Index(), fmt.Sprintf("%d", this.ID), _map) } func (this *ESManage) Search(page, pageSize int, params map[string]interface{}) (interface{}, int64, error) { termParams := make(map[string]interface{}, 0) shouldParams := make(map[string]interface{}, 0) if this.Industry != "" { termParams["industry"] = this.Industry } if this.Title != "" { shouldParams["title"] = this.Title } if this.Keyword != "" { shouldParams["keyword"] = this.Keyword } if this.Research != "" { shouldParams["research"] = this.Research } if len(params) > 0 { for k, v := range params { shouldParams[k] = v } } return es.Search(this, this.Index(), &es.SearchParams{ TermParams: termParams, ShouldParams: shouldParams, }, page, pageSize) } func WithManageID(id uint64) ESManageOption { return func(manage *ESManage) { manage.ID = id } } func WithManageIdentity(identity int) ESManageOption { return func(manage *ESManage) { manage.Identity = identity } } func WithManageTitle(title string) ESManageOption { return func(manage *ESManage) { manage.Title = title } } func WithManageIndustry(industry string) ESManageOption { return func(manage *ESManage) { manage.Industry = industry } } func WithManageKeyword(keyword string) ESManageOption { return func(manage *ESManage) { manage.Keyword = keyword } } func WithManageResearch(research string) ESManageOption { return func(manage *ESManage) { manage.Research = research } } func NewESManage(options ...ESManageOption) *ESManage { out := new(ESManage) for _, option := range options { option(out) } return out }