def get_schema(model, analyzer):
schema = {}
primary = None
searchable = set(getattr(model, '__searchable__', []))
for field in model.__table__.columns:
# primary key id
if field.primary_key:
schema[field.name] = whoosh_fields.ID(
stored=True, unique=True, sortable=True)
primary = field.name
if field.name not in searchable:
continue
# text types
if isinstance(field.type, TEXT_TYPES):
schema[field.name] = whoosh_fields.TEXT(analyzer=analyzer)
elif isinstance(field.type, DATE_TYPES):
is_unique = getattr(field, 'unique', False)
schema[field.name] = whoosh_fields.DATETIME(unique=is_unique)
elif isinstance(field.type, sql_types.Boolean):
schema[field.name] = whoosh_fields.BOOLEAN()
else:
raise WhooshAlchemyError(
'cannot index column of type %s' % field.type)
return whoosh_fields.Schema(**schema), primary
评论列表
文章目录