-
Notifications
You must be signed in to change notification settings - Fork 26
/
shared.go
57 lines (49 loc) · 1.42 KB
/
shared.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
package util
import (
"context"
"math"
es7 "github.com/olivere/elastic/v7"
es6 "gopkg.in/olivere/elastic.v6"
)
// GetIndexFilterQueryEs6 apply the index filtering logic
func GetIndexFilterQueryEs6(query *es6.BoolQuery, indices ...string) *es6.BoolQuery {
if indices != nil && len(indices) > 0 {
var indexQueries []es6.Query
for _, index := range indices {
query := es6.NewTermQuery("indices.keyword", index)
indexQueries = append(indexQueries, query)
}
query = query.Must(es6.NewBoolQuery().Should(indexQueries...))
}
return query
}
// GetIndexFilterQueryEs7 apply the index filtering logic
func GetIndexFilterQueryEs7(query *es7.BoolQuery, indices ...string) *es7.BoolQuery {
if indices != nil && len(indices) > 0 {
var indexQueries []es7.Query
for _, index := range indices {
query := es7.NewTermQuery("indices.keyword", index)
indexQueries = append(indexQueries, query)
}
query = query.Must(es7.NewBoolQuery().Should(indexQueries...))
}
return query
}
// GetTotalNodes retrieves the number of es nodes
func GetTotalNodes() (int, error) {
response, err := GetClient7().NodesInfo().
Metric("nodes").
Do(context.Background())
if err != nil {
return -1, err
}
return len(response.Nodes), nil
}
// GetReplicas calculates the number of replicas to set
func GetReplicas() int {
nodes, err := GetTotalNodes()
if err != nil {
return int(0)
}
return int(math.Min(float64(1), float64(nodes-1)))
}