From 8e00c0a666da4f4ba2fbe59e533f4dd358579f77 Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Thu, 10 Mar 2022 11:01:06 +0700 Subject: [PATCH 1/4] [Update] Define module elesticsearch --- .idea/.gitignore | 8 ++++ .idea/elasticsearch.iml | 9 ++++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ .idea/watcherTasks.xml | 9 ++++ config.go | 8 ++++ constants.go | 5 +++ elasticsearch.go | 91 +++++++++++++++++++++++++++++++++++++++++ go.mod | 14 +++++++ go.sum | 78 +++++++++++++++++++++++++++++++++++ struct.go | 79 +++++++++++++++++++++++++++++++++++ 11 files changed, 315 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/elasticsearch.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/watcherTasks.xml create mode 100644 config.go create mode 100644 constants.go create mode 100644 elasticsearch.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 struct.go diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/elasticsearch.iml b/.idea/elasticsearch.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/elasticsearch.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c38a18a --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/watcherTasks.xml b/.idea/watcherTasks.xml new file mode 100644 index 0000000..27aa2e8 --- /dev/null +++ b/.idea/watcherTasks.xml @@ -0,0 +1,9 @@ + + + + + + + \ No newline at end of file diff --git a/config.go b/config.go new file mode 100644 index 0000000..d0abdef --- /dev/null +++ b/config.go @@ -0,0 +1,8 @@ +package elasticsearch + +import "github.com/Selly-Modules/natsio" + +type Config struct { + ApiKey string + Nats natsio.Config +} diff --git a/constants.go b/constants.go new file mode 100644 index 0000000..9dc6c8f --- /dev/null +++ b/constants.go @@ -0,0 +1,5 @@ +package elasticsearch + +const SubjectSyncData = "sync_data" +const SubjectSearch = "search" +const SubjectUpdateDocument = "update_document" diff --git a/elasticsearch.go b/elasticsearch.go new file mode 100644 index 0000000..f0dbcee --- /dev/null +++ b/elasticsearch.go @@ -0,0 +1,91 @@ +package elasticsearch + +import ( + "encoding/json" + "errors" + "fmt" + + "github.com/Selly-Modules/natsio" +) + +type Client struct { + Config Config + natsServer natsio.Server + natsJetStream natsio.JetStream +} + +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 +} + +func (c *Client) SyncData(data SyncData) (bool, error) { + var ( + res Response + ) + msg, err := c.natsServer.Request(SubjectSyncData, toBytes(data)) + 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 +} + +func (c *Client) Search(query ESQuery) ([]string, error) { + var ( + res Response + ) + msg, err := c.natsServer.Request(SubjectSearch, toBytes(query)) + if err != nil { + return res.Data, err + } + if err = json.Unmarshal(msg.Data, &res); err != nil { + return res.Data, err + } + if res.Message != "" { + return res.Data, errors.New(res.Message) + } + return res.Data, nil +} + +func (c *Client) UpdateDocument(query UpdateDataPayload) ([]string, error) { + var ( + res Response + ) + msg, err := c.natsServer.Request(SubjectUpdateDocument, toBytes(query)) + if err != nil { + return res.Data, err + } + if err = json.Unmarshal(msg.Data, &res); err != nil { + return res.Data, err + } + if res.Message != "" { + return res.Data, errors.New(res.Message) + } + return res.Data, nil +} + +func toBytes(data interface{}) []byte { + b, _ := json.Marshal(data) + return b +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..555267f --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module github.com/Selly-Modules/elasticsearch + +go 1.17 + +require github.com/Selly-Modules/natsio v0.0.0-20211202032952-04a8b182fb92 + +require ( + github.com/logrusorgru/aurora v2.0.3+incompatible // indirect + github.com/nats-io/nats.go v1.13.0 // indirect + github.com/nats-io/nkeys v0.3.0 // indirect + github.com/nats-io/nuid v1.0.1 // indirect + github.com/thoas/go-funk v0.9.1 // indirect + golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..beec991 --- /dev/null +++ b/go.sum @@ -0,0 +1,78 @@ +github.com/Selly-Modules/natsio v0.0.0-20211202032952-04a8b182fb92 h1:tZMu1uKbo2l5G7aYRkCtyjQl4Gog4X98mYT/QZC94+8= +github.com/Selly-Modules/natsio v0.0.0-20211202032952-04a8b182fb92/go.mod h1:NG55g9ip18nvN5tfP6PcSEKec10/lOeIOZC8HqBVNlQ= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/klauspost/compress v1.13.4 h1:0zhec2I8zGnjWcKyLl6i3gPqKANCCn5e9xmviEEeX6s= +github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= +github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8= +github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= +github.com/minio/highwayhash v1.0.1 h1:dZ6IIu8Z14VlC0VpfKofAhCy74wu/Qb5gcn52yWoz/0= +github.com/minio/highwayhash v1.0.1/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY= +github.com/nats-io/jwt v1.2.2 h1:w3GMTO969dFg+UOKTmmyuu7IGdusK+7Ytlt//OYH/uU= +github.com/nats-io/jwt v1.2.2/go.mod h1:/xX356yQA6LuXI9xWW7mZNpxgF2mBmGecH+Fj34sP5Q= +github.com/nats-io/jwt/v2 v2.0.3 h1:i/O6cmIsjpcQyWDYNcq2JyZ3/VTF8SJ4JWluI5OhpvI= +github.com/nats-io/jwt/v2 v2.0.3/go.mod h1:VRP+deawSXyhNjXmxPCHskrR6Mq50BqpEI5SEcNiGlY= +github.com/nats-io/nats-server/v2 v2.6.1 h1:cJy+ia7/4EaJL+ZYDmIy2rD1mDWTfckhtPBU0GYo8xM= +github.com/nats-io/nats-server/v2 v2.6.1/go.mod h1:Az91TbZiV7K4a6k/4v6YYdOKEoxCXj+iqhHVf/MlrKo= +github.com/nats-io/nats.go v1.12.3/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w= +github.com/nats-io/nats.go v1.13.0 h1:LvYqRB5epIzZWQp6lmeltOOZNLqCvm4b+qfvzZO03HE= +github.com/nats-io/nats.go v1.13.0/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w= +github.com/nats-io/nkeys v0.2.0/go.mod h1:XdZpAbhgyyODYqjTawOnIOI7VlbKSarI9Gfy1tqEu/s= +github.com/nats-io/nkeys v0.3.0 h1:cgM5tL53EvYRU+2YLXIK0G2mJtK12Ft9oeooSZMA2G8= +github.com/nats-io/nkeys v0.3.0/go.mod h1:gvUNGjVcM2IPr5rCsRsC6Wb3Hr2CQAm08dsxtV6A5y4= +github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= +github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M= +github.com/thoas/go-funk v0.9.1/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI= +golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sys v0.0.0-20190130150945-aca44879d564/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 h1:NusfzzA6yGQ+ua51ck7E3omNUX/JuqbFSaRGqU8CcLI= +golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= +google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/struct.go b/struct.go new file mode 100644 index 0000000..05ec9e1 --- /dev/null +++ b/struct.go @@ -0,0 +1,79 @@ +package elasticsearch + +import "time" + +type Response struct { + Success bool `json:"success"` + Data []string `json:"data,omitempty"` + Total int64 `json:"total,omitempty"` + Page int64 `json:"page,omitempty"` + Limit int64 `json:"limit,omitempty"` + Message string `json:"message"` +} + +type SyncData struct { + Index string + Data []byte +} + +type UpdateDataPayload struct { + Index string + Body []byte +} + +type ESQuery struct { + Index string // Index + Page int64 + Limit int64 + Keyword string + ProvinceCode int + Active string + Categories []string + SubCategories []string + IgnoreIDs []string + Suppliers []string + SlugCites []string + Type string + ServiceDelivery string + SourceDelivery string + Banned string + ListUser []string + ListNotUser []string + PaymentMethod string + Source string + FromNewActiveSeller string + EmailStatus string + MerchantStatus string + IsCalled string + IsAutoApproved string + ProcessStatus string + OutboundRequestStatus string + IsWholesaleBonus string + IsPreorder string + IsDeleted string + Tags []string + Sorts []ESSort + ListStatus []string + ListDeliveryStatus []string + FromAt time.Time + ToAt time.Time + ApprovedFrom time.Time + ApprovedTo time.Time + DeliveredFrom time.Time + DeliveredTo time.Time + CashbackFrom time.Time + CashbackTo time.Time + FromPrice float64 + ToPrice float64 + Inventories []string + NotInventories []string + ReferralCode string + MembershipLevel int + Invitee string + Segments []string +} + +type ESSort struct { + Filed string // Filed sort + Ascending bool +} From 91598d848f98a9d7657cc0b178627e6fea162555 Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Thu, 10 Mar 2022 11:01:34 +0700 Subject: [PATCH 2/4] [Update] Define module elesticsearch --- .gitignore | 1 + .idea/.gitignore | 8 -------- .idea/elasticsearch.iml | 9 --------- .idea/modules.xml | 8 -------- .idea/vcs.xml | 6 ------ .idea/watcherTasks.xml | 9 --------- 6 files changed, 1 insertion(+), 40 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/elasticsearch.iml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml delete mode 100644 .idea/watcherTasks.xml diff --git a/.gitignore b/.gitignore index 66fd13c..33ce4c4 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out +.idea # Dependency directories (remove the comment below to include it) # vendor/ diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/elasticsearch.iml b/.idea/elasticsearch.iml deleted file mode 100644 index 5e764c4..0000000 --- a/.idea/elasticsearch.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index c38a18a..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/watcherTasks.xml b/.idea/watcherTasks.xml deleted file mode 100644 index 27aa2e8..0000000 --- a/.idea/watcherTasks.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - \ No newline at end of file From 0f0482916300a3f744ed5b3b8ae1ac4473eed665 Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Thu, 10 Mar 2022 11:36:06 +0700 Subject: [PATCH 3/4] [Update] Add comment --- config.go | 1 + constants.go | 6 +++--- elasticsearch.go | 9 +++++++++ struct.go | 14 +++++++++++++- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index d0abdef..996f49e 100644 --- a/config.go +++ b/config.go @@ -2,6 +2,7 @@ package elasticsearch import "github.com/Selly-Modules/natsio" +// Config int client elasticsearch type Config struct { ApiKey string Nats natsio.Config diff --git a/constants.go b/constants.go index 9dc6c8f..f3018b0 100644 --- a/constants.go +++ b/constants.go @@ -1,5 +1,5 @@ package elasticsearch -const SubjectSyncData = "sync_data" -const SubjectSearch = "search" -const SubjectUpdateDocument = "update_document" +const SubjectSyncData = "elasticsearch_sync_data" +const SubjectSearch = "elasticsearch_search" +const SubjectUpdateDocument = "elasticsearch_update_document" diff --git a/elasticsearch.go b/elasticsearch.go index f0dbcee..eb32160 100644 --- a/elasticsearch.go +++ b/elasticsearch.go @@ -8,12 +8,15 @@ import ( "github.com/Selly-Modules/natsio" ) +// Client ... type Client struct { Config Config natsServer natsio.Server natsJetStream natsio.JetStream } +// NewClient +// Init client elasticsearch func NewClient(config Config) (*Client, error) { if config.ApiKey == "" { return nil, errors.New("api key is required") @@ -34,6 +37,8 @@ func NewClient(config Config) (*Client, error) { return c, nil } +// SyncData +// Sync data to services ES func (c *Client) SyncData(data SyncData) (bool, error) { var ( res Response @@ -51,6 +56,8 @@ func (c *Client) SyncData(data SyncData) (bool, error) { return res.Success, nil } +// Search +// Request search to service es func (c *Client) Search(query ESQuery) ([]string, error) { var ( res Response @@ -68,6 +75,8 @@ func (c *Client) Search(query ESQuery) ([]string, error) { return res.Data, nil } +// UpdateDocument +// Insert or update document to ES func (c *Client) UpdateDocument(query UpdateDataPayload) ([]string, error) { var ( res Response diff --git a/struct.go b/struct.go index 05ec9e1..38a0519 100644 --- a/struct.go +++ b/struct.go @@ -2,6 +2,8 @@ package elasticsearch import "time" +// Response +// response to service es type Response struct { Success bool `json:"success"` Data []string `json:"data,omitempty"` @@ -11,16 +13,22 @@ type Response struct { Message string `json:"message"` } +// SyncData +// Payload for sync data to service es type SyncData struct { Index string Data []byte } +// UpdateDataPayload +// Payload for insert or update document type UpdateDataPayload struct { Index string Body []byte } +// ESQuery +// Query support to search document type ESQuery struct { Index string // Index Page int64 @@ -73,7 +81,11 @@ type ESQuery struct { Segments []string } +// ESSort +// ES sort with field +// ... filed is sort +// ... ascending [true is asc] [false is desc] type ESSort struct { - Filed string // Filed sort + Field string // Filed sort Ascending bool } From 8ff06996c8f682d5dee004c75148bbc2fc26214b Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Thu, 10 Mar 2022 11:45:38 +0700 Subject: [PATCH 4/4] [Update] Update constants --- constants.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/constants.go b/constants.go index f3018b0..fb397b9 100644 --- a/constants.go +++ b/constants.go @@ -1,5 +1,7 @@ package elasticsearch -const SubjectSyncData = "elasticsearch_sync_data" -const SubjectSearch = "elasticsearch_search" -const SubjectUpdateDocument = "elasticsearch_update_document" +const ( + SubjectSyncData = "elasticsearch/sync_data" + SubjectSearch = "elasticsearch/search" + SubjectUpdateDocument = "elasticsearch/update_document" +)