2021-12-09 16:17:23 +08:00
|
|
|
package es
|
|
|
|
|
|
|
|
import (
|
2021-12-20 17:07:39 +08:00
|
|
|
"context"
|
2021-12-22 14:11:14 +08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-12-20 17:07:39 +08:00
|
|
|
"github.com/olivere/elastic/v7"
|
2021-12-23 10:33:31 +08:00
|
|
|
"reflect"
|
2021-12-09 16:17:23 +08:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
dataIndex = "test"
|
2021-12-20 17:07:39 +08:00
|
|
|
//client *elastic.Client
|
|
|
|
host = "http://192.168.0.188:9200"
|
2021-12-09 16:17:23 +08:00
|
|
|
)
|
|
|
|
|
2021-12-22 14:11:14 +08:00
|
|
|
type Student struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Age int64 `json:"age"`
|
|
|
|
AverageScore float64 `json:"average_score"`
|
|
|
|
}
|
|
|
|
|
2021-12-09 16:17:23 +08:00
|
|
|
func TestNewInstance(t *testing.T) {
|
2021-12-20 17:07:39 +08:00
|
|
|
client, err := elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(host))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-12-22 14:11:14 +08:00
|
|
|
|
2021-12-20 17:07:39 +08:00
|
|
|
if _, _, err = client.Ping(host).Do(context.Background()); err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
return
|
|
|
|
}
|
2021-12-22 14:11:14 +08:00
|
|
|
|
|
|
|
//newStudent := Student{
|
|
|
|
// Name: "熊,桥,顺",
|
|
|
|
// Age: 10,
|
|
|
|
// AverageScore: 99.9,
|
|
|
|
//}
|
|
|
|
//dataJSON, _ := json.Marshal(newStudent)
|
|
|
|
//
|
|
|
|
//_, err = client.Index().
|
|
|
|
// Index("students").
|
|
|
|
// BodyJson(string(dataJSON)).
|
|
|
|
// Do(context.Background())
|
|
|
|
//
|
|
|
|
//if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
//}
|
|
|
|
var students []Student
|
|
|
|
|
|
|
|
searchSource := elastic.NewSearchSource()
|
|
|
|
searchSource.Query(elastic.NewMatchQuery("name", "顺"))
|
|
|
|
|
|
|
|
queryStr, _ := searchSource.Source()
|
|
|
|
queryJs, _ := json.Marshal(queryStr)
|
|
|
|
|
|
|
|
fmt.Println("[esclient]Final ESQuery=\n", string(queryJs))
|
|
|
|
|
|
|
|
searchService := client.Search().Index("students").SearchSource(searchSource)
|
|
|
|
|
|
|
|
searchResult, err1 := searchService.Do(context.Background())
|
|
|
|
|
|
|
|
if err1 != nil {
|
|
|
|
fmt.Println("[ProductsES][GetPIds]Error=", err1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, hit := range searchResult.Hits.Hits {
|
|
|
|
var student Student
|
|
|
|
err := json.Unmarshal(hit.Source, &student)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("[Getting Students][Unmarshal] Err=", err)
|
|
|
|
}
|
|
|
|
students = append(students, student)
|
|
|
|
}
|
|
|
|
t.Log(students)
|
2021-12-20 17:07:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewInstance1(t *testing.T) {
|
2021-12-09 16:17:23 +08:00
|
|
|
NewInstance(
|
|
|
|
WithEsAddress([]string{"http://192.168.0.188:9200"}),
|
|
|
|
).Init().Local()
|
|
|
|
var b strings.Builder
|
|
|
|
b.WriteString(`{"title" : "`)
|
|
|
|
b.WriteString("我是好人")
|
|
|
|
b.WriteString(`"}`)
|
2021-12-22 14:11:14 +08:00
|
|
|
//resp, err := client.Create(dataIndex, "2", strings.NewReader(b.String()))
|
|
|
|
//t.Log(err)
|
|
|
|
//t.Log(resp)
|
2021-12-09 16:17:23 +08:00
|
|
|
}
|
2021-12-20 17:07:39 +08:00
|
|
|
|
2021-12-23 10:33:31 +08:00
|
|
|
type Manage struct {
|
|
|
|
ID uint64 `json:"id"`
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:07:39 +08:00
|
|
|
func TestNewInstance2(t *testing.T) {
|
2021-12-23 10:33:31 +08:00
|
|
|
//NewInstance(
|
|
|
|
// WithEsAddress([]string{"http://192.168.0.188:9200"}),
|
|
|
|
//).Init().Local()
|
2021-12-20 17:07:39 +08:00
|
|
|
|
2021-12-22 14:11:14 +08:00
|
|
|
//client.Search(func(request *esapi.SearchRequest) {
|
|
|
|
//
|
|
|
|
//})
|
2021-12-23 10:33:31 +08:00
|
|
|
|
|
|
|
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)
|
2021-12-20 17:07:39 +08:00
|
|
|
}
|