class Meta(): document_type = 'purchase_order' document_boost = 0.9 fields = [ search_mappings.StringField(name="reference", boost=document_boost * 3.0, index="analyzed", term_vector="with_positions_offsets"), search_mappings.StringField(name="related_quotation_reference", boost=document_boost * 1.5, index="analyzed", term_vector="with_positions_offsets"), search_mappings.StringField(name="related_invoice_reference", boost=document_boost * 1.5, index="analyzed", term_vector="with_positions_offsets"), search_mappings.StringField(name="contact", index="analyzed", term_vector="with_positions_offsets"), search_mappings.StringField(name="organization", index="analyzed", term_vector="with_positions_offsets"), search_mappings.DateField(name="purchase_order_date", index="analyzed", term_vector="with_positions_offsets", include_in_all=False), search_mappings.StringField(name="state", index="not_analyzed", term_vector="with_positions_offsets", include_in_all=False), ]
def get_mapping_for_field(field, depth=1, **options): """Given a field returns a mapping""" ntype = type(field).__name__ if ntype in ["AutoField"]: # return mappings.MultiField(name=field.name, # fields={field.name:mappings.StringField(name=field.name, store=True), # "int":mappings.IntegerField(name="int", store=True)} # ) return mappings.StringField(name=field.name, store=True) elif ntype in [ "IntegerField", "PositiveSmallIntegerField", "SmallIntegerField", "PositiveIntegerField", "PositionField", ]: return mappings.IntegerField(name=field.name, store=True) elif ntype in [ "FloatField", "DecimalField", ]: return mappings.DoubleField(name=field.name, store=True) elif ntype in [ "BooleanField", "NullBooleanField", ]: return mappings.BooleanField(name=field.name, store=True) elif ntype in [ "DateField", "DateTimeField", "CreationDateTimeField", "ModificationDateTimeField", "AddedDateTimeField", "ModifiedDateTimeField", "brainaetic.djangoutils.db.fields.CreationDateTimeField", "brainaetic.djangoutils.db.fields.ModificationDateTimeField", ]: return mappings.DateField(name=field.name, store=True) elif ntype in [ "SlugField", "EmailField", "TagField", "URLField", "CharField", "ImageField", "FileField", ]: return mappings.MultiField( name=field.name, fields={ field.name: mappings.StringField(name=field.name, index="not_analyzed", store=True), "tk": mappings.StringField(name="tk", store=True, index="analyzed", term_vector="with_positions_offsets") }) elif ntype in [ "TextField", ]: data = dict(name=field.name, store=True, index="analyzed", term_vector="with_positions_offsets") if field.unique: data['index'] = 'not_analyzed' data.update(options) if data['index'] == 'not_analyzed': del data['term_vector'] return mappings.StringField(**data) elif ntype in [ "ForeignKey", "TaggableManager", "GenericRelation", ]: if depth >= 0: mapper = model_to_mapping(field.rel.to, depth - 1) if mapper: mapper.name = field.name return mapper return None return get_mapping_for_field(field.rel.to._meta.pk, depth - 1) # "IPAddressField", # 'PickledObjectField' elif ntype in [ "ManyToManyField", ]: if depth > 0: mapper = model_to_mapping(field.rel.to, depth - 1) mapper.name = field.name return mapper if depth == 0: mapper = get_mapping_for_field(field.rel.to._meta.pk, depth - 1) if mapper: mapper.name = field.name return mapper return None if depth < 0: return None print ntype return None