-
Notifications
You must be signed in to change notification settings - Fork 107
/
bulk.go
195 lines (179 loc) · 4.7 KB
/
bulk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// SPDX-License-Identifier: Apache-2.0
//
// The OpenSearch Contributors require contributions made to
// this file be licensed under the Apache-2.0 license or a
// compatible open source license.
package main
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"github.com/opensearch-project/opensearch-go/v4"
"github.com/opensearch-project/opensearch-go/v4/opensearchapi"
)
func main() {
if err := example(); err != nil {
fmt.Println(fmt.Sprintf("Error: %s", err))
os.Exit(1)
}
}
func example() error {
// Initialize the client with SSL/TLS enabled.
client, err := opensearchapi.NewClient(
opensearchapi.Config{
Client: opensearch.Config{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // For testing only. Use certificate for validation.
},
Addresses: []string{"https://localhost:9200"},
Username: "admin", // For testing only. Don't store credentials in code.
Password: "myStrongPassword123!",
},
},
)
if err != nil {
return err
}
ctx := context.Background()
movies := "movies"
books := "books"
createResp, err := client.Indices.Create(ctx, opensearchapi.IndicesCreateReq{Index: movies})
if err != nil {
return err
}
fmt.Printf("Index created: %t\n", createResp.Acknowledged)
createResp, err = client.Indices.Create(ctx, opensearchapi.IndicesCreateReq{Index: books})
if err != nil {
return err
}
fmt.Printf("Index created: %t\n", createResp.Acknowledged)
bulkResp, err := client.Bulk(
ctx,
opensearchapi.BulkReq{
Body: strings.NewReader(`{ "index": { "_index": "movies", "_id": 1 } }
{ "title": "Beauty and the Beast", "year": 1991 }
{ "index": { "_index": "movies", "_id": 2 } }
{ "title": "Beauty and the Beast - Live Action", "year": 2017 }
{ "index": { "_index": "books", "_id": 1 } }
{ "title": "The Lion King", "year": 1994 }
`),
},
)
if err != nil {
return err
}
respAsJson, err := json.MarshalIndent(bulkResp, "", " ")
if err != nil {
return err
}
fmt.Printf("Bulk Resp:\n%s\n", string(respAsJson))
bulkResp, err = client.Bulk(
ctx,
opensearchapi.BulkReq{
Body: strings.NewReader(`{ "create": { "_index": "movies" } }
{ "title": "Beauty and the Beast 2", "year": 2030 }
{ "create": { "_index": "movies", "_id": 1 } }
{ "title": "Beauty and the Beast 3", "year": 2031 }
{ "create": { "_index": "movies", "_id": 2 } }
{ "title": "Beauty and the Beast 4", "year": 2049 }
{ "create": { "_index": "books" } }
{ "title": "The Lion King 2", "year": 1998 }
`),
},
)
if err != nil {
return err
}
respAsJson, err = json.MarshalIndent(bulkResp, "", " ")
if err != nil {
return err
}
fmt.Printf("Bulk Resp:\n%s\n", string(respAsJson))
bulkResp, err = client.Bulk(
ctx,
opensearchapi.BulkReq{
Body: strings.NewReader(`{ "update": { "_index": "movies", "_id": 1 } }
{ "doc": { "year": 1992 } }
{ "update": { "_index": "movies", "_id": 1 } }
{ "doc": { "year": 2018 } }
`),
},
)
if err != nil {
return err
}
respAsJson, err = json.MarshalIndent(bulkResp, "", " ")
if err != nil {
return err
}
fmt.Printf("Bulk Resp:\n%s\n", string(respAsJson))
bulkResp, err = client.Bulk(
ctx,
opensearchapi.BulkReq{
Body: strings.NewReader(`{ "delete": { "_index": "movies", "_id": 1 } }
{ "delete": { "_index": "movies", "_id": 2 } }
`),
},
)
if err != nil {
return err
}
respAsJson, err = json.MarshalIndent(bulkResp, "", " ")
if err != nil {
return err
}
fmt.Printf("Bulk Resp:\n%s\n", string(respAsJson))
bulkResp, err = client.Bulk(
ctx,
opensearchapi.BulkReq{
Body: strings.NewReader(`{ "create": { "_index": "movies", "_id": 3 } }
{ "title": "Beauty and the Beast 5", "year": 2050 }
{ "create": { "_index": "movies", "_id": 4 } }
{ "title": "Beauty and the Beast 6", "year": 2051 }
{ "update": { "_index": "movies", "_id": 3 } }
{ "doc": { "year": 2052 } }
{ "delete": { "_index": "movies", "_id": 4 } }
`),
},
)
if err != nil {
return err
}
respAsJson, err = json.MarshalIndent(bulkResp, "", " ")
if err != nil {
return err
}
fmt.Printf("Bulk Resp:\n%s\n", string(respAsJson))
bulkResp, err = client.Bulk(
ctx,
opensearchapi.BulkReq{
Body: strings.NewReader("{\"delete\":{\"_index\":\"movies\",\"_id\":1}}\n"),
},
)
if err != nil {
return err
}
for _, item := range bulkResp.Items {
for operation, resp := range item {
if resp.Status > 299 {
fmt.Printf("Bulk %s Error: %s\n", operation, resp.Result)
}
}
}
delResp, err := client.Indices.Delete(
ctx,
opensearchapi.IndicesDeleteReq{
Indices: []string{"movies", "books"},
Params: opensearchapi.IndicesDeleteParams{IgnoreUnavailable: opensearchapi.ToPointer(true)},
},
)
if err != nil {
return err
}
fmt.Printf("Deleted: %t\n", delResp.Acknowledged)
return nil
}