54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package es
|
|
|
|
import (
|
|
"context"
|
|
"github.com/elastic/go-elasticsearch/v7/esapi"
|
|
"github.com/olivere/elastic/v7"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
var (
|
|
dataIndex = "test"
|
|
//client *elastic.Client
|
|
host = "http://192.168.0.188:9200"
|
|
)
|
|
|
|
func TestNewInstance(t *testing.T) {
|
|
client, err := elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(host))
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
if _, _, err = client.Ping(host).Do(context.Background()); err != nil {
|
|
t.Log(err)
|
|
return
|
|
}
|
|
//client.CreateIndex(dataIndex).BodyJson().Do(context.Background())
|
|
esversion, _ := client.ElasticsearchVersion(host)
|
|
t.Log(esversion)
|
|
}
|
|
|
|
func TestNewInstance1(t *testing.T) {
|
|
NewInstance(
|
|
WithEsAddress([]string{"http://192.168.0.188:9200"}),
|
|
).Init().Local()
|
|
var b strings.Builder
|
|
b.WriteString(`{"title" : "`)
|
|
b.WriteString("我是好人")
|
|
b.WriteString(`"}`)
|
|
resp, err := client.Create(dataIndex, "2", strings.NewReader(b.String()))
|
|
t.Log(err)
|
|
t.Log(resp)
|
|
}
|
|
|
|
func TestNewInstance2(t *testing.T) {
|
|
NewInstance(
|
|
WithEsAddress([]string{"http://192.168.0.188:9200"}),
|
|
).Init().Local()
|
|
|
|
client.Search(func(request *esapi.SearchRequest) {
|
|
|
|
})
|
|
}
|