107 lines
2.3 KiB
Go
107 lines
2.3 KiB
Go
package service
|
||
|
||
import (
|
||
"SciencesServer/serve/es"
|
||
"SciencesServer/serve/logger"
|
||
"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 {
|
||
if this.Industry != "" {
|
||
this.Title = this.Industry + " - " + this.Title
|
||
}
|
||
_bytes, _ := json.Marshal(this)
|
||
return es.Create(this.Index(), _bytes)
|
||
}
|
||
|
||
func (this *ESManage) Search(page, pageSize int) (interface{}, 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
|
||
}
|
||
out, err := es.Search(this, this.Index(), &es.SearchParams{
|
||
TermParams: termParams,
|
||
ShouldParams: shouldParams,
|
||
}, page, pageSize)
|
||
|
||
if err != nil {
|
||
logger.ErrorF("查询ES信息错误:【%s】", err)
|
||
}
|
||
return out, nil
|
||
}
|
||
|
||
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
|
||
}
|