[Add] Func delete document + index #3

Merged
trunglt251292 merged 2 commits from v1 into master 2022-03-15 08:04:56 +00:00
3 changed files with 49 additions and 15 deletions
Showing only changes of commit a4b47bb79b - Show all commits

View File

@ -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"
) )

View File

@ -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,19 +84,44 @@ 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 { if err != nil {
return false, err return false, err
} }
if err = json.Unmarshal(msg.Data, &res); err != nil { 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 false, err
} }
if res.Message != "" { return true, nil
return res.Success, errors.New(res.Message)
} }
return res.Success, nil
// CreateIndex
// Create index to ES
func (c *Client) CreateIndex(name string) (bool, error) {
var (
req = RequestBody{
ApiKey: c.Config.ApiKey,
Body: toBytes(name),
}
)
err := c.natsJetStream.Publish(JetStreamSearchService, SubjectCreateIndex, toBytes(req))
if err != nil {
return false, err
}
return true, nil
} }
func toBytes(data interface{}) []byte { func toBytes(data interface{}) []byte {

View File

@ -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