111 lines
2.8 KiB
Go
111 lines
2.8 KiB
Go
package elastic
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"cybertron/configs"
|
|
"encoding/json"
|
|
elasticsearch8 "github.com/elastic/go-elasticsearch/v8"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
type ElasticSearchClient struct {
|
|
client *elasticsearch8.TypedClient
|
|
Config configs.ElasticConfig
|
|
}
|
|
|
|
func NewElasticClient(elasticConfig configs.ElasticConfig) (*ElasticSearchClient, error) {
|
|
cfg := elasticsearch8.Config{
|
|
Addresses: elasticConfig.Addresses,
|
|
Username: elasticConfig.Username,
|
|
Password: elasticConfig.Password,
|
|
Transport: &http.Transport{
|
|
TLSClientConfig: &tls.Config{
|
|
InsecureSkipVerify: true,
|
|
},
|
|
},
|
|
}
|
|
|
|
client, err := elasticsearch8.NewTypedClient(cfg)
|
|
return &ElasticSearchClient{
|
|
client: client,
|
|
Config: elasticConfig,
|
|
}, err
|
|
}
|
|
|
|
func (el *ElasticSearchClient) IndexDocument(document interface{}) {
|
|
_, err := el.client.Index(el.Config.Index).
|
|
Request(document).
|
|
Do(context.TODO())
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func (el *ElasticSearchClient) DeleteDocuments(searchRequest string) {
|
|
_, err := el.client.DeleteByQuery(el.Config.Index).Raw(strings.NewReader(searchRequest)).Do(context.TODO())
|
|
if err != nil {
|
|
log.Printf("unable to delete documents: %s", err.Error())
|
|
return
|
|
}
|
|
log.Printf("successfully deleted documents: %s", searchRequest)
|
|
}
|
|
func (el *ElasticSearchClient) SearchDocuments(searchRequest string, fields []string) ([]map[string]interface{}, map[string]interface{}, int64, error) {
|
|
res, err := el.client.Search().
|
|
Index(el.Config.Index).Raw(strings.NewReader(searchRequest)).
|
|
Do(context.TODO())
|
|
if err != nil {
|
|
log.Println("Error getting response: %s", err)
|
|
return nil, nil, 0, err
|
|
}
|
|
|
|
var results []map[string]interface{}
|
|
for _, hit := range res.Hits.Hits {
|
|
var doc map[string]interface{}
|
|
|
|
err := json.Unmarshal(hit.Source_, &doc)
|
|
doc["id"] = *hit.Id_
|
|
if err != nil {
|
|
log.Printf("Error unmarshalling document: %s", err)
|
|
continue
|
|
}
|
|
results = append(results, doc)
|
|
}
|
|
aggregations := make(map[string]interface{})
|
|
if res.Aggregations != nil {
|
|
for aggName, agg := range res.Aggregations {
|
|
aggregations[aggName] = agg
|
|
}
|
|
}
|
|
|
|
return results, aggregations, res.Hits.Total.Value, nil
|
|
}
|
|
|
|
func (el *ElasticSearchClient) GetDocument(documentID string) (interface{}, error) {
|
|
// Retrieve the document by its ID
|
|
res, err := el.client.Get(el.Config.Index, documentID).Do(context.Background())
|
|
var document interface{}
|
|
if err != nil {
|
|
log.Printf("Error getting response: %s", err)
|
|
return nil, err
|
|
}
|
|
|
|
// Check if the document exists
|
|
if !res.Found {
|
|
log.Printf("Document with ID %s not found", documentID)
|
|
return document, nil
|
|
}
|
|
|
|
// Unmarshal the JSON response into the provided document interface
|
|
err = json.Unmarshal(res.Source_, &document)
|
|
if err != nil {
|
|
log.Printf("Error unmarshalling document: %s", err)
|
|
return nil, err
|
|
}
|
|
|
|
return document, nil
|
|
}
|