Commit bcf192ec authored by David Mendez's avatar David Mendez

Implement mapping_to_apply generation for nested properties

parent f07d8524
Pipeline #93046 passed with stages
in 3 minutes and 33 seconds
......@@ -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):
......
{
"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
......@@ -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:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment