-
Notifications
You must be signed in to change notification settings - Fork 0
/
elasticsearch.go
170 lines (161 loc) · 4.82 KB
/
elasticsearch.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
package elasticsearch
import (
"context"
"encoding/json"
"errors"
"reflect"
"strings"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
"github.com/elastic/go-elasticsearch/v8/esutil"
)
func FindIdField(modelType reflect.Type) (int, string, string) {
return findBsonField(modelType, "_id")
}
func findBsonField(modelType reflect.Type, bsonName string) (int, string, string) {
numField := modelType.NumField()
for i := 0; i < numField; i++ {
field := modelType.Field(i)
bsonTag := field.Tag.Get("bson")
tags := strings.Split(bsonTag, ",")
json := field.Name
if tag1, ok1 := field.Tag.Lookup("json"); ok1 {
json = strings.Split(tag1, ",")[0]
}
for _, tag := range tags {
if strings.TrimSpace(tag) == bsonName {
return i, field.Name, json
}
}
}
return -1, "", ""
}
func FindFieldByName(modelType reflect.Type, fieldName string) (index int, jsonTagName string) {
numField := modelType.NumField()
for index := 0; index < numField; index++ {
field := modelType.Field(index)
if field.Name == fieldName {
jsonTagName := fieldName
if jsonTag, ok := field.Tag.Lookup("json"); ok {
jsonTagName = strings.Split(jsonTag, ",")[0]
}
return index, jsonTagName
}
}
return -1, fieldName
}
func FindFieldByJson(modelType reflect.Type, jsonTagName string) (index int, fieldName string) {
numField := modelType.NumField()
for i := 0; i < numField; i++ {
field := modelType.Field(i)
tag1, ok1 := field.Tag.Lookup("json")
if ok1 && strings.Split(tag1, ",")[0] == jsonTagName {
return i, field.Name
}
}
return -1, jsonTagName
}
func FindFieldByIndex(modelType reflect.Type, fieldIndex int) (fieldName, jsonTagName string) {
if fieldIndex < modelType.NumField() {
field := modelType.Field(fieldIndex)
jsonTagName := ""
if jsonTag, ok := field.Tag.Lookup("json"); ok {
jsonTagName = strings.Split(jsonTag, ",")[0]
}
return field.Name, jsonTagName
}
return "", ""
}
func Exist(ctx context.Context, es *elasticsearch.Client, index string, documentID string) (bool, error) {
req := esapi.ExistsRequest{
Index: index,
DocumentID: documentID,
}
res, err := req.Do(ctx, es)
if err != nil {
return false, err
}
defer res.Body.Close()
if res.IsError() {
return false, errors.New("response error")
} else {
var r map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
return false, err
} else {
return r["found"].(bool), nil
}
}
}
func FindOne(ctx context.Context, client *elasticsearch.Client, index string, documentID string, result interface{}, idJson string) (bool, error) {
return FindOneWithVersion(ctx, client, index, documentID, result, idJson, "")
}
func FindOneWithVersion(ctx context.Context, client *elasticsearch.Client, index string, documentID string, result interface{}, idJson string, versionJson string) (bool, error) {
req := esapi.GetRequest{
Index: index,
DocumentID: documentID,
}
res, err := req.Do(ctx, client)
if err != nil {
return false, err
}
defer res.Body.Close()
if !res.IsError() {
var r map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&r); err == nil {
hit := r["_source"].(map[string]interface{})
if len(idJson) > 0 {
hit[idJson] = r["_id"]
}
if len(versionJson) > 0 {
hit[versionJson] = r["_version"]
}
if err := json.NewDecoder(esutil.NewJSONReader(hit)).Decode(&result); err != nil {
return false, err
}
return true, nil
}
return false, err
}
return false, errors.New("response error")
}
func Find(ctx context.Context, client *elasticsearch.Client, index []string, query map[string]interface{}, result interface{}, idJson string) error {
return FindWithVersion(ctx, client, index, query, result, idJson, "")
}
func FindWithVersion(ctx context.Context, client *elasticsearch.Client, index []string, query map[string]interface{}, result interface{}, idJson string, versionJson string) error {
req := esapi.SearchRequest{
Index: index,
Body: esutil.NewJSONReader(query),
TrackTotalHits: true,
Pretty: true,
}
res, err := req.Do(ctx, client)
if err != nil {
return err
}
defer res.Body.Close()
if res.IsError() {
return errors.New("response error")
} else {
var r map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
return err
} else {
hits := r["hits"].(map[string]interface{})["hits"].([]interface{})
listResults := make([]interface{}, 0)
for _, hitObj := range hits {
hit := hitObj.(map[string]interface{})
r := hit["_source"]
rs := r.(map[string]interface{})
if len(idJson) > 0 {
rs[idJson] = hit["_id"]
}
if len(versionJson) > 0 {
hit[versionJson] = hit["_version"]
}
listResults = append(listResults, r)
}
return json.NewDecoder(esutil.NewJSONReader(listResults)).Decode(result)
}
}
}