feat:完善产品详情
This commit is contained in:
@ -1,32 +1,71 @@
|
||||
package es
|
||||
|
||||
import "github.com/elastic/go-elasticsearch/v7"
|
||||
import (
|
||||
"github.com/elastic/go-elasticsearch/v7"
|
||||
)
|
||||
|
||||
type Es struct{ *EsConfig }
|
||||
type Instance struct {
|
||||
client *elasticsearch.Client
|
||||
|
||||
type EsConfig struct {
|
||||
Address []string
|
||||
address []string
|
||||
username, password string
|
||||
}
|
||||
|
||||
type EsServer func(*EsConfig) *Es
|
||||
type Option func(*Instance)
|
||||
|
||||
var esClient = new(elasticsearch.Client)
|
||||
var (
|
||||
client *elasticsearch.Client
|
||||
)
|
||||
|
||||
func (this *Es) Run() {
|
||||
func WithEsAddress(address []string) Option {
|
||||
return func(instance *Instance) {
|
||||
instance.address = address
|
||||
}
|
||||
}
|
||||
|
||||
func WithEsUsername(username string) Option {
|
||||
return func(instance *Instance) {
|
||||
instance.username = username
|
||||
}
|
||||
}
|
||||
|
||||
func WithEsPassword(password string) Option {
|
||||
return func(instance *Instance) {
|
||||
instance.password = password
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Instance) Init() *Instance {
|
||||
obj := elasticsearch.Config{
|
||||
Addresses: this.Address,
|
||||
Username: "",
|
||||
Password: "",
|
||||
Addresses: this.address,
|
||||
Username: this.username,
|
||||
Password: this.password,
|
||||
}
|
||||
var err error
|
||||
client, err := elasticsearch.NewClient(obj)
|
||||
|
||||
if esClient, err = elasticsearch.NewClient(obj); err != nil {
|
||||
panic("Elasticsearch Error " + err.Error())
|
||||
if err != nil {
|
||||
panic("Elasticsearch New Error " + err.Error())
|
||||
}
|
||||
|
||||
if _, err = client.Ping(); err != nil {
|
||||
panic("Elasticsearch Ping Error " + err.Error())
|
||||
|
||||
}
|
||||
this.client = client
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
func NewEs() EsServer {
|
||||
return func(config *EsConfig) *Es {
|
||||
return &Es{config}
|
||||
}
|
||||
func (this *Instance) Local() *Instance {
|
||||
client = this.client
|
||||
return this
|
||||
}
|
||||
|
||||
func NewInstance(option ...Option) *Instance {
|
||||
instance := new(Instance)
|
||||
|
||||
for _, v := range option {
|
||||
v(instance)
|
||||
}
|
||||
return instance
|
||||
}
|
||||
|
23
serve/es/es_test.go
Normal file
23
serve/es/es_test.go
Normal file
@ -0,0 +1,23 @@
|
||||
package es
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
dataIndex = "test"
|
||||
)
|
||||
|
||||
func TestNewInstance(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)
|
||||
}
|
@ -1,5 +1,10 @@
|
||||
package es
|
||||
|
||||
func Set() {
|
||||
esClient.Search()
|
||||
func Create() error {
|
||||
//_, err := client.Index().
|
||||
return nil
|
||||
}
|
||||
|
||||
func Search() {
|
||||
//resp, err := client.Search()
|
||||
}
|
||||
|
Reference in New Issue
Block a user