feat:优化信息
This commit is contained in:
@ -13,6 +13,7 @@ func (*ES) Create(c *gin.Context) {
|
|||||||
ID uint64 `json:"id" form:"id"`
|
ID uint64 `json:"id" form:"id"`
|
||||||
Title string `json:"title" form:"title"`
|
Title string `json:"title" form:"title"`
|
||||||
Keyword string `json:"keyword" form:"keyword"`
|
Keyword string `json:"keyword" form:"keyword"`
|
||||||
|
Research string `json:"research" form:"research"`
|
||||||
}{}
|
}{}
|
||||||
if err := api.Bind(form)(c); err != nil {
|
if err := api.Bind(form)(c); err != nil {
|
||||||
api.APIFailure(err.(error))(c)
|
api.APIFailure(err.(error))(c)
|
||||||
@ -22,6 +23,7 @@ func (*ES) Create(c *gin.Context) {
|
|||||||
service.WithManageID(form.ID),
|
service.WithManageID(form.ID),
|
||||||
service.WithManageTitle(form.Title),
|
service.WithManageTitle(form.Title),
|
||||||
service.WithManageKeyword(form.Keyword),
|
service.WithManageKeyword(form.Keyword),
|
||||||
|
service.WithManageResearch(form.Research),
|
||||||
)
|
)
|
||||||
api.APIResponse(manage.Create())(c)
|
api.APIResponse(manage.Create())(c)
|
||||||
}
|
}
|
||||||
@ -35,6 +37,6 @@ func (*ES) Search(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
manage := service.NewManage()
|
manage := service.NewManage()
|
||||||
data, err := manage.Search(form.Params)
|
data, err := manage.Search(1, 10, form.Params)
|
||||||
api.APIResponse(err, data)(c)
|
api.APIResponse(err, data)(c)
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ func (c *Search) Launch(identity int, params string) (interface{}, error) {
|
|||||||
_params := map[string]interface{}{
|
_params := map[string]interface{}{
|
||||||
"title": params, "keyword": params, "research": params,
|
"title": params, "keyword": params, "research": params,
|
||||||
}
|
}
|
||||||
data, err := manage.Search(_params)
|
data, err := manage.Search(1, 1, _params)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -2,10 +2,7 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"SciencesServer/serve/es"
|
"SciencesServer/serve/es"
|
||||||
"context"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"github.com/olivere/elastic/v7"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Manage struct {
|
type Manage struct {
|
||||||
@ -24,34 +21,11 @@ func (c *Manage) Index() string {
|
|||||||
|
|
||||||
func (this *Manage) Create() error {
|
func (this *Manage) Create() error {
|
||||||
_bytes, _ := json.Marshal(this)
|
_bytes, _ := json.Marshal(this)
|
||||||
_, err := es.GetInstance().Index().Index(this.Index()).BodyJson(string(_bytes)).Do(context.Background())
|
return es.Create(this.Index(), _bytes)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Manage) Search(condition map[string]interface{}) (interface{}, error) {
|
func (this *Manage) Search(page, pageSize int, condition map[string]interface{}) (interface{}, error) {
|
||||||
search := elastic.NewSearchSource()
|
return es.Search(this, this.Index(), condition, page, pageSize)
|
||||||
|
|
||||||
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 {
|
func WithManageID(id uint64) ManageOption {
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/olivere/elastic/v7"
|
"github.com/olivere/elastic/v7"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -91,12 +92,45 @@ func TestNewInstance1(t *testing.T) {
|
|||||||
//t.Log(resp)
|
//t.Log(resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Manage struct {
|
||||||
|
ID uint64 `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewInstance2(t *testing.T) {
|
func TestNewInstance2(t *testing.T) {
|
||||||
NewInstance(
|
//NewInstance(
|
||||||
WithEsAddress([]string{"http://192.168.0.188:9200"}),
|
// WithEsAddress([]string{"http://192.168.0.188:9200"}),
|
||||||
).Init().Local()
|
//).Init().Local()
|
||||||
|
|
||||||
//client.Search(func(request *esapi.SearchRequest) {
|
//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
|
package es
|
||||||
|
|
||||||
func Create() error {
|
import (
|
||||||
//_, err := client.Index().
|
"context"
|
||||||
// Index("students").
|
"encoding/json"
|
||||||
// BodyJson(string(dataJSON)).
|
"github.com/olivere/elastic/v7"
|
||||||
// Do(context.Background())
|
"reflect"
|
||||||
return nil
|
)
|
||||||
|
|
||||||
|
// Create 创建操作
|
||||||
|
func Create(index string, body []byte) error {
|
||||||
|
_, err := client.Index().
|
||||||
|
Index(index).
|
||||||
|
BodyJson(string(body)).
|
||||||
|
Do(context.Background())
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func Search() {
|
// Search 搜索操作
|
||||||
//resp, err := client.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