-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-map.go
152 lines (134 loc) · 4.04 KB
/
update-map.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
package main
import (
"bufio"
"bytes"
"context"
"crypto/tls"
"encoding/json"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
)
func main() {
log.Println("I will fill your database with some data")
log.Println(strings.Repeat("-", 37))
// Create default Client
cfg := elasticsearch.Config{
Addresses: []string{
"http://localhost:9200",
"http://localhost:9201",
},
Transport: &http.Transport{
MaxIdleConnsPerHost: 10,
ResponseHeaderTimeout: 5 * time.Second,
DialContext: (&net.Dialer{Timeout: 5 * time.Second}).DialContext,
TLSClientConfig: &tls.Config{
MaxVersion: tls.VersionTLS11,
InsecureSkipVerify: true,
},
},
}
es, err := elasticsearch.NewClient(cfg)
if err != nil {
log.Fatalf("Error creating the client: %s", err)
} else {
//log.Println(es.Info())
}
file, err := os.Open("source/extra_data.txt")
if err != nil {
log.Fatalf("failed opening file: %s", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
if !scanner.Scan() {
log.Fatalf("Empty file")
}
col_names := strings.Fields(scanner.Text())
gett_res, err := es.Indices.GetTemplate(
es.Indices.GetTemplate.WithName("test-template"),
es.Indices.GetTemplate.WithPretty(),
)
log.Println(gett_res)
createCnf := make(map[string]interface{})
if err := json.NewDecoder(gett_res.Body).Decode(&createCnf); err != nil {
log.Fatalf("Error parsing the response body: %s", err)
}
createCnf["test-template"].(map[string]interface{})["mappings"].(map[string]interface{})["properties"].(map[string]interface{})["arg_num"] = make(map[string]interface{})
createCnf["test-template"].(map[string]interface{})["mappings"].(map[string]interface{})["properties"].(map[string]interface{})["arg_num"].(map[string]interface{})["type"] = "long"
var buf bytes.Buffer
// we shouldn't write "mappings : {...}"
if err := json.NewEncoder(&buf).Encode(createCnf["test-template"]); err != nil {
log.Fatalf("Error encoding createCnf: %s", err)
}
// on main doc write wrong func args!!!!
// https://pkg.go.dev/github.com/elastic/[email protected]/esapi#IndicesPutMapping
// It hasn't WithIndex(...string) method
res, err := es.Indices.PutTemplate(
"test-template",
strings.NewReader(buf.String()),
es.Indices.PutTemplate.WithOrder(1),
es.Indices.PutTemplate.WithCreate(false),
)
if err != nil {
log.Fatalf("update mapping creation error: %s", err)
} else {
log.Println(res)
}
log.Println(strings.Repeat("-", 37))
defer res.Body.Close()
// Fill database
i := 1
var wg sync.WaitGroup
for scanner.Scan() {
wg.Add(1)
line := scanner.Text()
go func(i int, line string, col_names []string) {
defer wg.Done()
var body bytes.Buffer
request := make(map[string]string)
// Build the request body.
for i, field := range strings.Fields(line) {
request[col_names[i]] = field
}
if err := json.NewEncoder(&body).Encode(request); err != nil {
log.Fatalf("Error encoding request: %s", err)
}
// Set up the request object.
req := esapi.IndexRequest{
Index: "test",
DocumentID: strconv.Itoa(50 + i),
Body: strings.NewReader(body.String()),
Refresh: "true",
}
// Perform the request with the client.
res, err := req.Do(context.Background(), es)
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
defer res.Body.Close()
if res.IsError() {
log.Printf("[%s] Error indexing document ID=%d", res.Status(), i+1)
} else {
// Deserialize the response into a map.
var r map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
log.Printf("Error parsing the response body: %s", err)
} else {
// Print the response status and indexed document version.
log.Printf("{i=%2d} [%s] %s; version=%d", i, res.Status(), r["result"], int(r["_version"].(float64)))
}
}
}(i, line, col_names)
i++
}
wg.Wait()
log.Println(strings.Repeat("-", 37))
}