From bcf192ec74f4a6be1d3ae5a5551f546b75dd23b9 Mon Sep 17 00:00:00 2001 From: David Mendez Date: Tue, 18 Aug 2020 15:26:19 -0500 Subject: [PATCH] Implement mapping_to_apply generation for nested properties --- app/es_subset_generator/mappings.py | 47 +++++++++++++++-- .../test/data/mappings_to_apply_must_be_1json | 51 +++++++++++++++++++ .../test/test_mappings_handling.py | 18 +++---- 3 files changed, 99 insertions(+), 17 deletions(-) create mode 100644 app/es_subset_generator/test/data/mappings_to_apply_must_be_1json diff --git a/app/es_subset_generator/mappings.py b/app/es_subset_generator/mappings.py index f2585f0..5e4ee5b 100644 --- a/app/es_subset_generator/mappings.py +++ b/app/es_subset_generator/mappings.py @@ -27,15 +27,52 @@ def parse_mapping_from_es_response(index_mapping, required_source): :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 = { + all_mappings_to_apply = { 'properties': {} } - 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 + for current_property_path in required_source: + + mapping_to_apply = get_mapping_to_apply_in_subset_index(index_mapping, current_property_path) + add_mapping_to_property_path(mapping_to_apply, all_mappings_to_apply, current_property_path) + + return all_mappings_to_apply + + +def add_mapping_to_property_path(mapping_to_apply, all_mappings_to_apply, property_path): + """ + Adds the mapping to all the mappings to apply to the path indicated as parameter + :param mapping_to_apply: mapping to add to the dict + :param all_mappings_to_apply: a dict with all the mappings to apply + :param property_path: path to the property + """ + print('property_path: ', property_path) + + prop_path_parts = property_path.split('.') + current_prop = prop_path_parts[0] + + print('prop_path_parts: ', prop_path_parts) + + + if len(prop_path_parts) == 1: + + all_mappings_to_apply['properties'][current_prop] = mapping_to_apply + + else: + + container_dict = all_mappings_to_apply['properties'].get(current_prop) + + if container_dict is None: + all_mappings_to_apply['properties'][current_prop] = { + 'properties': {} + } + + rest_of_the_path_parts = prop_path_parts[1:] + rest_of_the_path = '.'.join(rest_of_the_path_parts) + inside_mappings_to_apply = all_mappings_to_apply['properties'][current_prop] + add_mapping_to_property_path(mapping_to_apply, inside_mappings_to_apply, rest_of_the_path) + - return mappings_to_apply def get_mapping_to_apply_in_subset_index(index_mapping, property_path): diff --git a/app/es_subset_generator/test/data/mappings_to_apply_must_be_1json b/app/es_subset_generator/test/data/mappings_to_apply_must_be_1json new file mode 100644 index 0000000..1670f2b --- /dev/null +++ b/app/es_subset_generator/test/data/mappings_to_apply_must_be_1json @@ -0,0 +1,51 @@ +{ + "properties": { + "molecule_properties": { + "properties": { + "hba": { + "type": "integer" + }, + "psa": { + "type": "double" + }, + "molecular_species": { + "type": "keyword" + } + } + }, + "molecule_synonyms": { + "properties": { + "molecule_synonym": { + "type": "keyword" + }, + "syn_type": { + "type": "keyword" + }, + "synonyms": { + "type": "keyword" + } + } + }, + "_metadata": { + "properties": { + "drug": { + "properties": { + "drug_data": { + "properties": { + "withdrawn_year": { + "type": "short" + }, + "prodrug": { + "type": "boolean" + } + } + } + } + } + } + }, + "helm_notation": { + "type": "keyword" + } + } +} \ No newline at end of file diff --git a/app/es_subset_generator/test/test_mappings_handling.py b/app/es_subset_generator/test/test_mappings_handling.py index d872901..4056fe8 100644 --- a/app/es_subset_generator/test/test_mappings_handling.py +++ b/app/es_subset_generator/test/test_mappings_handling.py @@ -54,25 +54,19 @@ class TestMappingsHandling(unittest.TestCase): ] mappings_to_apply_got = mappings.parse_mapping_from_es_response(mapping_from_base_index, source) - print('mappings_to_apply_got:') - print(json.dumps(mappings_to_apply_got, indent=4)) - mappings_to_apply_must_be = { - "properties": { - 'pref_name': { - 'type': 'keyword' - }, - } - } + mappings_to_apply_must_be_path = 'app/es_subset_generator/test/data/mappings_to_apply_must_be_1json' - # self.assertEqual(mappings_to_apply_got, mappings_to_apply_must_be, - # msg='The mappings were not produced correctly!') + with open(mappings_to_apply_must_be_path, 'rt') as mappings_to_apply_must_be_file: + mappings_to_apply_must_be = json.load(mappings_to_apply_must_be_file) + + 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): """ Test that it locates the config of a property """ - sample_mapping_path = 'app/es_subset_generator/test/data/sample_mapping_1.json' with open(sample_mapping_path, 'rt') as sample_mapping_file: -- GitLab