Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6.x index template data type fallback #1171

Merged
merged 6 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion scripts/generators/es_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from os.path import join
from generators import ecs_helpers
from schema.cleaner import field_or_multi_field_datatype_defaults
from schema.oss import TYPE_FALLBACKS


def generate(ecs_flat, ecs_version, out_dir, template_settings_file, mapping_settings_file):
Expand Down Expand Up @@ -93,7 +95,9 @@ def generate_template_version(elasticsearch_version, mappings_section, out_dir,
else:
template = default_template_settings()
if elasticsearch_version == 6:
template['mappings'] = {'_doc': mappings_section}
es6_mappings_section = copy.deepcopy(mappings_section)
es6_type_fallback(es6_mappings_section['properties'])
template['mappings'] = {'_doc': es6_mappings_section}
else:
template['mappings'] = mappings_section

Expand Down Expand Up @@ -144,3 +148,22 @@ def default_mapping_settings(ecs_version):
],
"properties": {}
}


def es6_type_fallback(mappings):
'''
Visits each leaf in mappings object and fallback to an
Elasticsearch 6.x supported type.

Since a field like `wildcard` won't have the same defaults as
a `keyword` field, we must add any missing defaults.
'''

for (name, details) in mappings.items():
if 'type' in details:
fallback_type = TYPE_FALLBACKS.get(details['type'])
if fallback_type:
mappings[name]['type'] = fallback_type
field_or_multi_field_datatype_defaults(mappings[name])
if 'properties' in details:
es6_type_fallback(details['properties'])
92 changes: 92 additions & 0 deletions scripts/tests/test_es_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,98 @@ def test_constant_keyword_no_value(self):
exp = {'type': 'constant_keyword'}
self.assertEqual(es_template.entry_for(test_map), exp)

def test_es6_fallback_base_case_wildcard(self):
test_map = {
"field": {
"name": "field",
"type": "wildcard"
}
}

exp = {
"field": {
"name": "field",
"type": "keyword",
"ignore_above": 1024
}
}

es_template.es6_type_fallback(test_map)
self.assertEqual(test_map, exp)

def test_es6_fallback_recursive_case_wildcard(self):
test_map = {
"top_field": {
"properties": {
"field": {
"name": "field",
"type": "wildcard"
}
}
}
}

exp = {
"top_field": {
"properties": {
"field": {
"name": "field",
"type": "keyword",
"ignore_above": 1024
}
}
}
}

es_template.es6_type_fallback(test_map)
self.assertEqual(test_map, exp)

def test_es6_fallback_base_case_constant_keyword(self):
test_map = {
"field": {
"name": "field",
"type": "constant_keyword"
}
}

exp = {
"field": {
"name": "field",
"type": "keyword",
"ignore_above": 1024
}
}

es_template.es6_type_fallback(test_map)
self.assertEqual(test_map, exp)

def test_es6_fallback_recursive_case_constant_keyword(self):
ebeahan marked this conversation as resolved.
Show resolved Hide resolved
test_map = {
"top_field": {
"properties": {
"field": {
"name": "field",
"type": "constant_keyword"
}
}
}
}

exp = {
"top_field": {
"properties": {
"field": {
"name": "field",
"type": "keyword",
"ignore_above": 1024
}
}
}
}

es_template.es6_type_fallback(test_map)
self.assertEqual(test_map, exp)


if __name__ == '__main__':
unittest.main()