45 lines
936 B
Go
45 lines
936 B
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
elasticsearch8 "github.com/elastic/go-elasticsearch/v8"
|
|
"log"
|
|
"log-enricher/configs"
|
|
"net/http"
|
|
)
|
|
|
|
type ElasticSearchClient struct {
|
|
client *elasticsearch8.TypedClient
|
|
}
|
|
|
|
func NewElasticClient(elasticConfig configs.ElasticConfig) (*ElasticSearchClient, error) {
|
|
cfg := elasticsearch8.Config{
|
|
Addresses: elasticConfig.Addresses,
|
|
APIKey: elasticConfig.APIKey,
|
|
Transport: &http.Transport{
|
|
TLSClientConfig: &tls.Config{
|
|
InsecureSkipVerify: true,
|
|
},
|
|
},
|
|
}
|
|
|
|
client, err := elasticsearch8.NewTypedClient(cfg)
|
|
if err != nil {
|
|
log.Fatalf("Error creating the client: %s", err)
|
|
}
|
|
return &ElasticSearchClient{
|
|
client: client,
|
|
}, err
|
|
}
|
|
|
|
func (el *ElasticSearchClient) IndexDocument(document interface{}) {
|
|
_, err := el.client.Index("cybertron").
|
|
Request(document).
|
|
Do(context.Background())
|
|
|
|
if err != nil {
|
|
log.Fatalf("Error ingesting the doc: %s", err)
|
|
}
|
|
}
|