def test_type_mapping(): """Test mapping a json type to another elasticsearch type.""" json_schema = { 'id': 'https://example.org/root_schema#', 'type': 'object', 'properties': { 'attr1': {'type': 'string', 'format': 'custom-date'}, 'attr2': {'type': 'number'}, }, } es_mapping = { '_all': {'enable': True}, 'numeric_detection': True, 'date_detection': True, 'properties': { 'attr1': {'type': 'date', 'format': 'YYYY'}, 'attr2': {'type': 'integer', 'coerce': False}, } } config = ElasticMappingGeneratorConfig() \ .map_type(es_type='integer', json_type='number', es_props={'coerce': False}) \ .map_type(es_type='date', json_type='string', json_format='custom-date') config.date_format = 'YYYY' assert config.date_format == 'YYYY' result_mapping = schema_to_mapping(json_schema, json_schema['id'], {}, config) assert result_mapping == es_mapping
$ mkdir generated $ esjsonschema schema_to_mapping schema.json --config config.json | \ esjsonschema mapping_to_jinja > generated/generated_schema.json $ esjsonschema jinja_to_mapping template.json --context_path generated """ from __future__ import absolute_import, print_function import json import os from es_jsonschema.mapping import ElasticMappingGeneratorConfig, \ schema_to_mapping from es_jsonschema.templating import jinja_to_mapping, mapping_to_jinja config = ElasticMappingGeneratorConfig() with open('schema.json') as schema_file: # read the schema schema = json.load(schema_file) with open('config.json') as config_file: # load the configuration file config.load(json.load(config_file)) # generate the intermediate mapping mapping = schema_to_mapping(schema, schema['id'], {}, config) print('>> Intermediate mapping') print(json.dumps(mapping, indent=4)) print('=' * 50)