WhooshAlchemy

Whoosh extension to SQLAlchemy
Download

WhooshAlchemy Ranking & Summary

Advertisement

  • Rating:
  • License:
  • BSD License
  • Price:
  • FREE
  • Publisher Name:
  • Stefane Fermigier
  • Publisher web site:
  • http://www.fermigier.com/fermigier/tabulator.html.en

WhooshAlchemy Tags


WhooshAlchemy Description

WhooshAlchemy supports the easy text-indexing of SQLAlchemy model fields.Written by Stefane Fermigier (http://www.fermigier.com/) based on Flask-WhooshAlchemy written by Karl Gyllstromk.Quick start exampleImport this library:>>> from whooshalchemy import IndexServiceStandard SQLAlchemy imports:>>> from sqlalchemy.ext.declarative import declarative_base>>> from sqlalchemy.schema import Column>>> from sqlalchemy.types import Integer, Text, DateTime>>> from sqlalchemy.engine import create_engine>>> from sqlalchemy.orm.session import sessionmakerSetup SQLAlchemy:>>> engine = create_engine('sqlite:///:memory:')>>> Session = sessionmaker(bind=engine)>>> session = Session()>>> Base = declarative_base()Our model:>>> class BlogPost(Base):... __tablename__ = 'blogpost'... __searchable__ = # these fields will be indexed by whoosh...... id = Column(Integer, primary_key=True)... title = Column(Text)... content = Column(Text)...... def __repr__(self):... return '{0}(title={1})'.format(self.__class__.__name__, self.title)...>>> Base.metadata.create_all(engine)Create and init indexing service:>>> config = {"WHOOSH_BASE": "/tmp/whoosh"}>>> index_service = IndexService(config=config, session=session)>>> index_service.register_class(BlogPost)FileIndex(FileStorage('/tmp/whoosh/BlogPost'), 'MAIN')Create a blog post:>>> m = BlogPost(title=u'My cool title', content=u'This is the first post.')>>> session.add(m); session.commit()Perform a few searches:>>> list(BlogPost.search_query(u'cool'))>>> list(BlogPost.search_query(u'first'))Note: the response is a BaseQuery object, so you can append other SQL operations:>>> list(BlogPost.search_query(u'first').filter(BlogPost.id >= 0))Using with FlaskSetup you Flask app, create the db object (db = SQLAlchemy(app)), import your models.Set WHOOSH_BASE to your Whoosh index directory in your Flask , then create the index service and register your models:>>> index_service = IndexService(config=app.config)>>> index_service.register_class(MyFirstModel)>>> index_service.register_class(MySecondModel)Etc.Product's homepage


WhooshAlchemy Related Software