diff --git a/app/es_subset_generator/mappings.py b/app/es_subset_generator/mappings.py index 68e6cdd7b941d261764d70075c4a68d7d1a0b14b..f2585f0c24a843161eed67ddcb3e7b3ea1341ca1 100644 --- a/app/es_subset_generator/mappings.py +++ b/app/es_subset_generator/mappings.py @@ -24,13 +24,18 @@ def parse_mapping_from_es_response(index_mapping, required_source): """ Reads the response of the mapping from ES and produces the mapping to use for the subset index :param index_mapping: mapping dict obtained from es + :param required_source: source (list of properties) to get from the mapping :return: the mapping ready to be applied to the subset index """ + mappings_to_apply = { + 'properties': {} + } - for source_property in required_source: - print(f'locating: {source_property}') + for current_property in required_source: + mapping_to_apply = get_mapping_to_apply_in_subset_index(index_mapping, current_property) + mappings_to_apply['properties'][current_property] = mapping_to_apply - print(index_mapping) + return mappings_to_apply def get_mapping_to_apply_in_subset_index(index_mapping, property_path): @@ -44,14 +49,11 @@ def get_mapping_to_apply_in_subset_index(index_mapping, property_path): is_nested = source_property_config.get('properties') is not None if not is_nested: - mapping_to_apply = { 'type': source_property_config['type'] } return mapping_to_apply - - mapping_to_apply = { 'properties': {} } diff --git a/app/es_subset_generator/test/test_mappings_handling.py b/app/es_subset_generator/test/test_mappings_handling.py index e4b2a0db8a9fc9e0d648466989485500920336ae..e51aa9620cef4366f5f0dc4c90e81199b9ea2c31 100644 --- a/app/es_subset_generator/test/test_mappings_handling.py +++ b/app/es_subset_generator/test/test_mappings_handling.py @@ -22,8 +22,18 @@ class TestMappingsHandling(unittest.TestCase): with open(sample_mapping_path, 'rt') as sample_mapping_file: mapping_from_base_index = json.load(sample_mapping_file) source = ['pref_name'] - mappings.parse_mapping_from_es_response(mapping_from_base_index, source) - # FINISH THIS! + mappings_to_apply_got = mappings.parse_mapping_from_es_response(mapping_from_base_index, source) + + mappings_to_apply_must_be = { + "properties": { + 'pref_name': { + 'type': 'keyword' + }, + } + } + + self.assertEqual(mappings_to_apply_got, mappings_to_apply_must_be, + msg='The mappings were not produced correctly!') def test_locates_source_config_of_property(self): """