[Update] Add comment

This commit is contained in:
trunglt251292 2022-03-10 11:36:06 +07:00
parent 91598d848f
commit 0f04829163
4 changed files with 26 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package elasticsearch
import "github.com/Selly-Modules/natsio" import "github.com/Selly-Modules/natsio"
// Config int client elasticsearch
type Config struct { type Config struct {
ApiKey string ApiKey string
Nats natsio.Config Nats natsio.Config

View File

@ -1,5 +1,5 @@
package elasticsearch package elasticsearch
const SubjectSyncData = "sync_data" const SubjectSyncData = "elasticsearch_sync_data"
const SubjectSearch = "search" const SubjectSearch = "elasticsearch_search"
const SubjectUpdateDocument = "update_document" const SubjectUpdateDocument = "elasticsearch_update_document"

View File

@ -8,12 +8,15 @@ import (
"github.com/Selly-Modules/natsio" "github.com/Selly-Modules/natsio"
) )
// Client ...
type Client struct { type Client struct {
Config Config Config Config
natsServer natsio.Server natsServer natsio.Server
natsJetStream natsio.JetStream natsJetStream natsio.JetStream
} }
// NewClient
// Init client elasticsearch
func NewClient(config Config) (*Client, error) { func NewClient(config Config) (*Client, error) {
if config.ApiKey == "" { if config.ApiKey == "" {
return nil, errors.New("api key is required") return nil, errors.New("api key is required")
@ -34,6 +37,8 @@ func NewClient(config Config) (*Client, error) {
return c, nil return c, nil
} }
// SyncData
// Sync data to services ES
func (c *Client) SyncData(data SyncData) (bool, error) { func (c *Client) SyncData(data SyncData) (bool, error) {
var ( var (
res Response res Response
@ -51,6 +56,8 @@ func (c *Client) SyncData(data SyncData) (bool, error) {
return res.Success, nil return res.Success, nil
} }
// Search
// Request search to service es
func (c *Client) Search(query ESQuery) ([]string, error) { func (c *Client) Search(query ESQuery) ([]string, error) {
var ( var (
res Response res Response
@ -68,6 +75,8 @@ func (c *Client) Search(query ESQuery) ([]string, error) {
return res.Data, nil return res.Data, nil
} }
// UpdateDocument
// Insert or update document to ES
func (c *Client) UpdateDocument(query UpdateDataPayload) ([]string, error) { func (c *Client) UpdateDocument(query UpdateDataPayload) ([]string, error) {
var ( var (
res Response res Response

View File

@ -2,6 +2,8 @@ package elasticsearch
import "time" import "time"
// Response
// response to service es
type Response struct { type Response struct {
Success bool `json:"success"` Success bool `json:"success"`
Data []string `json:"data,omitempty"` Data []string `json:"data,omitempty"`
@ -11,16 +13,22 @@ type Response struct {
Message string `json:"message"` Message string `json:"message"`
} }
// SyncData
// Payload for sync data to service es
type SyncData struct { type SyncData struct {
Index string Index string
Data []byte Data []byte
} }
// UpdateDataPayload
// Payload for insert or update document
type UpdateDataPayload struct { type UpdateDataPayload struct {
Index string Index string
Body []byte Body []byte
} }
// ESQuery
// Query support to search document
type ESQuery struct { type ESQuery struct {
Index string // Index Index string // Index
Page int64 Page int64
@ -73,7 +81,11 @@ type ESQuery struct {
Segments []string Segments []string
} }
// ESSort
// ES sort with field
// ... filed is sort
// ... ascending [true is asc] [false is desc]
type ESSort struct { type ESSort struct {
Filed string // Filed sort Field string // Filed sort
Ascending bool Ascending bool
} }