-
Notifications
You must be signed in to change notification settings - Fork 40
/
indexing.go
438 lines (396 loc) · 11.9 KB
/
indexing.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Copyright 2021 by Leipzig University Library, http://ub.uni-leipzig.de
// The Finc Authors, http://finc.info
// Martin Czygan, <[email protected]>
//
// This file is part of some open source application.
//
// Some open source application is free software: you can redistribute
// it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// Some open source application is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Foobar. If not, see <http://www.gnu.org/licenses/>.
//
// @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
package esbulk
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"strings"
"sync"
"time"
"github.com/segmentio/encoding/json"
"github.com/sethgrid/pester"
)
var errParseCannotServerAddr = errors.New("cannot parse server address")
// Options represents bulk indexing options.
type Options struct {
Servers []string
Index string
OpType string
DocType string
BatchSize int
Verbose bool
IDField string
Scheme string // http or https; deprecated, use: Servers.
Username string
Password string
Pipeline string
IncludeTypeName bool // https://www.elastic.co/blog/moving-from-types-to-typeless-apis-in-elasticsearch-7-0
}
// Item represents a bulk action.
type Item struct {
IndexAction struct {
Index string `json:"_index"`
Type string `json:"_type"`
ID string `json:"_id"`
Status int `json:"status"`
Error struct {
Type string `json:"type"`
Reason string `json:"reason"`
IndexUUID string `json:"index_uuid"`
Shard string `json:"shard"`
Index string `json:"index"`
} `json:"error"`
} `json:"index"`
}
// BulkResponse is a response to a bulk request.
type BulkResponse struct {
Took int `json:"took"`
HasErrors bool `json:"errors"`
Items []Item `json:"items"`
}
// nestedStr handles the nested JSON values.
func nestedStr(tokstr []string, docmap map[string]interface{}, currentID string) interface{} {
thistok := tokstr[0]
tempStr2, ok := docmap[thistok].(map[string]interface{})
if !ok {
return nil
}
var TokenVal interface{}
var ok1 bool
TokenVal = tempStr2
for count3 := 1; count3 < len(tokstr); count3++ {
thistok = tokstr[count3]
TokenVal, ok1 = tempStr2[thistok]
if !ok1 {
return nil
}
if count3 < len(tokstr)-1 {
tempStr2 = TokenVal.(map[string]interface{})
}
}
return TokenVal
}
// BulkIndex takes a set of documents as strings and indexes them into elasticsearch.
func BulkIndex(docs []string, options Options) error {
if len(docs) == 0 {
return nil
}
rand.Seed(time.Now().Unix())
server := options.Servers[rand.Intn(len(options.Servers))]
link := fmt.Sprintf("%s/_bulk", server)
if options.Pipeline != "" {
link = fmt.Sprintf("%s/_bulk?pipeline=%s", server, options.Pipeline)
}
var lines []string
for _, doc := range docs {
if len(strings.TrimSpace(doc)) == 0 {
continue
}
var header string
if options.DocType == "" {
header = fmt.Sprintf(`{"%s": {"_index": "%s"}}`, options.OpType, options.Index)
} else {
header = fmt.Sprintf(`{"%s": {"_index": "%s", "_type": "%s"}}`, options.OpType, options.Index, options.DocType)
}
// If an "-id" is given, peek into the document to extract the ID and
// use it in the header.
if options.IDField != "" {
var docmap map[string]interface{}
dec := json.NewDecoder(strings.NewReader(doc))
dec.UseNumber()
if err := dec.Decode(&docmap); err != nil {
return fmt.Errorf("failed to json decode doc: %v", err)
}
idstring := options.IDField // A delimiter separates string with all the fields to be used as ID.
id := strings.FieldsFunc(idstring, func(r rune) bool { return r == ',' || r == ' ' })
// ID can be any type at this point, try to find a string
// representation or bail out.
var idstr string
var currentID string
for counter := range id {
currentID = id[counter]
tokstr := strings.Split(currentID, ".")
var TokenVal interface{}
if len(tokstr) > 1 {
TokenVal = nestedStr(tokstr, docmap, currentID)
if TokenVal == nil {
return fmt.Errorf("document has no ID field (%s): %s", currentID, doc)
}
} else {
var ok2 bool
TokenVal, ok2 = docmap[currentID]
if !ok2 {
return fmt.Errorf("document has no ID field (%s): %s", currentID, doc)
}
}
switch tempStr1 := interface{}(TokenVal).(type) {
case string:
idstr = idstr + tempStr1
case fmt.Stringer:
idstr = idstr + tempStr1.String()
case json.Number:
idstr = idstr + tempStr1.String()
default:
return fmt.Errorf("cannot convert id value to string")
}
}
if options.DocType == "" {
header = fmt.Sprintf(`{"%s": {"_index": "%s", "_id": %q}}`, options.OpType, options.Index, idstr)
} else {
header = fmt.Sprintf(`{"%s": {"_index": "%s", "_type": "%s", "_id": %q}}`,
options.OpType, options.Index, options.DocType, idstr)
}
// Remove the IDField if it is accidentally named '_id', since
// Field [_id] is a metadata field and cannot be added inside a
// document.
var flag int
for count := range id {
if id[count] == "_id" {
flag = 1 // Check if any of the id fields to be concatenated is named '_id'.
}
}
if flag == 1 {
delete(docmap, "_id")
b, err := json.Marshal(docmap)
if err != nil {
return err
}
doc = string(b)
}
}
if options.OpType == "update" {
doc = fmt.Sprintf(`{"doc": %s, "doc_as_upsert" : true}`, doc)
}
lines = append(lines, header, doc)
}
body := fmt.Sprintf("%s\n", strings.Join(lines, "\n"))
if options.Verbose {
log.Printf("message content-length will be %d", len(body))
}
// There are multiple ways indexing can fail, e.g. connection errors or
// bad requests. Finally, if we have a HTTP 200, the bulk request could
// still have failed: for that we need to decode the elasticsearch
// response.
req, err := http.NewRequest("POST", link, strings.NewReader(body))
if err != nil {
return err
}
if options.Username != "" && options.Password != "" {
req.SetBasicAuth(options.Username, options.Password)
}
req.Header.Set("Content-Type", "application/json")
response, err := pester.Do(req)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode >= 400 {
var buf bytes.Buffer
if _, err := io.Copy(&buf, response.Body); err != nil {
return err
}
return fmt.Errorf("indexing failed with %d %s: %s",
response.StatusCode, http.StatusText(response.StatusCode), buf.String())
}
var br BulkResponse
if err := json.NewDecoder(response.Body).Decode(&br); err != nil {
return err
}
if br.HasErrors {
if options.Verbose {
log.Println("error details: ")
for _, v := range br.Items {
log.Printf(" %q\n", v.IndexAction.Error)
}
}
log.Printf("request body: %s", body)
return fmt.Errorf("error during bulk operation, check error details; maybe try fewer workers (-w) or increase thread_pool.bulk.queue_size in your nodes")
}
return nil
}
// Worker will batch index documents that come in on the lines channel.
func Worker(id string, options Options, lines chan string, wg *sync.WaitGroup) {
defer wg.Done()
var docs []string
counter := 0
for s := range lines {
docs = append(docs, s)
counter++
if counter%options.BatchSize == 0 {
msg := make([]string, len(docs))
if n := copy(msg, docs); n != len(docs) {
log.Fatalf("expected %d, but got %d", len(docs), n)
}
if err := BulkIndex(msg, options); err != nil {
log.Fatal(err)
}
if options.Verbose {
log.Printf("[%s] @%d\n", id, counter)
}
docs = nil
}
}
if len(docs) == 0 {
return
}
msg := make([]string, len(docs))
if n := copy(msg, docs); n != len(docs) {
log.Fatalf("expected %d, but got %d", len(docs), n)
}
if err := BulkIndex(msg, options); err != nil {
log.Fatal(err)
}
if options.Verbose {
log.Printf("[%s] @%d\n", id, counter)
}
}
// PutMapping applies a mapping from a reader.
func PutMapping(options Options, body io.Reader) error {
rand.Seed(time.Now().Unix())
server := options.Servers[rand.Intn(len(options.Servers))]
var link string
if options.DocType == "" {
link = fmt.Sprintf("%s/%s/_mapping", server, options.Index)
} else {
if options.IncludeTypeName {
// https://www.elastic.co/blog/moving-from-types-to-typeless-apis-in-elasticsearch-7-0
link = fmt.Sprintf("%s/%s/_mapping/%s?include_type_name=true", server, options.Index, options.DocType)
} else {
link = fmt.Sprintf("%s/%s/_mapping/%s", server, options.Index, options.DocType)
}
}
if options.Verbose {
log.Printf("applying mapping: %s", link)
}
req, err := http.NewRequest("PUT", link, body)
if err != nil {
return err
}
if options.Username != "" && options.Password != "" {
req.SetBasicAuth(options.Username, options.Password)
}
req.Header.Set("Content-Type", "application/json")
resp, err := pester.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
var buf bytes.Buffer
if _, err := io.Copy(&buf, resp.Body); err != nil {
return err
}
return fmt.Errorf("failed to apply mapping with %s: %s", resp.Status, buf.String())
}
if options.Verbose {
log.Printf("applied mapping: %s", resp.Status)
}
return resp.Body.Close()
}
// CreateIndex creates a new index.
func CreateIndex(options Options, body io.Reader) error {
rand.Seed(time.Now().Unix())
server := options.Servers[rand.Intn(len(options.Servers))]
link := fmt.Sprintf("%s/%s", server, options.Index)
req, err := http.NewRequest("GET", link, nil)
if err != nil {
return err
}
if options.Username != "" && options.Password != "" {
req.SetBasicAuth(options.Username, options.Password)
}
req.Header.Set("Content-Type", "application/json")
resp, err := pester.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// Index already exists, return.
if resp.StatusCode == 200 {
return nil
}
req, err = http.NewRequest("PUT", fmt.Sprintf("%s/%s/", server, options.Index), body)
if err != nil {
return err
}
if options.Username != "" && options.Password != "" {
req.SetBasicAuth(options.Username, options.Password)
}
req.Header.Set("Content-Type", "application/json")
resp, err = pester.Do(req)
if err != nil {
return nil
}
defer resp.Body.Close()
// Elasticsearch backwards compat.
if resp.StatusCode == 400 {
var errResponse struct {
Error string `json:"error"`
Status int `json:"status"`
}
var buf bytes.Buffer
rdr := io.TeeReader(resp.Body, &buf)
// Might return a 400 on "No handler found for uri" ...
if err := json.NewDecoder(rdr).Decode(&errResponse); err == nil {
if strings.Contains(errResponse.Error, "IndexAlreadyExistsException") {
return nil
}
}
log.Printf("elasticsearch response was: %s", buf.String())
}
if resp.StatusCode >= 400 {
var buf bytes.Buffer
if _, err := io.Copy(&buf, resp.Body); err != nil {
return err
}
return errors.New(buf.String())
}
if options.Verbose {
log.Printf("created index: %s\n", resp.Status)
}
return nil
}
// DeleteIndex removes an index.
func DeleteIndex(options Options) error {
rand.Seed(time.Now().Unix())
server := options.Servers[rand.Intn(len(options.Servers))]
link := fmt.Sprintf("%s/%s", server, options.Index)
req, err := http.NewRequest("DELETE", link, nil)
if err != nil {
return err
}
if options.Username != "" && options.Password != "" {
req.SetBasicAuth(options.Username, options.Password)
}
req.Header.Set("Content-Type", "application/json")
resp, err := pester.Do(req)
if err != nil {
return err
}
if options.Verbose {
log.Printf("purged index: %s", resp.Status)
}
return resp.Body.Close()
}