Coverage for portality/lib/es_data_mapping.py: 96%
26 statements
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-22 15:59 +0100
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-22 15:59 +0100
1# -*- coding: UTF-8 -*-
2""" Create mappings from models """
4# ~~ESMappings:Library->Elasticsearch:Technology~~
5# ~~->Seamless:Library~~
6# ~~->DataObj:Library~~
8from portality.lib import plugin
11def get_mappings(app):
12 """Get the full set of mappings required for the app"""
14 # LEGACY DEFAULT MAPPINGS
15 mappings = app.config["MAPPINGS"]
17 # TYPE SPECIFIC MAPPINGS
18 # get the list of classes which carry the type-specific mappings to be loaded
19 mapping_daos = app.config.get("ELASTIC_SEARCH_MAPPINGS", [])
21 # load each class and execute the "mappings" function to get the mappings that need to be imported
22 for cname in mapping_daos:
23 klazz = plugin.load_class_raw(cname)
24 mappings[klazz.__type__] = {'mappings': klazz().mappings()}
26 return mappings
29def apply_mapping_opts(field_name, path, spec, mapping_opts):
30 dot_path = '.'.join(path + (field_name,))
31 if dot_path in mapping_opts.get('exceptions', {}):
32 return mapping_opts['exceptions'][dot_path]
33 elif spec['coerce'] in mapping_opts['coerces']:
34 return mapping_opts['coerces'][spec['coerce']]
35 else:
36 # We have found a data type in the struct we don't have a map for to ES type.
37 raise Exception("Mapping error - no mapping found for {}".format(spec['coerce']))
40def create_mapping(struct, mapping_opts, path=()):
41 result = {"properties": {}}
43 for field, spec in struct.get("fields", {}).items():
44 result["properties"][field] = apply_mapping_opts(field, path, spec, mapping_opts)
46 for field, spec in struct.get("lists", {}).items():
47 if "coerce" in spec:
48 result["properties"][field] = apply_mapping_opts(field, path, spec, mapping_opts)
50 for struct_name, struct_body in struct.get("structs", {}).items():
51 result["properties"][struct_name] = create_mapping(struct_body, mapping_opts, path + (struct_name,))
53 return result