-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathelastic.go
162 lines (138 loc) · 4.04 KB
/
elastic.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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
"io"
"log"
"strings"
)
var ES *elasticsearch.Client
const IndexName = "floors"
func InitSearch() {
var r map[string]interface{}
var err error
ES, err = elasticsearch.NewClient(elasticsearch.Config{
Addresses: []string{Config.ElasticsearchUrl},
})
if err != nil {
log.Fatalf("Error creating the client: %s", err)
}
res, err := ES.Info()
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(res.Body)
if res.IsError() {
log.Fatalf("Error: %s", res.String())
}
if err = json.NewDecoder(res.Body).Decode(&r); err != nil {
log.Fatalf("Error parsing the response body: %s", err.Error())
}
// print Client and Server Info
log.Printf("Client: %s\n", elasticsearch.Version)
log.Printf("Server: %s", r["version"].(map[string]interface{})["number"])
log.Println(strings.Repeat("~", 37))
CheckIndex()
DeleteAllDocuments()
}
func CheckIndex() {
rsp, err := ES.Indices.Get([]string{IndexName})
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
_ = rsp.Body.Close()
if rsp.StatusCode == 404 {
indexMapping := Map{
"mappings": Map{
"properties": Map{
"content": Map{
"type": "text",
// https://www.elastic.co/guide/en/elasticsearch/reference/current/analyzer.html
"analyzer": "ik_max_word",
// https://www.elastic.co/guide/en/elasticsearch/reference/current/search-analyzer.html
"search_analyzer": "ik_smart",
// https://www.elastic.co/guide/en/elasticsearch/reference/current/similarity.html
"similarity": "boolean",
},
"id": Map{
// https://www.elastic.co/guide/en/elasticsearch/reference/current/number.html
"type": "long",
},
"updated_at": Map{
// https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html
"type": "date",
},
},
},
}
buffer := bytes.NewBuffer(make([]byte, 0, 1024))
err = json.NewEncoder(buffer).Encode(indexMapping)
if err != nil {
log.Fatal(err)
}
req := esapi.IndicesCreateRequest{
Index: IndexName,
Body: buffer,
}
rsp, err = req.Do(context.Background(), ES)
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
if rsp.IsError() {
log.Fatalf("Error: %s", rsp.String())
}
}
}
func DeleteAllDocuments() {
req := esapi.DeleteByQueryRequest{
Index: []string{IndexName},
Body: strings.NewReader(`{"query": {"match_all": {}}}`),
}
rsp, err := req.Do(context.Background(), ES)
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
_ = rsp.Body.Close()
if rsp.IsError() {
log.Fatalf("Error: %s", rsp.String())
}
}
var BulkBuffer *bytes.Buffer
// BulkInsert run in single goroutine only, used when dump floors
// see https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-bulk.html
func BulkInsert(floors Floors) error {
if BulkBuffer == nil {
BulkBuffer = bytes.NewBuffer(make([]byte, 0, 1024000)) // 100 KB buffer
}
if len(floors) == 0 {
return nil
}
firstFloorID := floors[0].ID
lastFloorID := floors[len(floors)-1].ID
for _, floor := range floors {
// meta: use index, it will insert or replace a document
BulkBuffer.WriteString(fmt.Sprintf(`{ "index" : { "_id" : "%d" } }%s`, floor.ID, "\n"))
// data: should not contain \n, because \n is the delimiter of one action
data, err := json.Marshal(floor)
if err != nil {
return fmt.Errorf("error failed to marshal floor: %s", err)
}
BulkBuffer.Write(data)
BulkBuffer.WriteByte('\n') // the final line of data must end with a newline character \n
}
log.Printf("Preparing insert floor [%d, %d]\n", firstFloorID, lastFloorID)
res, err := ES.Bulk(BulkBuffer, ES.Bulk.WithIndex(IndexName))
if err != nil || res.IsError() {
return fmt.Errorf("error indexing floor [%d, %d]: %s", firstFloorID, lastFloorID, err)
}
_ = res.Body.Close()
log.Printf("index floor [%d, %d] success\n", firstFloorID, lastFloorID)
BulkBuffer.Reset()
return nil
}