2024-05-20 16:11:50 +08:00
|
|
|
package es7
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-12-13 15:01:40 +08:00
|
|
|
|
2024-05-20 16:11:50 +08:00
|
|
|
elastic "github.com/elastic/go-elasticsearch/v7"
|
|
|
|
"github.com/elastic/go-elasticsearch/v7/esutil"
|
2025-02-05 18:07:53 +08:00
|
|
|
"github.com/loveuer/esgo2dump/pkg/log"
|
|
|
|
"github.com/loveuer/esgo2dump/pkg/model"
|
2024-05-20 16:11:50 +08:00
|
|
|
)
|
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
func WriteData[T any](ctx context.Context, client *elastic.Client, index string, docs ...*model.ESSource[T]) error {
|
2024-05-20 16:11:50 +08:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
indexer esutil.BulkIndexer
|
2024-05-24 17:27:52 +08:00
|
|
|
total int
|
2024-05-20 16:11:50 +08:00
|
|
|
)
|
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
if len(docs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-20 16:11:50 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
count := 0
|
2024-05-20 16:11:50 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
if indexer, err = esutil.NewBulkIndexer(esutil.BulkIndexerConfig{
|
|
|
|
NumWorkers: 0,
|
|
|
|
FlushBytes: 0,
|
|
|
|
FlushInterval: 0,
|
|
|
|
Client: client,
|
|
|
|
Decoder: nil,
|
|
|
|
OnError: func(ctx context.Context, err error) {
|
|
|
|
log.Error("es7.writer: on error log, err = %s", err.Error())
|
|
|
|
},
|
|
|
|
Index: index,
|
|
|
|
ErrorTrace: true,
|
|
|
|
FilterPath: []string{},
|
|
|
|
Header: map[string][]string{},
|
|
|
|
Human: false,
|
|
|
|
Pipeline: "",
|
|
|
|
Pretty: false,
|
|
|
|
Refresh: "",
|
|
|
|
Routing: "",
|
|
|
|
Source: []string{},
|
|
|
|
SourceExcludes: []string{},
|
|
|
|
SourceIncludes: []string{},
|
|
|
|
Timeout: 0,
|
|
|
|
WaitForActiveShards: "",
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-20 16:11:50 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
for _, doc := range docs {
|
|
|
|
var bs []byte
|
2024-05-20 16:11:50 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
if bs, err = json.Marshal(doc.Content); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-20 16:11:50 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
if err = indexer.Add(context.Background(), esutil.BulkIndexerItem{
|
|
|
|
Action: "index",
|
|
|
|
Index: index,
|
|
|
|
DocumentID: doc.DocId,
|
|
|
|
Body: bytes.NewReader(bs),
|
|
|
|
OnFailure: func(ctx context.Context, item esutil.BulkIndexerItem, item2 esutil.BulkIndexerResponseItem, bulkErr error) {
|
|
|
|
log.Error("es7.writer: on failure err log, err = %s", bulkErr.Error())
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-20 16:11:50 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
count++
|
|
|
|
}
|
2024-05-24 17:27:52 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
total += count
|
2024-05-20 16:11:50 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
if err = indexer.Close(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-24 17:27:52 +08:00
|
|
|
|
2025-02-05 18:07:53 +08:00
|
|
|
stats := indexer.Stats()
|
|
|
|
if stats.NumFailed > 0 {
|
|
|
|
return fmt.Errorf("write to es failed_count=%d bulk_count=%d", stats.NumFailed, count)
|
2024-05-20 16:11:50 +08:00
|
|
|
}
|
2025-02-05 18:07:53 +08:00
|
|
|
|
|
|
|
return nil
|
2024-05-20 16:11:50 +08:00
|
|
|
}
|