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 {
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/olivere/elastic/v7"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@ -91,12 +92,45 @@ func TestNewInstance1(t *testing.T) {
|
||||
//t.Log(resp)
|
||||
}
|
||||
|
||||
type Manage struct {
|
||||
ID uint64 `json:"id"`
|
||||
}
|
||||
|
||||
func TestNewInstance2(t *testing.T) {
|
||||
NewInstance(
|
||||
WithEsAddress([]string{"http://192.168.0.188:9200"}),
|
||||
).Init().Local()
|
||||
//NewInstance(
|
||||
// WithEsAddress([]string{"http://192.168.0.188:9200"}),
|
||||
//).Init().Local()
|
||||
|
||||
//client.Search(func(request *esapi.SearchRequest) {
|
||||
//
|
||||
//})
|
||||
|
||||
function := func(src interface{}) {
|
||||
obj := reflect.ValueOf(src).Interface()
|
||||
t.Log(obj)
|
||||
|
||||
var a interface{} = &Manage{ID: 123123}
|
||||
|
||||
out := make([]interface{}, 0)
|
||||
|
||||
out = append(out, a)
|
||||
|
||||
}
|
||||
|
||||
manage := new(Manage)
|
||||
|
||||
function(manage)
|
||||
|
||||
var a interface{} = &Manage{ID: 123123}
|
||||
|
||||
out := make([]interface{}, 0)
|
||||
|
||||
out = append(out, a)
|
||||
|
||||
t.Log(reflect.TypeOf(manage))
|
||||
|
||||
for _, v := range out {
|
||||
t.Log(v.(*Manage))
|
||||
}
|
||||
t.Log(a)
|
||||
}
|
||||
|
@ -1,13 +1,50 @@
|
||||
package es
|
||||
|
||||
func Create() error {
|
||||
//_, err := client.Index().
|
||||
// Index("students").
|
||||
// BodyJson(string(dataJSON)).
|
||||
// Do(context.Background())
|
||||
return nil
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/olivere/elastic/v7"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Create 创建操作
|
||||
func Create(index string, body []byte) error {
|
||||
_, err := client.Index().
|
||||
Index(index).
|
||||
BodyJson(string(body)).
|
||||
Do(context.Background())
|
||||
return err
|
||||
}
|
||||
|
||||
func Search() {
|
||||
//resp, err := client.Search()
|
||||
// Search 搜索操作
|
||||
func Search(src interface{}, index string, params map[string]interface{}, page, pageSize int) ([]interface{}, error) {
|
||||
query := elastic.NewBoolQuery()
|
||||
|
||||
for k, v := range params {
|
||||
query.Should(elastic.NewMatchQuery(k, v))
|
||||
}
|
||||
service := client.Search().Index(index).Pretty(true).Query(query)
|
||||
|
||||
if page > 0 && pageSize > 0 {
|
||||
// 游标分页
|
||||
service.From((page - 1) * pageSize)
|
||||
service.Size(pageSize)
|
||||
}
|
||||
result, err := service.Do(context.Background())
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]interface{}, 0)
|
||||
|
||||
for _, hit := range result.Hits.Hits {
|
||||
data := new(interface{})
|
||||
reflect.DeepEqual(data, src)
|
||||
|
||||
if err = json.Unmarshal(hit.Source, data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, data)
|
||||
}
|
||||
return out, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user