forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.go
266 lines (222 loc) · 5.69 KB
/
processor.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
package template
import (
"errors"
"github.com/elastic/beats/libbeat/common"
)
type Processor struct {
EsVersion common.Version
}
var (
defaultScalingFactor = 1000
)
// This includes all entries without special handling for different versions.
// Currently this is:
// long, geo_point, date, short, byte, float, double, boolean
func (p *Processor) process(fields common.Fields, path string, output common.MapStr) error {
for _, field := range fields {
if field.Name == "" {
continue
}
field.Path = path
var mapping common.MapStr
switch field.Type {
case "ip":
mapping = p.ip(&field)
case "scaled_float":
mapping = p.scaledFloat(&field)
case "half_float":
mapping = p.halfFloat(&field)
case "integer":
mapping = p.integer(&field)
case "text":
mapping = p.text(&field)
case "", "keyword":
mapping = p.keyword(&field)
case "object":
mapping = p.object(&field)
case "array":
mapping = p.array(&field)
case "group":
var newPath string
if path == "" {
newPath = field.Name
} else {
newPath = path + "." + field.Name
}
mapping = common.MapStr{}
if field.Dynamic.Value != nil {
mapping["dynamic"] = field.Dynamic.Value
}
// Combine properties with previous field definitions (if any)
properties := common.MapStr{}
key := common.GenerateKey(field.Name) + ".properties"
currentProperties, err := output.GetValue(key)
if err == nil {
var ok bool
properties, ok = currentProperties.(common.MapStr)
if !ok {
// This should never happen
return errors.New(key + " is expected to be a MapStr")
}
}
if err := p.process(field.Fields, newPath, properties); err != nil {
return err
}
mapping["properties"] = properties
default:
mapping = p.other(&field)
}
if len(mapping) > 0 {
output.Put(common.GenerateKey(field.Name), mapping)
}
}
return nil
}
func (p *Processor) other(f *common.Field) common.MapStr {
property := getDefaultProperties(f)
if f.Type != "" {
property["type"] = f.Type
}
return property
}
func (p *Processor) integer(f *common.Field) common.MapStr {
property := getDefaultProperties(f)
property["type"] = "long"
return property
}
func (p *Processor) scaledFloat(f *common.Field) common.MapStr {
property := getDefaultProperties(f)
property["type"] = "scaled_float"
if p.EsVersion.IsMajor(2) {
property["type"] = "float"
} else {
scalingFactor := f.ScalingFactor
// Set default scaling factor
if scalingFactor == 0 {
scalingFactor = defaultScalingFactor
}
property["scaling_factor"] = scalingFactor
}
return property
}
func (p *Processor) halfFloat(f *common.Field) common.MapStr {
property := getDefaultProperties(f)
property["type"] = "half_float"
if p.EsVersion.IsMajor(2) {
property["type"] = "float"
}
return property
}
func (p *Processor) ip(f *common.Field) common.MapStr {
property := getDefaultProperties(f)
property["type"] = "ip"
if p.EsVersion.IsMajor(2) {
property["type"] = "string"
property["ignore_above"] = 1024
property["index"] = "not_analyzed"
}
return property
}
func (p *Processor) keyword(f *common.Field) common.MapStr {
property := getDefaultProperties(f)
property["type"] = "keyword"
property["ignore_above"] = 1024
if p.EsVersion.IsMajor(2) {
property["type"] = "string"
property["ignore_above"] = 1024
property["index"] = "not_analyzed"
}
return property
}
func (p *Processor) text(f *common.Field) common.MapStr {
properties := getDefaultProperties(f)
properties["type"] = "text"
if p.EsVersion.IsMajor(2) {
properties["type"] = "string"
properties["index"] = "analyzed"
if !f.Norms {
properties["norms"] = common.MapStr{
"enabled": false,
}
}
} else {
if !f.Norms {
properties["norms"] = false
}
}
if f.Analyzer != "" {
properties["analyzer"] = f.Analyzer
}
if f.SearchAnalyzer != "" {
properties["search_analyzer"] = f.SearchAnalyzer
}
if len(f.MultiFields) > 0 {
fields := common.MapStr{}
p.process(f.MultiFields, "", fields)
properties["fields"] = fields
}
return properties
}
func (p *Processor) array(f *common.Field) common.MapStr {
properties := getDefaultProperties(f)
if f.ObjectType != "" {
properties["type"] = f.ObjectType
}
return properties
}
func (p *Processor) object(f *common.Field) common.MapStr {
dynProperties := getDefaultProperties(f)
switch f.ObjectType {
case "text":
dynProperties["type"] = "text"
if p.EsVersion.IsMajor(2) {
dynProperties["type"] = "string"
dynProperties["index"] = "analyzed"
}
addDynamicTemplate(f, dynProperties, "string")
case "long":
dynProperties["type"] = f.ObjectType
addDynamicTemplate(f, dynProperties, "long")
case "keyword":
dynProperties["type"] = f.ObjectType
addDynamicTemplate(f, dynProperties, "string")
}
properties := getDefaultProperties(f)
properties["type"] = "object"
if f.Enabled != nil {
properties["enabled"] = *f.Enabled
}
if f.Dynamic.Value != nil {
properties["dynamic"] = f.Dynamic.Value
}
return properties
}
func addDynamicTemplate(f *common.Field, properties common.MapStr, matchType string) {
path := ""
if len(f.Path) > 0 {
path = f.Path + "."
}
template := common.MapStr{
// Set the path of the field as name
path + f.Name: common.MapStr{
"mapping": properties,
"match_mapping_type": matchType,
"path_match": path + f.Name + ".*",
},
}
dynamicTemplates = append(dynamicTemplates, template)
}
func getDefaultProperties(f *common.Field) common.MapStr {
// Currently no defaults exist
properties := common.MapStr{}
if f.Index != nil {
properties["index"] = *f.Index
}
if f.DocValues != nil {
properties["doc_values"] = *f.DocValues
}
if f.CopyTo != "" {
properties["copy_to"] = f.CopyTo
}
return properties
}