feat:完善网站信息
This commit is contained in:
@ -1,20 +1,22 @@
|
||||
package es
|
||||
|
||||
import (
|
||||
"github.com/elastic/go-elasticsearch/v7"
|
||||
"context"
|
||||
"github.com/olivere/elastic/v7"
|
||||
)
|
||||
|
||||
type Instance struct {
|
||||
client *elasticsearch.Client
|
||||
client *elastic.Client
|
||||
|
||||
address []string
|
||||
username, password string
|
||||
logger elastic.Logger
|
||||
}
|
||||
|
||||
type Option func(*Instance)
|
||||
|
||||
var (
|
||||
client *elasticsearch.Client
|
||||
client *elastic.Client
|
||||
)
|
||||
|
||||
func WithEsAddress(address []string) Option {
|
||||
@ -35,24 +37,29 @@ func WithEsPassword(password string) Option {
|
||||
}
|
||||
}
|
||||
|
||||
func WithEsLog(logger elastic.Logger) Option {
|
||||
return func(instance *Instance) {
|
||||
instance.logger = logger
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Instance) Init() *Instance {
|
||||
obj := elasticsearch.Config{
|
||||
Addresses: this.address,
|
||||
Username: this.username,
|
||||
Password: this.password,
|
||||
}
|
||||
client, err := elasticsearch.NewClient(obj)
|
||||
|
||||
client, err := elastic.NewClient(
|
||||
elastic.SetSniff(false),
|
||||
elastic.SetURL(this.address...),
|
||||
//elastic.SetInfoLog(this.logger),
|
||||
)
|
||||
if err != nil {
|
||||
panic("Elasticsearch New Error " + err.Error())
|
||||
panic(err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
if _, err = client.Ping(); err != nil {
|
||||
panic("Elasticsearch Ping Error " + err.Error())
|
||||
|
||||
for _, address := range this.address {
|
||||
if _, _, err = client.Ping(address).Do(ctx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
this.client = client
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
@ -69,3 +76,7 @@ func NewInstance(option ...Option) *Instance {
|
||||
}
|
||||
return instance
|
||||
}
|
||||
|
||||
func GetInstance() *elastic.Client {
|
||||
return client
|
||||
}
|
||||
|
@ -2,7 +2,8 @@ package es
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/elastic/go-elasticsearch/v7/esapi"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/olivere/elastic/v7"
|
||||
"strings"
|
||||
"testing"
|
||||
@ -14,19 +15,67 @@ var (
|
||||
host = "http://192.168.0.188:9200"
|
||||
)
|
||||
|
||||
type Student struct {
|
||||
Name string `json:"name"`
|
||||
Age int64 `json:"age"`
|
||||
AverageScore float64 `json:"average_score"`
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
//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)
|
||||
}
|
||||
|
||||
func TestNewInstance1(t *testing.T) {
|
||||
@ -37,9 +86,9 @@ func TestNewInstance1(t *testing.T) {
|
||||
b.WriteString(`{"title" : "`)
|
||||
b.WriteString("我是好人")
|
||||
b.WriteString(`"}`)
|
||||
resp, err := client.Create(dataIndex, "2", strings.NewReader(b.String()))
|
||||
t.Log(err)
|
||||
t.Log(resp)
|
||||
//resp, err := client.Create(dataIndex, "2", strings.NewReader(b.String()))
|
||||
//t.Log(err)
|
||||
//t.Log(resp)
|
||||
}
|
||||
|
||||
func TestNewInstance2(t *testing.T) {
|
||||
@ -47,7 +96,7 @@ func TestNewInstance2(t *testing.T) {
|
||||
WithEsAddress([]string{"http://192.168.0.188:9200"}),
|
||||
).Init().Local()
|
||||
|
||||
client.Search(func(request *esapi.SearchRequest) {
|
||||
|
||||
})
|
||||
//client.Search(func(request *esapi.SearchRequest) {
|
||||
//
|
||||
//})
|
||||
}
|
||||
|
@ -2,6 +2,9 @@ package es
|
||||
|
||||
func Create() error {
|
||||
//_, err := client.Index().
|
||||
// Index("students").
|
||||
// BodyJson(string(dataJSON)).
|
||||
// Do(context.Background())
|
||||
return nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user