-
Notifications
You must be signed in to change notification settings - Fork 0
/
v7.go
614 lines (545 loc) · 14.2 KB
/
v7.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
package elasticsearch
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"github.com/olivere/elastic/v7"
"github.com/team-ide/go-tool/util"
"go.uber.org/zap"
"io/ioutil"
"net/http"
"sort"
"strings"
"sync"
)
// V7Service 注册处理器在线信息等
type V7Service struct {
*Config
client *elastic.Client
clientLock sync.Mutex
}
func (this_ *V7Service) init() error {
var err error
return err
}
func (this_ *V7Service) GetClient() (client *elastic.Client, err error) {
if this_ == nil {
err = errors.New("elasticsearch service is null")
return
}
this_.clientLock.Lock()
defer this_.clientLock.Unlock()
if this_.client != nil && this_.client.IsRunning() {
client = this_.client
return
}
util.Logger.Info("es client is null or not running,now to create client", zap.Any("url", this_.Url))
var urls []string
if strings.Contains(this_.Url, ",") {
urls = strings.Split(this_.Url, ",")
} else if strings.Contains(this_.Url, ";") {
urls = strings.Split(this_.Url, ";")
} else {
urls = []string{this_.Url}
}
var isHttps bool
for _, one := range urls {
if strings.HasPrefix(one, "https") {
isHttps = true
}
}
var options []elastic.ClientOptionFunc
options = append(options, elastic.SetURL(urls...))
options = append(options, elastic.SetSniff(false))
if isHttps {
httpClient := &http.Client{}
TLSClientConfig := &tls.Config{
InsecureSkipVerify: true,
}
if this_.CertPath != "" {
certPool := x509.NewCertPool()
var pemCerts []byte
pemCerts, err = ioutil.ReadFile(this_.CertPath)
if err != nil {
return
}
if !certPool.AppendCertsFromPEM(pemCerts) {
err = errors.New("证书[" + this_.CertPath + "]解析失败")
return
}
TLSClientConfig.RootCAs = certPool
//TLSClientConfig.Certificates = []tls.Certificate{clicrt}
}
httpClient.Transport = &http.Transport{
TLSClientConfig: TLSClientConfig,
}
options = append(options, elastic.SetHttpClient(httpClient))
}
if this_.Username != "" {
options = append(options, elastic.SetBasicAuth(this_.Username, this_.Password))
}
client, err = elastic.NewClient(options...)
if err != nil {
if client != nil {
client.Stop()
}
return
}
this_.client = client
return
}
func (this_ *V7Service) Close() {
if this_ != nil && this_.client != nil {
this_.client.Stop()
this_.client = nil
}
}
func (this_ *V7Service) Info() (res *elastic.NodesInfoResponse, err error) {
client, err := this_.GetClient()
if err != nil {
return
}
//defer client.Stop()
res, err = client.NodesInfo().Do(context.Background())
if err != nil {
return
}
return
}
func (this_ *V7Service) DeleteIndex(indexName string) (err error) {
client, err := this_.GetClient()
if err != nil {
return
}
//defer client.Stop()
_, err = client.DeleteIndex(indexName).Do(context.Background())
if err != nil {
return
}
return
}
func (this_ *V7Service) CreateIndex(indexName string, bodyJSON map[string]interface{}) (err error) {
client, err := this_.GetClient()
if err != nil {
return
}
//defer client.Stop()
_, err = client.CreateIndex(indexName).BodyJson(bodyJSON).Do(context.Background())
if err != nil {
return
}
return
}
type IndexInfo struct {
IndexName string `json:"indexName"`
}
func (this_ *V7Service) Indexes() (indexes []*IndexInfo, err error) {
client, err := this_.GetClient()
if err != nil {
return
}
//defer client.Stop()
indexNames, err := client.IndexNames()
if err != nil {
return
}
sort.Slice(indexNames, func(i, j int) bool {
return strings.ToLower(indexNames[i]) < strings.ToLower(indexNames[j]) //升序 即前面的值比后面的小 忽略大小写排序
})
for _, indexName := range indexNames {
info := &IndexInfo{
IndexName: indexName,
}
indexes = append(indexes, info)
}
return
}
func (this_ *V7Service) GetMapping(indexName string) (res interface{}, err error) {
client, err := this_.GetClient()
if err != nil {
return
}
//defer client.Stop()
mappingMap, err := client.GetMapping().Index(indexName).Do(context.Background())
if err != nil {
return
}
for key, value := range mappingMap {
if key == indexName {
res = value
}
}
return
}
func (this_ *V7Service) PutMapping(indexName string, bodyJSON map[string]interface{}) (err error) {
client, err := this_.GetClient()
if err != nil {
return
}
//defer client.Stop()
_, err = client.PutMapping().Index(indexName).BodyJson(bodyJSON).Do(context.Background())
if err != nil {
return
}
return
}
func (this_ *V7Service) SetFieldType(indexName string, fieldName string, fieldType string) (err error) {
bodyJSON := map[string]interface{}{}
bodyJSON["properties"] = map[string]interface{}{
fieldName: map[string]interface{}{
"type": fieldType,
},
}
err = this_.PutMapping(indexName, bodyJSON)
if err != nil {
return
}
return
}
type SearchResult struct {
TotalHits *elastic.TotalHits `json:"total,omitempty"` // total number of hits found
MaxScore *float64 `json:"max_score,omitempty"` // maximum score of all hits
Hits []*HitData `json:"hits,omitempty"` // the actual hits returned
}
type HitData struct {
Index string `json:"_index,omitempty"` // index name
Type string `json:"_type,omitempty"` // type meta field
Id string `json:"_id,omitempty"` // external or internal
Uid string `json:"_uid,omitempty"` // uid meta field (see MapperService.java for all meta fields)
Version *int64 `json:"_version,omitempty"` // version number, when Version is set to true in SearchService
Source string `json:"_source,omitempty"` // stored document source
}
type Where struct {
Name string `json:"name"`
Value string `json:"value"`
Before string `json:"before"`
After string `json:"after"`
CustomSql string `json:"customSql"`
SqlConditionalOperation string `json:"sqlConditionalOperation"`
AndOr string `json:"andOr"`
}
type Order struct {
Name string `json:"name"`
AscDesc string `json:"ascDesc"`
}
func (this_ *V7Service) Search(indexName string, pageIndex int, pageSize int, whereList []*Where, orderList []*Order) (res *SearchResult, err error) {
client, err := this_.GetClient()
if err != nil {
return
}
//defer client.Stop()
doer := client.Search(indexName)
var query = elastic.NewBoolQuery()
for _, where := range whereList {
var q elastic.Query
var isNot = false
switch where.SqlConditionalOperation {
case "like":
q = elastic.NewWildcardQuery(where.Name, "*"+where.Value+"*")
case "not like":
q = elastic.NewWildcardQuery(where.Name, "*"+where.Value+"*")
isNot = true
case "like start":
q = elastic.NewWildcardQuery(where.Name, where.Value+"*")
case "not like start":
q = elastic.NewWildcardQuery(where.Name, where.Value+"*")
isNot = true
case "like end":
q = elastic.NewWildcardQuery(where.Name, "*"+where.Value)
case "not like end":
q = elastic.NewWildcardQuery(where.Name, "*"+where.Value)
isNot = true
case "between":
q = elastic.NewRangeQuery(where.Name).Gte(where.Before).Lte(where.After)
case "not between":
q = elastic.NewRangeQuery(where.Name).Gte(where.Before).Lte(where.After)
isNot = true
case "in":
q = elastic.NewTermsQuery(where.Name, strings.Split(where.Value, ","))
case "not in":
q = elastic.NewTermsQuery(where.Name, strings.Split(where.Value, ","))
isNot = true
default:
q = elastic.NewTermQuery(where.Name, where.Value)
}
var addQ elastic.Query
if strings.Contains(where.Name, ".") {
addQ = elastic.NewNestedQuery(where.Name[0:strings.LastIndex(where.Name, ".")], q)
} else {
addQ = q
}
if isNot {
query.MustNot(addQ)
} else {
query.Must(addQ)
}
}
doer.Query(query)
for _, one := range orderList {
switch one.AscDesc {
case "ASC":
doer.Sort(one.Name, true)
break
default:
doer.Sort(one.Name, false)
break
}
}
//ss, _ := query.Source()
//util.Logger.Info("es search", zap.Any("query", ss))
doer.TrackTotalHits(true)
searchResult, err := doer.Size(pageSize).From((pageIndex - 1) * pageSize).Do(context.Background())
if err != nil {
return
}
res = &SearchResult{}
if searchResult.Hits != nil {
res.TotalHits = searchResult.Hits.TotalHits
res.MaxScore = searchResult.Hits.MaxScore
for _, one := range searchResult.Hits.Hits {
data := &HitData{
Id: one.Id,
Type: one.Type,
Index: one.Index,
Uid: one.Uid,
Version: one.Version,
}
if one.Source != nil {
bs, _ := json.Marshal(one.Source)
if bs != nil {
data.Source = string(bs)
}
}
res.Hits = append(res.Hits, data)
}
}
return
}
type InsertResponse struct {
*elastic.IndexResponse
}
func (this_ *V7Service) Insert(indexName string, id string, doc interface{}) (res *InsertResponse, err error) {
client, err := this_.GetClient()
if err != nil {
return
}
//defer client.Stop()
doer := client.Index()
indexResponse, err := doer.Index(indexName).Id(id).BodyJson(doc).Refresh("wait_for").Do(context.Background())
if err != nil {
return
}
res = &InsertResponse{
IndexResponse: indexResponse,
}
return
}
func (this_ *V7Service) InsertNotWait(indexName string, id string, doc interface{}) (res *InsertResponse, err error) {
client, err := this_.GetClient()
if err != nil {
return
}
//defer client.Stop()
doer := client.Index()
indexResponse, err := doer.Index(indexName).Id(id).BodyJson(doc).Do(context.Background())
if err != nil {
return
}
res = &InsertResponse{
IndexResponse: indexResponse,
}
return
}
type InsertDoc struct {
IndexName string
Id string
Doc interface{}
}
type BulkResponse struct {
*elastic.BulkResponse
}
func (this_ *V7Service) BatchInsertNotWait(docs []*InsertDoc) (res *BulkResponse, err error) {
if len(docs) == 0 {
return
}
client, err := this_.GetClient()
if err != nil {
return
}
//defer client.Stop()
bulk := client.Bulk()
for _, doc := range docs {
bulk.Add(elastic.NewBulkIndexRequest().Index(doc.IndexName).Id(doc.Id).Doc(doc.Doc))
}
bulkResponse, err := bulk.Do(context.Background())
if err != nil {
return
}
res = &BulkResponse{
BulkResponse: bulkResponse,
}
return
}
type UpdateResponse struct {
*elastic.UpdateResponse
}
func (this_ *V7Service) Update(indexName string, id string, doc interface{}) (res *UpdateResponse, err error) {
client, err := this_.GetClient()
if err != nil {
return
}
//defer client.Stop()
doer := client.Update()
updateResponse, err := doer.Index(indexName).Id(id).Doc(doc).Refresh("wait_for").Do(context.Background())
if err != nil {
return
}
res = &UpdateResponse{
UpdateResponse: updateResponse,
}
return
}
func (this_ *V7Service) UpdateNotWait(indexName string, id string, doc interface{}) (res *UpdateResponse, err error) {
client, err := this_.GetClient()
if err != nil {
return
}
//defer client.Stop()
doer := client.Update()
updateResponse, err := doer.Index(indexName).Id(id).Doc(doc).Do(context.Background())
if err != nil {
return
}
res = &UpdateResponse{
UpdateResponse: updateResponse,
}
return
}
type DeleteResponse struct {
*elastic.DeleteResponse
}
func (this_ *V7Service) Delete(indexName string, id string) (res *DeleteResponse, err error) {
client, err := this_.GetClient()
if err != nil {
return
}
//defer client.Stop()
doer := client.Delete()
deleteResponse, err := doer.Index(indexName).Id(id).Refresh("wait_for").Do(context.Background())
if err != nil {
return
}
res = &DeleteResponse{
DeleteResponse: deleteResponse,
}
return
}
func (this_ *V7Service) DeleteNotWait(indexName string, id string) (res *DeleteResponse, err error) {
client, err := this_.GetClient()
if err != nil {
return
}
//defer client.Stop()
doer := client.Delete()
deleteResponse, err := doer.Index(indexName).Id(id).Do(context.Background())
if err != nil {
return
}
res = &DeleteResponse{
DeleteResponse: deleteResponse,
}
return
}
type BulkIndexByScrollResponse struct {
*elastic.BulkIndexByScrollResponse
}
func (this_ *V7Service) Reindex(sourceIndexName string, toIndexName string) (res *BulkIndexByScrollResponse, err error) {
client, err := this_.GetClient()
if err != nil {
return
}
//defer client.Stop()
doer := client.Reindex()
bulkIndexByScrollResponse, err := doer.Source(elastic.NewReindexSource().Index(sourceIndexName)).DestinationIndex(toIndexName).Refresh("true").Do(context.Background())
if err != nil {
return
}
res = &BulkIndexByScrollResponse{
BulkIndexByScrollResponse: bulkIndexByScrollResponse,
}
return
}
type IndicesStatsResponse struct {
*elastic.IndicesStatsResponse
}
func (this_ *V7Service) IndexStat(indexName string) (res *IndicesStatsResponse, err error) {
client, err := this_.GetClient()
if err != nil {
return
}
//defer client.Stop()
doer := client.IndexStats()
response, err := doer.Index(indexName).Do(context.Background())
if err != nil {
return
}
res = &IndicesStatsResponse{
IndicesStatsResponse: response,
}
return
}
func (this_ *V7Service) Scroll(indexName string, scrollId string, pageSize int, whereList []*Where, orderList []*Order) (res *SearchResult, err error) {
client, err := this_.GetClient()
if err != nil {
return
}
//defer client.Stop()
doer := client.Scroll(indexName)
query := elastic.NewBoolQuery()
searchResult, err := doer.Query(query).Size(pageSize).ScrollId(scrollId).Do(context.Background())
if err != nil {
return
}
if searchResult.Hits != nil {
res.TotalHits = searchResult.Hits.TotalHits
res.MaxScore = searchResult.Hits.MaxScore
for _, one := range searchResult.Hits.Hits {
data := &HitData{
Id: one.Id,
Type: one.Type,
Index: one.Index,
Uid: one.Uid,
Version: one.Version,
}
if one.Source != nil {
bs, _ := json.Marshal(one.Source)
if bs != nil {
data.Source = string(bs)
}
}
res.Hits = append(res.Hits, data)
}
}
return
}
type IndexAliasResponse struct {
*elastic.AliasResult
}
func (this_ *V7Service) IndexAlias(indexName string, aliasName string) (res *IndexAliasResponse, err error) {
client, err := this_.GetClient()
if err != nil {
return
}
//defer client.Stop()
doer := client.Alias()
aliasResult, err := doer.Add(indexName, aliasName).Do(context.Background())
if err != nil {
return
}
res = &IndexAliasResponse{
AliasResult: aliasResult,
}
return
}