2022-03-10 04:01:06 +00:00
|
|
|
package elasticsearch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Selly-Modules/natsio"
|
|
|
|
)
|
|
|
|
|
2022-03-10 04:36:06 +00:00
|
|
|
// Client ...
|
2022-03-10 04:01:06 +00:00
|
|
|
type Client struct {
|
|
|
|
Config Config
|
|
|
|
natsServer natsio.Server
|
|
|
|
natsJetStream natsio.JetStream
|
|
|
|
}
|
|
|
|
|
2022-03-10 04:36:06 +00:00
|
|
|
// NewClient
|
|
|
|
// Init client elasticsearch
|
2022-03-10 04:01:06 +00:00
|
|
|
func NewClient(config Config) (*Client, error) {
|
|
|
|
if config.ApiKey == "" {
|
|
|
|
return nil, errors.New("api key is required")
|
|
|
|
}
|
|
|
|
if config.Nats.URL == "" {
|
|
|
|
return nil, errors.New("nats url is required")
|
|
|
|
}
|
|
|
|
if err := natsio.Connect(config.Nats); err != nil {
|
|
|
|
return nil, fmt.Errorf("nats connect failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &Client{
|
|
|
|
Config: config,
|
|
|
|
natsServer: natsio.GetServer(),
|
|
|
|
natsJetStream: natsio.GetJetStream(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2022-03-10 04:36:06 +00:00
|
|
|
// SyncData
|
|
|
|
// Sync data to services ES
|
2022-03-10 04:01:06 +00:00
|
|
|
func (c *Client) SyncData(data SyncData) (bool, error) {
|
2022-03-18 05:36:36 +00:00
|
|
|
var (
|
|
|
|
req = RequestBody{
|
|
|
|
ApiKey: c.Config.ApiKey,
|
|
|
|
Body: toBytes(data),
|
|
|
|
}
|
|
|
|
res *Response
|
|
|
|
)
|
|
|
|
msg, err := c.natsServer.Request(SubjectSyncData, toBytes(req))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(msg.Data, &res); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if res.Message != "" {
|
|
|
|
return false, errors.New(res.Message)
|
|
|
|
}
|
|
|
|
return res.Success, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyncDataWithJetStream
|
|
|
|
// Sync data to services ES with JetStream
|
|
|
|
func (c *Client) SyncDataWithJetStream(data SyncData) (bool, error) {
|
2022-03-10 04:01:06 +00:00
|
|
|
var (
|
2022-03-10 13:41:44 +00:00
|
|
|
req = RequestBody{
|
|
|
|
ApiKey: c.Config.ApiKey,
|
|
|
|
Body: toBytes(data),
|
|
|
|
}
|
2022-03-10 04:01:06 +00:00
|
|
|
)
|
2022-03-15 08:00:34 +00:00
|
|
|
err := c.natsJetStream.Publish(JetStreamSearchService, SubjectSyncData, toBytes(req))
|
2022-03-10 04:01:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2022-03-18 05:36:36 +00:00
|
|
|
|
2022-03-15 08:00:34 +00:00
|
|
|
return true, nil
|
2022-03-10 04:01:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 04:36:06 +00:00
|
|
|
// Search
|
|
|
|
// Request search to service es
|
2022-03-10 13:41:44 +00:00
|
|
|
func (c *Client) Search(query ESQuery) (*Response, error) {
|
2022-03-10 04:01:06 +00:00
|
|
|
var (
|
2022-03-10 13:41:44 +00:00
|
|
|
req = RequestBody{
|
|
|
|
ApiKey: c.Config.ApiKey,
|
|
|
|
Body: toBytes(query),
|
|
|
|
}
|
|
|
|
res *Response
|
2022-03-10 04:01:06 +00:00
|
|
|
)
|
2022-03-10 13:41:44 +00:00
|
|
|
msg, err := c.natsServer.Request(SubjectSearch, toBytes(req))
|
2022-03-10 04:01:06 +00:00
|
|
|
if err != nil {
|
2022-03-10 13:41:44 +00:00
|
|
|
return nil, err
|
2022-03-10 04:01:06 +00:00
|
|
|
}
|
|
|
|
if err = json.Unmarshal(msg.Data, &res); err != nil {
|
2022-03-10 13:41:44 +00:00
|
|
|
return nil, err
|
2022-03-10 04:01:06 +00:00
|
|
|
}
|
|
|
|
if res.Message != "" {
|
2022-03-10 13:41:44 +00:00
|
|
|
return nil, errors.New(res.Message)
|
2022-03-10 04:01:06 +00:00
|
|
|
}
|
2022-03-10 13:41:44 +00:00
|
|
|
return res, nil
|
2022-03-10 04:01:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 04:36:06 +00:00
|
|
|
// UpdateDocument
|
|
|
|
// Insert or update document to ES
|
2022-03-10 13:41:44 +00:00
|
|
|
func (c *Client) UpdateDocument(query UpdateDataPayload) (bool, error) {
|
2022-03-10 04:01:06 +00:00
|
|
|
var (
|
2022-03-10 13:41:44 +00:00
|
|
|
req = RequestBody{
|
|
|
|
ApiKey: c.Config.ApiKey,
|
|
|
|
Body: toBytes(query),
|
|
|
|
}
|
2022-03-10 04:01:06 +00:00
|
|
|
)
|
2022-03-15 08:00:34 +00:00
|
|
|
err := c.natsJetStream.Publish(JetStreamSearchService, SubjectUpdateDocument, toBytes(req))
|
2022-03-10 04:01:06 +00:00
|
|
|
if err != nil {
|
2022-03-10 13:41:44 +00:00
|
|
|
return false, err
|
2022-03-10 04:01:06 +00:00
|
|
|
}
|
2022-03-15 08:00:34 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteDocument
|
|
|
|
// Delete document to ES
|
|
|
|
func (c *Client) DeleteDocument(payload DeleteDataPayload) (bool, error) {
|
|
|
|
var (
|
|
|
|
req = RequestBody{
|
|
|
|
ApiKey: c.Config.ApiKey,
|
|
|
|
Body: toBytes(payload),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
err := c.natsJetStream.Publish(JetStreamSearchService, SubjectUpdateDocument, toBytes(req))
|
|
|
|
if err != nil {
|
2022-03-10 13:41:44 +00:00
|
|
|
return false, err
|
2022-03-10 04:01:06 +00:00
|
|
|
}
|
2022-03-15 08:00:34 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateIndex
|
|
|
|
// Create index to ES
|
|
|
|
func (c *Client) CreateIndex(name string) (bool, error) {
|
|
|
|
var (
|
|
|
|
req = RequestBody{
|
|
|
|
ApiKey: c.Config.ApiKey,
|
|
|
|
Body: toBytes(name),
|
|
|
|
}
|
2022-03-15 08:03:26 +00:00
|
|
|
res *Response
|
2022-03-15 08:00:34 +00:00
|
|
|
)
|
2022-03-15 08:03:26 +00:00
|
|
|
msg, err := c.natsServer.Request(SubjectCreateIndex, toBytes(req))
|
2022-03-15 08:00:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2022-03-10 04:01:06 +00:00
|
|
|
}
|
2022-03-15 08:03:26 +00:00
|
|
|
if err = json.Unmarshal(msg.Data, &res); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if res.Message != "" {
|
|
|
|
return false, errors.New(res.Message)
|
|
|
|
}
|
|
|
|
return res.Success, nil
|
2022-03-10 04:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func toBytes(data interface{}) []byte {
|
|
|
|
b, _ := json.Marshal(data)
|
|
|
|
return b
|
|
|
|
}
|