-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
67 lines (52 loc) · 1.45 KB
/
utils.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
package elastic
import (
"context"
common_utils "github.com/dispenal/go-common/utils"
"github.com/elastic/go-elasticsearch/v8"
"github.com/pkg/errors"
)
func MappingIndex(ctx context.Context, esClient *elasticsearch.Client, indexName string, mapping []byte) error {
exists, err := isIndexExists(ctx, esClient, indexName)
if err != nil {
common_utils.LogError("error while checking index exists")
return errors.Wrap(err, "error while checking index exists")
}
if exists {
return nil
}
response, err := CreateIndex(ctx, esClient, indexName, mapping)
if err != nil {
common_utils.LogError("error while creating index")
return errors.Wrap(err, "error while creating index")
}
defer response.Body.Close()
if response.IsError() {
return errors.New(response.String())
}
return nil
}
func isIndexExists(ctx context.Context, esClient *elasticsearch.Client, indexName string) (bool, error) {
response, err := Exists(ctx, esClient, []string{indexName})
if err != nil {
common_utils.LogError("error while checking index exists")
return false, errors.Wrap(err, "esclient.Exists")
}
defer response.Body.Close()
if response.IsError() && response.StatusCode == 404 {
common_utils.LogInfo("index not exists")
return false, nil
}
return true, nil
}
func IntPointer(v int) *int {
return &v
}
func Int32Pointer(v int32) *int32 {
return &v
}
func Int64Pointer(v int64) *int64 {
return &v
}
func StringPointer(v string) *string {
return &v
}