forked from infinilabs/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
319 lines (267 loc) · 8.02 KB
/
index.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
package elastic
import (
"github.com/huminghe/framework/core/util"
"strings"
)
type Indexes map[string]interface{}
type ScrollResponseAPI interface {
GetScrollId() string
SetScrollId(id string)
GetHitsTotal() int
GetShardResponse() ShardResponse
GetDocs() []interface{}
}
type ShardResponse struct {
Total int `json:"total,omitempty"`
Successful int `json:"successful,omitempty"`
Skipped int `json:"skipped,omitempty"`
Failed int `json:"failed,omitempty"`
Failures []struct {
Shard int `json:"shard,omitempty"`
Index string `json:"index,omitempty"`
Status int `json:"status,omitempty"`
Reason interface{} `json:"reason,omitempty"`
} `json:"failures,omitempty"`
}
type ScrollResponse struct {
Took int `json:"took,omitempty"`
ScrollId string `json:"_scroll_id,omitempty"`
TimedOut bool `json:"timed_out,omitempty"`
Hits struct {
MaxScore float32 `json:"max_score,omitempty"`
Total int `json:"total,omitempty"`
Docs []interface{} `json:"hits,omitempty"`
} `json:"hits"`
Shards ShardResponse `json:"_shards,omitempty"`
}
type ScrollResponseV7 struct {
ScrollResponse
Hits struct {
MaxScore float32 `json:"max_score,omitempty"`
Total struct {
Value int `json:"value,omitempty"`
Relation string `json:"relation,omitempty"`
} `json:"total,omitempty"`
Docs []interface{} `json:"hits,omitempty"`
} `json:"hits"`
}
func (scroll *ScrollResponse) GetHitsTotal() int {
return scroll.Hits.Total
}
func (scroll *ScrollResponse) GetScrollId() string {
return scroll.ScrollId
}
func (scroll *ScrollResponse) SetScrollId(id string) {
scroll.ScrollId = id
}
func (scroll *ScrollResponse) GetDocs() []interface{} {
return scroll.Hits.Docs
}
func (scroll *ScrollResponse) GetShardResponse() ShardResponse {
return scroll.Shards
}
func (scroll *ScrollResponseV7) GetHitsTotal() int {
return scroll.Hits.Total.Value
}
func (scroll *ScrollResponseV7) GetScrollId() string {
return scroll.ScrollId
}
func (scroll *ScrollResponseV7) SetScrollId(id string) {
scroll.ScrollId = id
}
func (scroll *ScrollResponseV7) GetDocs() []interface{} {
return scroll.Hits.Docs
}
func (scroll *ScrollResponseV7) GetShardResponse() ShardResponse {
return scroll.Shards
}
type ClusterVersion struct {
Name string `json:"name"`
ClusterName string `json:"cluster_name"`
Version struct {
Number string `json:"number"`
LuceneVersion string `json:"lucene_version"`
} `json:"version"`
}
func (c *ClusterVersion) GetMajorVersion() int {
vs := strings.Split(c.Version.Number, ".")
n, err := util.ToInt(vs[0])
if err != nil {
panic(err)
}
return n
}
type ClusterHealth struct {
Name string `json:"cluster_name"`
Status string `json:"status"`
}
// IndexDocument used to construct indexing document
type IndexDocument struct {
Index string `json:"_index,omitempty"`
Type string `json:"_type,omitempty"`
ID interface{} `json:"_id,omitempty"`
Routing string `json:"_routing,omitempty"`
Source map[string]interface{} `json:"_source,omitempty"`
Highlight map[string][]interface{} `json:"highlight,omitempty"`
}
type Bucket struct {
Key string `json:"key,omitempty"`
DocCount int `json:"doc_count,omitempty"`
}
type AggregationResponse struct {
Buckets []Bucket `json:"buckets,omitempty"`
}
// InsertResponse is a index response object
type InsertResponse struct {
Result string `json:"result"`
Index string `json:"_index"`
Type string `json:"_type"`
ID string `json:"_id"`
Version int `json:"_version"`
}
// GetResponse is a get response object
type GetResponse struct {
Found bool `json:"found"`
Index string `json:"_index"`
Type string `json:"_type"`
ID string `json:"_id"`
Version int `json:"_version"`
Source map[string]interface{} `json:"_source"`
}
// DeleteResponse is a delete response object
type DeleteResponse struct {
Result string `json:"result"`
Index string `json:"_index"`
Type string `json:"_type"`
ID string `json:"_id"`
Version int `json:"_version"`
}
// CountResponse is a count response object
type CountResponse struct {
Count int `json:"count"`
}
// SearchResponse is a count response object
type SearchResponse struct {
Took int `json:"took"`
TimedOut bool `json:"timed_out"`
Hits struct {
Total interface{} `json:"total"`
MaxScore float32 `json:"max_score"`
Hits []IndexDocument `json:"hits,omitempty"`
} `json:"hits"`
Aggregations map[string]AggregationResponse `json:"aggregations,omitempty"`
}
func (response *SearchResponse) GetTotal() int {
if response.Hits.Total != nil {
if util.TypeIsMap(response.Hits.Total) {
v := response.Hits.Total.(map[string]interface{})
return util.GetIntValue(v["value"])
} else {
return util.GetIntValue(response.Hits.Total)
}
}
return -1
}
// RangeQuery is used to find value in range
type RangeQuery struct {
Range map[string]map[string]interface{} `json:"range,omitempty"`
}
func (query *RangeQuery) Gt(field string, value interface{}) {
query.Range = map[string]map[string]interface{}{}
v := map[string]interface{}{}
v["gt"] = value
query.Range[field] = v
}
func (query *RangeQuery) Gte(field string, value interface{}) {
query.Range = map[string]map[string]interface{}{}
v := map[string]interface{}{}
v["gte"] = value
query.Range[field] = v
}
func (query *RangeQuery) Lt(field string, value interface{}) {
query.Range = map[string]map[string]interface{}{}
v := map[string]interface{}{}
v["lt"] = value
query.Range[field] = v
}
func (query *RangeQuery) Lte(field string, value interface{}) {
query.Range = map[string]map[string]interface{}{}
v := map[string]interface{}{}
v["lte"] = value
query.Range[field] = v
}
type MatchQuery struct {
Match map[string]interface{} `json:"match,omitempty"`
}
type QueryStringQuery struct {
Query map[string]interface{} `json:"query_string,omitempty"`
}
func NewQueryString(q string) *QueryStringQuery {
query := QueryStringQuery{}
query.Query = map[string]interface{}{}
return &query
}
type TermsAggregationQuery struct {
term string
size int
}
func (query *TermsAggregationQuery) Field(field string) *TermsAggregationQuery {
query.term = field
return query
}
func (query *TermsAggregationQuery) Size(size int) *TermsAggregationQuery {
query.size = size
return query
}
func NewTermsAggregation() (query *TermsAggregationQuery) {
return &TermsAggregationQuery{}
}
func (query *QueryStringQuery) QueryString(q string) {
query.Query["query"] = q
}
func (query *QueryStringQuery) DefaultOperator(op string) {
query.Query["default_operator"] = op
}
func (query *QueryStringQuery) Fields(fields ...string) {
query.Query["default_operator"] = fields
}
// Init match query's condition
func (match *MatchQuery) Set(field string, v interface{}) {
match.Match = map[string]interface{}{}
match.Match[field] = v
}
// BoolQuery wrapper queries
type BoolQuery struct {
Must []interface{} `json:"must,omitempty"`
MustNot []interface{} `json:"must_not,omitempty"`
Should []interface{} `json:"should,omitempty"`
}
// Query is the root query object
type Query struct {
BoolQuery *BoolQuery `json:"bool"`
}
// SearchRequest is the root search query object
type SearchRequest struct {
Query *Query `json:"query,omitempty"`
From int `json:"from"`
Size int `json:"size"`
Sort *[]interface{} `json:"sort,omitempty"`
AggregationRequest `json:"aggs,omitempty"`
}
type AggregationRequest struct {
Aggregations map[string]Aggregation `json:"aggregations,omitempty"`
}
type Aggregation struct {
}
// AddSort add sort conditions to SearchRequest
func (request *SearchRequest) AddSort(field string, order string) {
if (request.Sort) == nil {
s := []interface{}{}
request.Sort = &s
}
s := map[string]interface{}{}
v := map[string]interface{}{}
v["order"] = order
s[field] = v
*request.Sort = append(*request.Sort, s)
}