feat:优化信息

This commit is contained in:
henry
2021-12-23 10:33:31 +08:00
parent f4c02860b4
commit 741138b3ea
5 changed files with 92 additions and 45 deletions

View File

@ -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)
}

View File

@ -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
}