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

26 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-19 17:05 +0100

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 # 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", []) 

20 

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()} 

25 

26 return mappings 

27 

28 

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'])) 

38 

39 

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

41 result = {"properties": {}} 

42 

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

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

45 

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) 

49 

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,)) 

52 

53 return result