Coverage for portality/lib/es_data_mapping.py: 97%

35 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 13:43 +0000

1# -*- coding: UTF-8 -*- 

2""" Create mappings from models """ 

3 

4# ~~ESMappings:Library->Elasticsearch:Technology~~ 

5# ~~->Seamless:Library~~ 

6# ~~->DataObj:Library~~ 

7 

8from portality.lib import plugin 

9 

10 

11def get_mappings(app): 

12 """Get the full set of mappings required for the app""" 

13 

14 # LEGACY DEFAULT MAPPINGS 

15 mappings = app.config["MAPPINGS"] 

16 

17 # Ensure legacy mappings also respect any DEFAULT_INDEX_SETTINGS override (e.g. from test.cfg), 

18 # rather than using the hardcoded dict reference set at module import time in settings.py. 

19 default_settings = app.config.get("DEFAULT_INDEX_SETTINGS", {}) 

20 index_overrides = app.config.get("INDEX_SETTINGS_OVERRIDES", {}) 

21 for key in list(mappings.keys()): 

22 if 'settings' in mappings[key]: 

23 mappings[key]['settings'] = {**default_settings, **index_overrides.get(key, {})} 

24 

25 # TYPE SPECIFIC MAPPINGS 

26 # get the list of classes which carry the type-specific mappings to be loaded 

27 mapping_daos = app.config.get("ELASTIC_SEARCH_MAPPINGS", []) 

28 

29 # load each class and execute the "mappings" function to get the mappings that need to be imported 

30 for cname in mapping_daos: 

31 klazz = plugin.load_class_raw(cname) 

32 mappings[klazz.__type__] = {'mappings': klazz().mappings()} 

33 mappings[klazz.__type__]['settings'] = {**default_settings, **index_overrides.get(klazz.__type__, {})} 

34 

35 return mappings 

36 

37 

38def apply_mapping_opts(field_name, path, spec, mapping_opts): 

39 dot_path = '.'.join(path + (field_name,)) 

40 if dot_path in mapping_opts.get('exceptions', {}): 

41 return mapping_opts['exceptions'][dot_path] 

42 elif spec['coerce'] in mapping_opts['coerces']: 

43 return mapping_opts['coerces'][spec['coerce']] 

44 else: 

45 # We have found a data type in the struct we don't have a map for to ES type. 

46 raise Exception("Mapping error - no mapping found for {}".format(spec['coerce'])) 

47 

48 

49def create_mapping(struct, mapping_opts, path=()): 

50 result = {"properties": {}} 

51 

52 for field, spec in struct.get("fields", {}).items(): 

53 result["properties"][field] = apply_mapping_opts(field, path, spec, mapping_opts) 

54 

55 for field, spec in struct.get("lists", {}).items(): 

56 if "coerce" in spec: 

57 result["properties"][field] = apply_mapping_opts(field, path, spec, mapping_opts) 

58 

59 for struct_name, struct_body in struct.get("structs", {}).items(): 

60 result["properties"][struct_name] = create_mapping(struct_body, mapping_opts, path + (struct_name,)) 

61 

62 dotpath = '.'.join(path) 

63 for field, additional_mapping in mapping_opts.get("additional_mappings", {}).items(): 

64 if field.startswith(dotpath) and "." not in field[len(dotpath) + 1:]: 

65 result["properties"][field[len(dotpath):]] = additional_mapping 

66 

67 return result