-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
166 lines (146 loc) · 3.87 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esutil"
"github.com/Jimeux/elastic-go/address"
)
var es *elasticsearch.Client
func init() {
es, _ = elasticsearch.NewClient(elasticsearch.Config{
// https://www.elastic.co/blog/the-go-client-for-elasticsearch-configuration-and-customization
Addresses: []string{"http://localhost:9210"},
EnableDebugLogger: true,
})
/*res, err := es.Info()
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
defer res.Body.Close()
log.Println(res)*/
}
func main() {
ctx := context.Background()
createIndex(ctx)
// deleteIndex(ctx)
// index(ctx)
// update(ctx)
// search(ctx)
}
func createIndex(ctx context.Context) {
// Spec: https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-put-template.html
// Kibana: https://www.elastic.co/guide/en/elasticsearch/reference/master/index-mgmt.html
// create index template
res, err := es.Indices.PutIndexTemplate(
address.TemplateName,
strings.NewReader(address.Template),
es.Indices.PutIndexTemplate.WithContext(ctx),
)
if err != nil {
log.Fatalf("cannot create template: %s", err)
}
if res.IsError() {
log.Fatalf("Cannot create template: %s", res)
}
// create index
res, err = es.Indices.Create(address.IndexName, es.Indices.Create.WithContext(ctx))
if err != nil {
log.Fatalf("cannot create index: %s", err)
}
if res.IsError() {
log.Fatalf("cannot create index: %s", res)
}
// create alias
res, err = es.Indices.PutAlias([]string{address.IndexName}, address.Alias)
if err != nil {
log.Fatalf("cannot create alias: %s", err)
}
if res.IsError() {
log.Fatalf("cannot create alias: %s", err)
}
}
func deleteIndex(ctx context.Context) {
res, err := es.Indices.Delete([]string{address.IndexName}, es.Indices.Delete.WithContext(ctx))
if err != nil {
log.Fatalf("cannot delete index: %s", err)
}
if res.IsError() {
log.Fatalf("cannot delete index: %s", res)
}
res, err = es.Indices.DeleteIndexTemplate(address.TemplateName, es.Indices.DeleteIndexTemplate.WithContext(ctx))
if err != nil {
log.Fatalf("cannot delete template: %s", err)
}
if res.IsError() {
log.Fatalf("cannot delete template: %s", res)
}
}
func index(ctx context.Context) {
// https://www.elastic.co/blog/the-go-client-for-elasticsearch-working-with-data
doc := address.Address{
ID: 1,
Line1: "ユースフル幡ヶ谷",
Line2: "渋谷区",
}
res, err := es.Index(address.Alias, esutil.NewJSONReader(&doc),
es.Index.WithContext(ctx),
es.Index.WithRequireAlias(true),
es.Index.WithDocumentID(strconv.FormatInt(doc.ID, 10)),
)
if err != nil {
log.Fatalf("cannot insert doc: %s", err)
}
if res.IsError() {
log.Fatalf("cannot insert doc: %s", res)
}
}
func update(ctx context.Context) {
// https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-update.html
body := map[string]any{
"doc": map[string]any{
"line_1": "ラ・メイゾン",
},
}
res, err := es.Update(address.Alias, "1", esutil.NewJSONReader(&body),
es.Update.WithContext(ctx),
es.Update.WithRequireAlias(true),
)
if err != nil {
log.Fatalf("cannot insert doc: %s", err)
}
if res.IsError() {
log.Fatalf("cannot insert doc: %s", res)
}
}
func search(ctx context.Context) {
query := map[string]any{
"query": map[string]any{
"match": map[string]any{
"line_2": "渋谷区",
},
},
}
res, err := es.Search(
es.Search.WithContext(ctx),
es.Search.WithIndex(address.Alias),
es.Search.WithBody(esutil.NewJSONReader(&query)),
es.Search.WithTrackTotalHits(true),
es.Search.WithPretty(),
)
if err != nil {
log.Fatalf("cannot search: %s", err)
}
if res.IsError() {
log.Fatalf("cannot search: %s", res)
}
var e map[string]any
if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
log.Fatal(err)
}
fmt.Printf("%+v", e)
}