-
Notifications
You must be signed in to change notification settings - Fork 18
/
elasticsearch_adapter.go
156 lines (136 loc) · 3.41 KB
/
elasticsearch_adapter.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
package Es
import (
"context"
"log"
"os"
"github.com/olivere/elastic/v7"
"github.com/kokizzu/gotro/L"
"github.com/kokizzu/gotro/M"
)
type Adapter struct {
*elastic.Client
Reconnect func() *elastic.Client
}
// example url:
// http://127.0.0.1:9200
func Connect1(url string, debug bool) *elastic.Client {
opts := []elastic.ClientOptionFunc{
elastic.SetURL(url),
elastic.SetSniff(false),
}
if debug {
opts = append(opts,
elastic.SetErrorLog(log.New(os.Stderr, "ES-ERR ", log.LstdFlags)),
elastic.SetInfoLog(log.New(os.Stdout, "ES-INFO ", log.LstdFlags)),
)
}
esClient, err := elastic.NewClient(opts...)
L.PanicIf(err, `elastic.NewClient`)
_, _, err = esClient.Ping(url).Do(context.Background())
L.PanicIf(err, `elastic.Ping`)
return esClient
}
/* example parameter:
{
"query": {
"bool": {
"must": [
{"match": { "cards.token": TOKEN_HERE }}
]
}
}
}
became:
M.SX{
`query`: M.SX{
`bool`: M.SX{
`must`: []any{
M.SX{`match`: M.SX{`cards.token`: Token}},
},
},
},
}
and inside you must deserialize it yourself:
_ = a.QueryRaw(index, query, func(id string, rawJson []byte) (exitEarly bool) {
row := MyStruct{}
err := json.Unmarshal(rawJson, &v)
if L.IsError(err, `json.Unmarshal`) {
return true
}
... // do something with row
return
})
*/
func (a *Adapter) QueryRaw(index string, query M.SX, eachRowFunc func(id string, rawJson []byte) (exitEarly bool)) (res *elastic.SearchResult) {
var err error
res, err = a.Search(index).Source(query).Do(context.Background())
if L.IsError(err, `Es.QueryRaw.Search`) {
return
}
for _, hit := range res.Hits.Hits {
if eachRowFunc(hit.Id, hit.Source) {
return
}
}
return
}
// possible generic 1.18 implementation:
// but go 1.18 method doesn't allow this:
// https://github.com/golang/go/issues/49085
//func (a *Adapter) Query[T any](index string, query M.SX, eachRowFunc func(id string, row *T) (exitEarly bool)) (res *elastic.SearchResult) {
// var err error
// res, err = a.Search(index).Source(query).Do(context.Background())
// if L.IsError(err, `Es.Search`) {
// return
// }
// for _, hit := range res.Hits.Hits {
// var v T
// err := json.Unmarshal(hit.Source, &v)
// if L.IsError(err, `json.Unmarshal`) {
// return
// }
// if eachRowFunc(hit.Id, v) {
// return
// }
// }
// return
//}
// so we might end up with for 1.18:
//func Query[T any](a *Adapter, index string, query M.SX, eachRowFunc func(id string, row *T) (exitEarly bool)) (res *elastic.SearchResult) {
// var err error
// res, err = a.Search(index).Source(query).Do(context.Background())
// if L.IsError(err, `Es.Search`) {
// return
// }
// for _, hit := range res.Hits.Hits {
// var v T
// err := json.Unmarshal(hit.Source, &v)
// if L.IsError(err, `json.Unmarshal`) {
// return
// }
// if eachRowFunc(hit.Id, v) {
// return
// }
// }
// return
//}
// or use template in the struct but not using it as data member like this:
// type Adapter[T any] struct { ... }
//func (a *Adapter[T]) Query(index string, query M.SX, eachRowFunc func(id string, row *T) (exitEarly bool)) (res *elastic.SearchResult) {
// var err error
// res, err = a.Search(index).Source(query).Do(context.Background())
// if L.IsError(err, `Es.Search`) {
// return
// }
// for _, hit := range res.Hits.Hits {
// var v T
// err := json.Unmarshal(hit.Source, &v)
// if L.IsError(err, `json.Unmarshal`) {
// return
// }
// if eachRowFunc(hit.Id, v) {
// return
// }
// }
// return
//}