[Add] Func delete document + index #3
|
@ -4,4 +4,10 @@ const (
|
||||||
SubjectSyncData = "elasticsearch/sync_data"
|
SubjectSyncData = "elasticsearch/sync_data"
|
||||||
SubjectSearch = "elasticsearch/search"
|
SubjectSearch = "elasticsearch/search"
|
||||||
SubjectUpdateDocument = "elasticsearch/update_document"
|
SubjectUpdateDocument = "elasticsearch/update_document"
|
||||||
|
SubjectCreateIndex = "elasticsearch/create_index"
|
||||||
|
SubjectDeleteDocument = "elasticsearch/delete_document"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
JetStreamSearchService = "JetStreamSearchService"
|
||||||
)
|
)
|
||||||
|
|
|
@ -41,23 +41,16 @@ func NewClient(config Config) (*Client, error) {
|
||||||
// Sync data to services ES
|
// 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
|
|
||||||
req = RequestBody{
|
req = RequestBody{
|
||||||
ApiKey: c.Config.ApiKey,
|
ApiKey: c.Config.ApiKey,
|
||||||
Body: toBytes(data),
|
Body: toBytes(data),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
msg, err := c.natsServer.Request(SubjectSyncData, toBytes(req))
|
err := c.natsJetStream.Publish(JetStreamSearchService, SubjectSyncData, toBytes(req))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
if err = json.Unmarshal(msg.Data, &res); err != nil {
|
return true, nil
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
if res.Message != "" {
|
|
||||||
return false, errors.New(res.Message)
|
|
||||||
}
|
|
||||||
return res.Success, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search
|
// Search
|
||||||
|
@ -91,9 +84,41 @@ func (c *Client) UpdateDocument(query UpdateDataPayload) (bool, error) {
|
||||||
ApiKey: c.Config.ApiKey,
|
ApiKey: c.Config.ApiKey,
|
||||||
Body: toBytes(query),
|
Body: toBytes(query),
|
||||||
}
|
}
|
||||||
res Response
|
|
||||||
)
|
)
|
||||||
msg, err := c.natsServer.Request(SubjectUpdateDocument, toBytes(req))
|
err := c.natsJetStream.Publish(JetStreamSearchService, SubjectUpdateDocument, toBytes(req))
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
res *Response
|
||||||
|
)
|
||||||
|
msg, err := c.natsServer.Request(SubjectCreateIndex, toBytes(req))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
@ -101,7 +126,7 @@ func (c *Client) UpdateDocument(query UpdateDataPayload) (bool, error) {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
if res.Message != "" {
|
if res.Message != "" {
|
||||||
return res.Success, errors.New(res.Message)
|
return false, errors.New(res.Message)
|
||||||
}
|
}
|
||||||
return res.Success, nil
|
return res.Success, nil
|
||||||
}
|
}
|
||||||
|
|
10
struct.go
10
struct.go
|
@ -33,6 +33,13 @@ type UpdateDataPayload struct {
|
||||||
Body []byte
|
Body []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteDataPayload
|
||||||
|
// Payload for delete document
|
||||||
|
type DeleteDataPayload struct {
|
||||||
|
Index string
|
||||||
|
ID string
|
||||||
|
}
|
||||||
|
|
||||||
// ESQuery
|
// ESQuery
|
||||||
// Query support to search document
|
// Query support to search document
|
||||||
type ESQuery struct {
|
type ESQuery struct {
|
||||||
|
@ -42,6 +49,9 @@ type ESQuery struct {
|
||||||
Keyword string
|
Keyword string
|
||||||
ProvinceCode int
|
ProvinceCode int
|
||||||
Active string
|
Active string
|
||||||
|
IsOutOfStock string
|
||||||
|
CanIssueInvoice string
|
||||||
|
PendingInactive string
|
||||||
Categories []string
|
Categories []string
|
||||||
SubCategories []string
|
SubCategories []string
|
||||||
IgnoreIDs []string
|
IgnoreIDs []string
|
||||||
|
|
Loading…
Reference in New Issue