def get_es_mapping(cls, _depth=None, types_map=None): """ Generate ES mapping from model schema. """ from nefertari.elasticsearch import ES if types_map is None: types_map = TYPES_MAP if _depth is None: _depth = cls._nesting_depth depth_reached = _depth <= 0 nested_substitutions = [] properties = {} mapping = { ES.src2type(cls.__name__): { 'properties': properties } } mapper = class_mapper(cls) columns = {c.name: c for c in mapper.columns} relationships = {r.key: r for r in mapper.relationships} for name, column in columns.items(): column_type = column.type if isinstance(column_type, types.ChoiceArray): column_type = column_type.impl.item_type column_type = type(column_type) if column_type not in types_map: continue properties[name] = types_map[column_type] if hasattr(column, "_es_multi_field") and getattr(column, "_es_multi_field"): multi_fields = getattr(column, "_es_multi_field") properties[name] = properties[name].copy() properties[name]["fields"] = {} for multi_field_name in multi_fields: properties[name]["fields"][multi_field_name] = multi_fields[multi_field_name].copy() properties[name]["fields"][multi_field_name].update(types_map[column_type]) for name, column in relationships.items(): if name in cls._nested_relationships and not depth_reached: column_type = {'type': 'nested', 'include_in_parent': True} nested_substitutions.append(name) submapping, sub_substitutions = column.mapper.class_.get_es_mapping( _depth=_depth - 1) column_type.update(list(submapping.values())[0]) properties[name + "_nested"] = column_type rel_pk_field = column.mapper.class_.pk_field_type() column_type = types_map[rel_pk_field] properties[name] = column_type properties['_pk'] = {'type': 'string'} return mapping, nested_substitutions
def _get_es_types(models): """ Get ES types from document model classes. :param models: List of document classes. :returns: String with ES type names joing by comma. """ type_names = [t.__name__ for t in models if getattr(t, '_index_enabled', False)] es_types = [ES.src2type(name) for name in type_names] return ','.join(es_types)
def _get_es_types(models): """ Get ES types from document model classes. :param models: List of document classes. :returns: String with ES type names joing by comma. """ type_names = [ t.__name__ for t in models if getattr(t, '_index_enabled', False) ] es_types = [ES.src2type(name) for name in type_names] return ','.join(es_types)
def _set_object_self(self, obj): """ Add '_self' key value to :obj: dict. """ from nefertari.elasticsearch import ES location = self.request.path_url try: type_, obj_pk = obj['_type'], obj['_pk'] except KeyError: return resource = (self.model_collections.get(type_) or self.model_collections.get(ES.src2type(type_))) if resource is not None: location = self.request.route_url( resource.uid, **{resource.id_name: obj_pk}) obj.setdefault('_self', location)
def determine_types(self): """ Determine ES type names from request data. In particular `request.matchdict['collections']` is used to determine types names. Its value is comma-separated sequence of collection names under which views have been registered. """ from nefertari.elasticsearch import ES collections = self.get_collections() resources = self.get_resources(collections) models = set([res.view.Model for res in resources]) es_models = [mdl for mdl in models if mdl and getattr(mdl, '_index_enabled', False)] types = [ES.src2type(mdl.__name__) for mdl in es_models] return types
def determine_types(self): """ Determine ES type names from request data. In particular `request.matchdict['collections']` is used to determine types names. Its value is comma-separated sequence of collection names under which views have been registered. """ from nefertari.elasticsearch import ES collections = self.get_collections() resources = self.get_resources(collections) models = set([res.view.Model for res in resources]) es_models = [ mdl for mdl in models if mdl and getattr(mdl, '_index_enabled', False) ] types = [ES.src2type(mdl.__name__) for mdl in es_models] return types
def _set_object_self(self, obj): """ Add '_self' key value to :obj: dict. """ from nefertari.elasticsearch import ES location = self.request.path_url route_kwargs = {} """ Check for parents """ if self.request.matchdict: route_kwargs.update(self.request.matchdict) try: type_, obj_pk = obj['_type'], obj['_pk'] except KeyError: return resource = (self.model_collections.get(type_) or self.model_collections.get(ES.src2type(type_))) if resource is not None: route_kwargs.update({resource.id_name: obj_pk}) location = self.request.route_url(resource.uid, **route_kwargs) obj.setdefault('_self', location)
def get_es_mapping(cls, _depth=None, types_map=None): """ Generate ES mapping from model schema. """ from nefertari.elasticsearch import ES if types_map is None: types_map = TYPES_MAP if _depth is None: _depth = cls._nesting_depth depth_reached = _depth <= 0 properties = {} mapping = { ES.src2type(cls.__name__): { 'properties': properties } } fields = cls._fields.copy() for name, field in fields.items(): if isinstance(field, RelationshipField): field = field.field if isinstance(field, (ReferenceField, RelationshipField)): if name in cls._nested_relationships and not depth_reached: field_mapping = {'type': 'nested'} submapping = field.document_type.get_es_mapping( _depth=_depth-1) field_mapping.update(list(submapping.values())[0]) else: field_mapping = types_map[ field.document_type.pk_field_type()] properties[name] = field_mapping continue if isinstance(field, ChoiceField): field = field._real_field field_type = type(field) if field_type is ListField: field_type = field.item_type if field_type not in types_map: continue properties[name] = types_map[field_type] properties['_pk'] = {'type': 'string'} return mapping
def _set_object_self(self, obj): """ Add '_self' key value to :obj: dict. """ from nefertari.elasticsearch import ES location = self.request.path_url route_kwargs = {} """ Check for parents """ if self.request.matchdict: route_kwargs.update(self.request.matchdict) try: type_, obj_pk = obj['_type'], obj['_pk'] except KeyError: return resource = (self.model_collections.get(type_) or self.model_collections.get(ES.src2type(type_))) if resource is not None: route_kwargs.update({resource.id_name: obj_pk}) location = self.request.route_url( resource.uid, **route_kwargs) obj.setdefault('_self', location)
def get_es_mapping(cls, _depth=None, types_map=None): """ Generate ES mapping from model schema. """ from nefertari.elasticsearch import ES if types_map is None: types_map = TYPES_MAP if _depth is None: _depth = cls._nesting_depth depth_reached = _depth <= 0 properties = {} mapping = { ES.src2type(cls.__name__): { 'properties': properties } } mapper = class_mapper(cls) columns = {c.name: c for c in mapper.columns} relationships = {r.key: r for r in mapper.relationships} for name, column in columns.items(): column_type = column.type if isinstance(column_type, types.ChoiceArray): column_type = column_type.impl.item_type column_type = type(column_type) if column_type not in types_map: continue properties[name] = types_map[column_type] for name, column in relationships.items(): if name in cls._nested_relationships and not depth_reached: column_type = {'type': 'nested'} submapping = column.mapper.class_.get_es_mapping( _depth=_depth-1) column_type.update(list(submapping.values())[0]) else: rel_pk_field = column.mapper.class_.pk_field_type() column_type = types_map[rel_pk_field] properties[name] = column_type properties['_pk'] = {'type': 'string'} return mapping
def get_es_mapping(cls, _depth=None, types_map=None): """ Generate ES mapping from model schema. """ from nefertari.elasticsearch import ES if types_map is None: types_map = TYPES_MAP if _depth is None: _depth = cls._nesting_depth depth_reached = _depth <= 0 properties = {} mapping = {ES.src2type(cls.__name__): {'properties': properties}} mapper = class_mapper(cls) columns = {c.name: c for c in mapper.columns} relationships = {r.key: r for r in mapper.relationships} for name, column in columns.items(): column_type = column.type if isinstance(column_type, types.ChoiceArray): column_type = column_type.impl.item_type column_type = type(column_type) if column_type not in types_map: continue properties[name] = types_map[column_type] for name, column in relationships.items(): if name in cls._nested_relationships and not depth_reached: column_type = {'type': 'nested'} submapping = column.mapper.class_.get_es_mapping( _depth=_depth - 1) column_type.update(list(submapping.values())[0]) else: rel_pk_field = column.mapper.class_.pk_field_type() column_type = types_map[rel_pk_field] properties[name] = column_type properties['_pk'] = {'type': 'string'} return mapping
def get_es_mapping(cls, _depth=None, types_map=None): """ Generate ES mapping from model schema. """ from nefertari.elasticsearch import ES if types_map is None: types_map = TYPES_MAP if _depth is None: _depth = cls._nesting_depth depth_reached = _depth <= 0 nested_substitutions = [] properties = {} mapping = {ES.src2type(cls.__name__): {'properties': properties}} mapper = class_mapper(cls) columns = {c.name: c for c in mapper.columns} relationships = {r.key: r for r in mapper.relationships} for name, column in columns.items(): column_type = column.type if isinstance(column_type, types.ChoiceArray): column_type = column_type.impl.item_type column_type = type(column_type) if column_type not in types_map: continue if getattr(column, '_custom_analyzer', False): properties[name] = {'analyzer': column._custom_analyzer} properties[name].update(types_map[column_type]) continue properties[name] = types_map[column_type] if hasattr(column, "_es_multi_field") and getattr( column, "_es_multi_field"): multi_fields = getattr(column, "_es_multi_field") properties[name] = properties[name].copy() properties[name]["fields"] = {} for multi_field_name in multi_fields: properties[name]["fields"][ multi_field_name] = multi_fields[ multi_field_name].copy() properties[name]["fields"][multi_field_name].update( types_map[column_type]) for name, column in relationships.items(): if name in cls._nested_relationships and not depth_reached: column_type = {'type': 'nested', 'include_in_parent': True} nested_substitutions.append(name) submapping, sub_substitutions = column.mapper.class_.get_es_mapping( _depth=_depth - 1) column_type.update(list(submapping.values())[0]) properties[name + "_nested"] = column_type rel_pk_field = column.mapper.class_.pk_field_type() column_type = types_map[rel_pk_field] properties[name] = column_type properties['_pk'] = {'type': 'string'} return mapping, nested_substitutions