Skip to content

Commit

Permalink
Add Create to ES
Browse files Browse the repository at this point in the history
  • Loading branch information
gr4c2-2000 committed May 23, 2024
1 parent 79a139b commit e83f843
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ func (e *ElasticSearchConnector) Replace(Index string, Type string, Id string, q
}
return nil
}
func (e *ElasticSearchConnector) Create(ctx context.Context, index string, docType string, query map[string]interface{}) error {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(query); err != nil {
return eris.Wrapf(err, "")
}

err := e.esInterface.Create(context.Background(), index, docType, &buf)
if err != nil {
return eris.Wrapf(err, "")
}
return nil
}

func (e *ElasticSearchConnector) ExecuteQuery(Index string, Type string, query map[string]interface{}) (*ResposeES, error) {
var buf bytes.Buffer
Expand Down
14 changes: 14 additions & 0 deletions pkg/elasticsearch/elasticsearch5.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ func (e *ElasticSearchGatway5) Replace(ctx context.Context, index string, docTyp
}
return nil
}

func (e *ElasticSearchGatway5) Create(ctx context.Context, index string, docType string, document io.Reader) error {

res, err := e.client.Index(index, document, e.client.Index.WithContext(ctx))
if err != nil {
return eris.Wrapf(err, "")
}
defer res.Body.Close()
if res.IsError() {
return eris.New(res.String())
}
return nil
}

func (e *ElasticSearchGatway5) Search(ctx context.Context, index string, docType string, query io.Reader) (*bytes.Buffer, error) {
res, err := e.client.Search(
e.client.Search.WithContext(ctx),
Expand Down
13 changes: 13 additions & 0 deletions pkg/elasticsearch/elasticsearch8.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ func (e *ElasticSearchGatway8) Replace(ctx context.Context, index string, docTyp
return nil
}

func (e *ElasticSearchGatway8) Create(ctx context.Context, index string, docType string, document io.Reader) error {

res, err := e.client.Index(index, document, e.client.Index.WithContext(ctx))
if err != nil {
return eris.Wrapf(err, "")
}
defer res.Body.Close()
if res.IsError() {
return eris.New(res.String())
}
return nil
}

func (e *ElasticSearchGatway8) Search(ctx context.Context, index string, docType string, query io.Reader) (*bytes.Buffer, error) {
res, err := e.client.Search(
e.client.Search.WithContext(ctx),
Expand Down
1 change: 1 addition & 0 deletions pkg/elasticsearch/elasticsearch_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type ElasticSearchInterface interface {
GetESConfig() *config.ElasticSearchConfig
Search(ctx context.Context, index string, docType string, query io.Reader) (*bytes.Buffer, error)
Replace(ctx context.Context, index string, docType string, id string, document io.Reader) error
Create(ctx context.Context, index string, docType string, document io.Reader) error
getDialerContext() func(ctx context.Context, network, address string) (net.Conn, error)
setConnection()
}

0 comments on commit e83f843

Please sign in to comment.