feat:优化信息
This commit is contained in:
@ -10,9 +10,10 @@ type ES struct{}
|
||||
|
||||
func (*ES) Create(c *gin.Context) {
|
||||
form := &struct {
|
||||
ID uint64 `json:"id" form:"id"`
|
||||
Title string `json:"title" form:"title"`
|
||||
Keyword string `json:"keyword" form:"keyword"`
|
||||
ID uint64 `json:"id" form:"id"`
|
||||
Title string `json:"title" form:"title"`
|
||||
Keyword string `json:"keyword" form:"keyword"`
|
||||
Research string `json:"research" form:"research"`
|
||||
}{}
|
||||
if err := api.Bind(form)(c); err != nil {
|
||||
api.APIFailure(err.(error))(c)
|
||||
@ -22,6 +23,7 @@ func (*ES) Create(c *gin.Context) {
|
||||
service.WithManageID(form.ID),
|
||||
service.WithManageTitle(form.Title),
|
||||
service.WithManageKeyword(form.Keyword),
|
||||
service.WithManageResearch(form.Research),
|
||||
)
|
||||
api.APIResponse(manage.Create())(c)
|
||||
}
|
||||
@ -35,6 +37,6 @@ func (*ES) Search(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
manage := service.NewManage()
|
||||
data, err := manage.Search(form.Params)
|
||||
data, err := manage.Search(1, 10, form.Params)
|
||||
api.APIResponse(err, data)(c)
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ func (c *Search) Launch(identity int, params string) (interface{}, error) {
|
||||
_params := map[string]interface{}{
|
||||
"title": params, "keyword": params, "research": params,
|
||||
}
|
||||
data, err := manage.Search(_params)
|
||||
data, err := manage.Search(1, 1, _params)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -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 {
|
||||
|
Reference in New Issue
Block a user