Skip to content

Commit

Permalink
Add support for index to fields.yml (elastic#4853)
Browse files Browse the repository at this point in the history
Setting `index: false` for a field was not possible so far. This adds this possibility. For more details in `index` see: https://www.elastic.co/guide/en/elasticsearch/reference/5.5/mapping-index.html

Additional changes:

* `enable` and `dynamic` are only available for objects. The functionality was move to object. https://www.elastic.co/guide/en/elasticsearch/reference/5.5/enabled.html
* Add support for `enable` and `index` to the docs script so it is visible in the docs which fields are indexed and which ones not
  • Loading branch information
ruflin authored and tsg committed Aug 10, 2017
1 parent 318cc1a commit af3ecf9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
8 changes: 8 additions & 0 deletions libbeat/scripts/generate_fields_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ def document_field(output, field, path):
if "description" in field:
output.write("{}\n\n".format(field["description"]))

if "index" in field:
if not field["index"]:
output.write("{}\n\n".format("Field is not indexed."))

if "enable" in field:
if not field["enable"]:
output.write("{}\n\n".format("Object is not enabled."))


def fields_to_asciidoc(input, output, beat):

Expand Down
20 changes: 13 additions & 7 deletions libbeat/template/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Field struct {
SearchAnalyzer string `config:"search_analyzer"`
Norms bool `config:"norms"`
Dynamic dynamicType `config:"dynamic"`
Index *bool `config:"index"`

path string
esVersion common.Version
Expand Down Expand Up @@ -161,6 +162,14 @@ func (f *Field) object() common.MapStr {

properties := f.getDefaultProperties()
properties["type"] = "object"
if f.Enabled != nil {
properties["enabled"] = *f.Enabled
}

if f.Dynamic.value != nil {
properties["dynamic"] = f.Dynamic.value
}

return properties
}

Expand All @@ -183,16 +192,13 @@ func (f *Field) addDynamicTemplate(properties common.MapStr, matchType string) {

func (f *Field) getDefaultProperties() common.MapStr {
// Currently no defaults exist
property := common.MapStr{}
if f.Enabled != nil {
property["enabled"] = *f.Enabled
}
properties := common.MapStr{}

if f.Dynamic.value != nil {
property["dynamic"] = f.Dynamic.value
if f.Index != nil {
properties["index"] = *f.Index
}

return property
return properties
}

// Recursively generates the correct key based on the dots
Expand Down
27 changes: 21 additions & 6 deletions libbeat/template/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TestField(t *testing.T) {
assert.NoError(t, err)

falseVar := false
trueVar := true

tests := []struct {
field Field
Expand Down Expand Up @@ -52,7 +53,7 @@ func TestField(t *testing.T) {
},
{
field: Field{Type: "object", Enabled: &falseVar},
method: func(f Field) common.MapStr { return f.other() },
method: func(f Field) common.MapStr { return f.object() },
output: common.MapStr{
"type": "object",
"enabled": false,
Expand Down Expand Up @@ -135,23 +136,37 @@ func TestField(t *testing.T) {
},
{
field: Field{Dynamic: dynamicType{false}},
method: func(f Field) common.MapStr { return f.other() },
method: func(f Field) common.MapStr { return f.object() },
output: common.MapStr{
"dynamic": false,
"dynamic": false, "type": "object",
},
},
{
field: Field{Dynamic: dynamicType{true}},
method: func(f Field) common.MapStr { return f.other() },
method: func(f Field) common.MapStr { return f.object() },
output: common.MapStr{
"dynamic": true,
"dynamic": true, "type": "object",
},
},
{
field: Field{Dynamic: dynamicType{"strict"}},
method: func(f Field) common.MapStr { return f.object() },
output: common.MapStr{
"dynamic": "strict", "type": "object",
},
},
{
field: Field{Type: "long", Index: &falseVar},
method: func(f Field) common.MapStr { return f.other() },
output: common.MapStr{
"type": "long", "index": false,
},
},
{
field: Field{Type: "text", Index: &trueVar},
method: func(f Field) common.MapStr { return f.other() },
output: common.MapStr{
"dynamic": "strict",
"type": "text", "index": true,
},
},
}
Expand Down

0 comments on commit af3ecf9

Please sign in to comment.