MCPcopy
hub / github.com/elastic/go-elasticsearch / ExampleNewBulkIndexer

Function ExampleNewBulkIndexer

esutil/bulk_indexer_example_test.go:34–140  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

32)
33
34func ExampleNewBulkIndexer() {
35 log.SetFlags(0)
36
37 // Create the Elasticsearch client
38 //
39 es, err := elasticsearch.NewClient(elasticsearch.Config{
40 // Retry on 429 TooManyRequests statuses
41 //
42 RetryOnStatus: []int{502, 503, 504, 429},
43
44 // A simple incremental backoff function
45 //
46 RetryBackoff: func(i int) time.Duration { return time.Duration(i) * 100 * time.Millisecond },
47
48 // Retry up to 5 attempts
49 //
50 MaxRetries: 5,
51 })
52 if err != nil {
53 log.Fatalf("Error creating the client: %s", err)
54 }
55 defer func() {
56 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
57 defer cancel()
58 if closeErr := es.Close(ctx); closeErr != nil {
59 log.Fatalf("Error closing the client: %s", closeErr)
60 }
61 }()
62
63 // Create the indexer
64 //
65 indexer, err := esutil.NewBulkIndexer(esutil.BulkIndexerConfig{
66 Client: es, // The Elasticsearch client
67 Index: "test", // The default index name
68 NumWorkers: 4, // The number of worker goroutines (default: number of CPUs)
69 FlushBytes: 5e+6, // The flush threshold in bytes (default: 5M)
70 })
71 if err != nil {
72 log.Printf("Error creating the indexer: %s", err)
73 return
74 }
75
76 // Add an item to the indexer
77 //
78 err = indexer.Add(
79 context.Background(),
80 esutil.BulkIndexerItem{
81 // Action field configures the operation to perform (index, create, delete, update)
82 Action: "index",
83
84 // DocumentID is the optional document ID
85 DocumentID: "1",
86
87 // Body is an `io.Reader` with the payload
88 Body: strings.NewReader(`{"title":"Test"}`),
89
90 // OnSuccess is the optional callback for each successful operation
91 OnSuccess: func(

Callers

nothing calls this directly

Calls 7

CloseMethod · 0.95
AddMethod · 0.95
StatsMethod · 0.95
NewBulkIndexerFunction · 0.92
DurationMethod · 0.80
PrintfMethod · 0.80
WithTimeoutMethod · 0.45

Tested by

no test coverage detected