Skip to content

Commit

Permalink
Add support for keyword in dynamic fields (#4606)
Browse files Browse the repository at this point in the history
* Add support for keyword in dynamic fields

This PR allows fields under a namespace to be set to keyword instead of auto detection from elasticsearch.

* Fixes issue where dynamic fields needed a path in addition to a name, means it did not work properly on the top level
* Add tests for dynamic fields.

* switch to switch / case statement
  • Loading branch information
ruflin authored and andrewkroh committed Jul 6, 2017
1 parent 7223e5f commit 877f311
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
24 changes: 15 additions & 9 deletions libbeat/template/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,23 @@ func (f *Field) array() common.MapStr {

func (f *Field) object() common.MapStr {

if f.ObjectType == "text" {
dynProperties := f.getDefaultProperties()
dynProperties := f.getDefaultProperties()

switch f.ObjectType {
case "text":
dynProperties["type"] = "text"

if f.esVersion.IsMajor(2) {
dynProperties["type"] = "string"
dynProperties["index"] = "analyzed"
}
f.addDynamicTemplate(dynProperties, "string")
}

if f.ObjectType == "long" {
dynProperties := f.getDefaultProperties()
dynProperties["type"] = "long"
case "long":
dynProperties["type"] = f.ObjectType
f.addDynamicTemplate(dynProperties, "long")
case "keyword":
dynProperties["type"] = f.ObjectType
f.addDynamicTemplate(dynProperties, "string")
}

properties := f.getDefaultProperties()
Expand All @@ -161,12 +163,16 @@ func (f *Field) object() common.MapStr {

func (f *Field) addDynamicTemplate(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
f.path + "." + f.Name: common.MapStr{
path + f.Name: common.MapStr{
"mapping": properties,
"match_mapping_type": matchType,
"path_match": f.path + "." + f.Name + ".*",
"path_match": path + f.Name + ".*",
},
}

Expand Down
58 changes: 58 additions & 0 deletions libbeat/template/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,61 @@ func TestField(t *testing.T) {
assert.Equal(t, test.output, output)
}
}

func TestDynamicTemplate(t *testing.T) {

tests := []struct {
field Field
method func(f Field) common.MapStr
output common.MapStr
}{
{
field: Field{
Type: "object", ObjectType: "keyword",
Name: "context",
},
method: func(f Field) common.MapStr { return f.object() },
output: common.MapStr{
"context": common.MapStr{
"mapping": common.MapStr{"type": "keyword"},
"match_mapping_type": "string",
"path_match": "context.*",
},
},
},
{
field: Field{
Type: "object", ObjectType: "long",
path: "language", Name: "english",
},
method: func(f Field) common.MapStr { return f.object() },
output: common.MapStr{
"language.english": common.MapStr{
"mapping": common.MapStr{"type": "long"},
"match_mapping_type": "long",
"path_match": "language.english.*",
},
},
},
{
field: Field{
Type: "object", ObjectType: "text",
path: "language", Name: "english",
},
method: func(f Field) common.MapStr { return f.object() },
output: common.MapStr{
"language.english": common.MapStr{
"mapping": common.MapStr{"type": "text"},
"match_mapping_type": "string",
"path_match": "language.english.*",
},
},
},
}

for _, test := range tests {
dynamicTemplates = nil
test.method(test.field)
assert.Equal(t, test.output, dynamicTemplates[0])
}
}

0 comments on commit 877f311

Please sign in to comment.