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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
import (
"context"
"encoding/json"
"testing"
)
func TestIndexLifecycle(t *testing.T) {
client := setupTestClientAndCreateIndex(t)
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
// Add a document
indexResult, err := client.Index().
Index(testIndexName).
Type("doc").
Id("1").
BodyJson(&tweet1).
Do(context.TODO())
if err != nil {
t.Fatal(err)
}
if indexResult == nil {
t.Errorf("expected result to be != nil; got: %v", indexResult)
}
// Exists
exists, err := client.Exists().Index(testIndexName).Type("doc").Id("1").Do(context.TODO())
if err != nil {
t.Fatal(err)
}
if !exists {
t.Errorf("expected exists %v; got %v", true, exists)
}
// Get document
getResult, err := client.Get().
Index(testIndexName).
Type("doc").
Id("1").
Do(context.TODO())
if err != nil {
t.Fatal(err)
}
if getResult.Index != testIndexName {
t.Errorf("expected GetResult.Index %q; got %q", testIndexName, getResult.Index)
}
if getResult.Type != "doc" {
t.Errorf("expected GetResult.Type %q; got %q", "doc", getResult.Type)
}
if getResult.Id != "1" {
t.Errorf("expected GetResult.Id %q; got %q", "1", getResult.Id)
}
if getResult.Source == nil {
t.Errorf("expected GetResult.Source to be != nil; got nil")
}
// Decode the Source field
var tweetGot tweet
err = json.Unmarshal(*getResult.Source, &tweetGot)
if err != nil {
t.Fatal(err)
}
if tweetGot.User != tweet1.User {
t.Errorf("expected Tweet.User to be %q; got %q", tweet1.User, tweetGot.User)
}
if tweetGot.Message != tweet1.Message {
t.Errorf("expected Tweet.Message to be %q; got %q", tweet1.Message, tweetGot.Message)
}
// Delete document again
deleteResult, err := client.Delete().Index(testIndexName).Type("doc").Id("1").Do(context.TODO())
if err != nil {
t.Fatal(err)
}
if deleteResult == nil {
t.Errorf("expected result to be != nil; got: %v", deleteResult)
}
// Exists
exists, err = client.Exists().Index(testIndexName).Type("doc").Id("1").Do(context.TODO())
if err != nil {
t.Fatal(err)
}
if exists {
t.Errorf("expected exists %v; got %v", false, exists)
}
}
func TestIndexLifecycleWithAutomaticIDGeneration(t *testing.T) {
client := setupTestClientAndCreateIndex(t)
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
// Add a document
indexResult, err := client.Index().
Index(testIndexName).
Type("doc").
BodyJson(&tweet1).
Do(context.TODO())
if err != nil {
t.Fatal(err)
}
if indexResult == nil {
t.Errorf("expected result to be != nil; got: %v", indexResult)
}
if indexResult.Id == "" {
t.Fatalf("expected Es to generate an automatic ID, got: %v", indexResult.Id)
}
id := indexResult.Id
// Exists
exists, err := client.Exists().Index(testIndexName).Type("doc").Id(id).Do(context.TODO())
if err != nil {
t.Fatal(err)
}
if !exists {
t.Errorf("expected exists %v; got %v", true, exists)
}
// Get document
getResult, err := client.Get().
Index(testIndexName).
Type("doc").
Id(id).
Do(context.TODO())
if err != nil {
t.Fatal(err)
}
if getResult.Index != testIndexName {
t.Errorf("expected GetResult.Index %q; got %q", testIndexName, getResult.Index)
}
if getResult.Type != "doc" {
t.Errorf("expected GetResult.Type %q; got %q", "doc", getResult.Type)
}
if getResult.Id != id {
t.Errorf("expected GetResult.Id %q; got %q", id, getResult.Id)
}
if getResult.Source == nil {
t.Errorf("expected GetResult.Source to be != nil; got nil")
}
// Decode the Source field
var tweetGot tweet
err = json.Unmarshal(*getResult.Source, &tweetGot)
if err != nil {
t.Fatal(err)
}
if tweetGot.User != tweet1.User {
t.Errorf("expected Tweet.User to be %q; got %q", tweet1.User, tweetGot.User)
}
if tweetGot.Message != tweet1.Message {
t.Errorf("expected Tweet.Message to be %q; got %q", tweet1.Message, tweetGot.Message)
}
// Delete document again
deleteResult, err := client.Delete().Index(testIndexName).Type("doc").Id(id).Do(context.TODO())
if err != nil {
t.Fatal(err)
}
if deleteResult == nil {
t.Errorf("expected result to be != nil; got: %v", deleteResult)
}
// Exists
exists, err = client.Exists().Index(testIndexName).Type("doc").Id(id).Do(context.TODO())
if err != nil {
t.Fatal(err)
}
if exists {
t.Errorf("expected exists %v; got %v", false, exists)
}
}
func TestIndexValidate(t *testing.T) {
client := setupTestClient(t)
tweet := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
// No index name -> fail with error
res, err := NewIndexService(client).Type("doc").Id("1").BodyJson(&tweet).Do(context.TODO())
if err == nil {
t.Fatalf("expected Index to fail without index name")
}
if res != nil {
t.Fatalf("expected result to be == nil; got: %v", res)
}
// No index name -> fail with error
res, err = NewIndexService(client).Index(testIndexName).Id("1").BodyJson(&tweet).Do(context.TODO())
if err == nil {
t.Fatalf("expected Index to fail without type")
}
if res != nil {
t.Fatalf("expected result to be == nil; got: %v", res)
}
}
func TestIndexCreateExistsOpenCloseDelete(t *testing.T) {
// TODO: Find out how to make these test robust
t.Skip("test fails regularly with 409 (Conflict): " +
"IndexPrimaryShardNotAllocatedException[[elastic-test] " +
"primary not allocated post api... skipping")
client := setupTestClient(t)
// Create index
createIndex, err := client.CreateIndex(testIndexName).Body(testMapping).Do(context.TODO())
if err != nil {
t.Fatal(err)
}
if createIndex == nil {
t.Fatalf("expected response; got: %v", createIndex)
}
if !createIndex.Acknowledged {
t.Errorf("expected ack for creating index; got: %v", createIndex.Acknowledged)
}
// Exists
indexExists, err := client.IndexExists(testIndexName).Do(context.TODO())
if err != nil {
t.Fatal(err)
}
if !indexExists {
t.Fatalf("expected index exists=%v; got %v", true, indexExists)
}
// Flush
_, err = client.Flush().Index(testIndexName).Do(context.TODO())
if err != nil {
t.Fatal(err)
}
// Close index
closeIndex, err := client.CloseIndex(testIndexName).Do(context.TODO())
if err != nil {
t.Fatal(err)
}
if closeIndex == nil {
t.Fatalf("expected response; got: %v", closeIndex)
}
if !closeIndex.Acknowledged {
t.Errorf("expected ack for closing index; got: %v", closeIndex.Acknowledged)
}
// Open index
openIndex, err := client.OpenIndex(testIndexName).Do(context.TODO())
if err != nil {
t.Fatal(err)
}
if openIndex == nil {
t.Fatalf("expected response; got: %v", openIndex)
}
if !openIndex.Acknowledged {
t.Errorf("expected ack for opening index; got: %v", openIndex.Acknowledged)
}
// Flush
_, err = client.Flush().Index(testIndexName).Do(context.TODO())
if err != nil {
t.Fatal(err)
}
// Delete index
deleteIndex, err := client.DeleteIndex(testIndexName).Do(context.TODO())
if err != nil {
t.Fatal(err)
}
if deleteIndex == nil {
t.Fatalf("expected response; got: %v", deleteIndex)
}
if !deleteIndex.Acknowledged {
t.Errorf("expected ack for deleting index; got %v", deleteIndex.Acknowledged)
}
}
|