def build_schema(self, fields, language): """ Build the index schema for the given field. New argument language. :param fields: :param language: the language code :return: a dictionary wit the field name (string) and the mapping configuration (dictionary) """ content_field_name = '' mapping = { DJANGO_CT: {'type': 'string', 'index': 'not_analyzed', 'include_in_all': False}, DJANGO_ID: {'type': 'string', 'index': 'not_analyzed', 'include_in_all': False}, } for field_name, field_class in fields.items(): field_mapping = FIELD_MAPPINGS.get( field_class.field_type, DEFAULT_FIELD_MAPPING).copy() if field_class.boost != 1.0: field_mapping['boost'] = field_class.boost if field_class.document is True: content_field_name = field_class.index_fieldname # Do this last to override `text` fields. if field_mapping['type'] == 'string': # Use the language analyzer for text fields. if field_mapping['analyzer'] == DEFAULT_FIELD_MAPPING['analyzer']: field_mapping['analyzer'] = get_analyzer_for(language) if field_class.indexed is False or hasattr(field_class, 'facet_for'): field_mapping['index'] = 'not_analyzed' del field_mapping['analyzer'] mapping[field_class.index_fieldname] = field_mapping return content_field_name, mapping
def build_schema(self, fields): content_field_name = '' mapping = { DJANGO_CT: {'type': 'string', 'index': 'not_analyzed', 'include_in_all': False}, DJANGO_ID: {'type': 'string', 'index': 'not_analyzed', 'include_in_all': False}, } for field_name, field_class in fields.items(): field_mapping = FIELD_MAPPINGS.get( field_class.field_type, self.DEFAULT_FIELD_MAPPING).copy() if field_class.boost != 1.0: field_mapping['boost'] = field_class.boost if field_class.document is True: content_field_name = field_class.index_fieldname # Do this last to override `text` fields. if field_mapping['type'] == 'string': if field_class.indexed is False or hasattr(field_class, 'facet_for'): field_mapping['index'] = 'not_analyzed' del field_mapping['analyzer'] mapping[field_class.index_fieldname] = field_mapping return (content_field_name, mapping)
def build_schema(self, fields): content_field_name = '' mapping = { DJANGO_CT: { 'type': 'keyword', 'index': 'not_analyzed', 'include_in_all': False }, DJANGO_ID: { 'type': 'keyword', 'index': 'not_analyzed', 'include_in_all': False }, } for field_name, field_class in fields.items(): field_mapping = FIELD_MAPPINGS.get(field_class.field_type, DEFAULT_FIELD_MAPPING).copy() if field_class.boost != 1.0: field_mapping['boost'] = field_class.boost if field_class.document is True: content_field_name = field_class.index_fieldname # Do this last to override `text` fields. if field_mapping['type'] == 'string': if field_class.indexed is False or hasattr( field_class, 'facet_for'): field_mapping['index'] = 'not_analyzed' del field_mapping['analyzer'] mapping[field_class.index_fieldname] = field_mapping return (content_field_name, mapping)
def build_schema(self, indexes): """Build Elasticsearch schema. :param indexes: Dictionary of model -> index. :type indexes: dict :return: Schema. :rtype: dict """ schema = {} for model, index in indexes.iteritems(): mapping_properties = { DJANGO_CT: {'type': 'string', 'index': 'not_analyzed', 'include_in_all': False}, DJANGO_ID: {'type': 'string', 'index': 'not_analyzed', 'include_in_all': False}, } for field_name, field_class in index.fields.items(): field_mapping = FIELD_MAPPINGS.get(field_class.field_type, DEFAULT_FIELD_MAPPING).copy() if field_class.boost != 1.0: field_mapping['boost'] = field_class.boost if field_class.stored is True: field_mapping['store'] = True # Do this last to override `text` fields. if field_mapping['type'] == 'string': if field_class.indexed is False or hasattr(field_class, 'facet_for') or getattr(field_class, 'is_multivalued', False): field_mapping['index'] = 'not_analyzed' try: del field_mapping['analyzer'] del field_mapping['term_vector'] except: pass elif field_class.field_type not in ('ngram', 'edge_ngram'): # Check analyzer attribute if not hasattr(field_class, 'analyzer') or field_class.analyzer is None: logger.warning("Set default analyzer for field {}".format(field_name)) field_mapping['index'] = 'analyzed' field_mapping['analyzer'] = getattr(field_class, 'analyzer', self.DEFAULT_ANALYZER) # Check term_vector attribute if hasattr(field_class, 'term_vector') and field_class.term_vector is not None: field_mapping['term_vector'] = field_class.term_vector mapping_properties[field_class.index_fieldname] = field_mapping mapping_type = { 'properties': mapping_properties, '_boost': {'name': 'boost', 'null_value': 1.0}, } schema[get_model_ct(model)] = mapping_type return schema