WhooshAlchemy
=============
Supports the easy text-indexing of SQLAlchemy model fields.
BSD license.
Written by Stefane Fermigier (http://www.fermigier.com/) based on
Flask-WhooshAlchemy written by Karl Gyllstromk.
Quick-start example
-------------------
Import this library:
>>> from whooshalchemy import IndexService
Standard 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 sessionmaker
Setup SQLAlchemy:
>>> engine = create_engine('sqlite:///:memory:')
>>> Session = sessionmaker(bind=engine)
>>> session = Session()
>>> Base = declarative_base()
Our model:
>>> class BlogPost(Base):
... __tablename__ = 'blogpost'
... __searchable__ = ['title', 'content'] # 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'))
[BlogPost(title=My cool title)]
>>> list(BlogPost.search_query(u'first'))
[BlogPost(title=My cool title)]
Note: the response is a ``BaseQuery`` object, so you can append other SQL operations:
>>> list(BlogPost.search_query(u'first').filter(BlogPost.id >= 0))
[BlogPost(title=My cool title)]
Using with Flask
----------------
Setup 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.
CHANGES
-------
Version 0.3.0 (2017/01/09)
~~~~~~~~~~~~~~~~~~~~~~~~~~
- Python 3 compatibility.
- Use pytest instead of nose for tests
Raw data
{
"_id": null,
"home_page": "https://github.com/sfermigier/WhooshAlchemy",
"name": "WhooshAlchemy",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Stefane Fermigier",
"author_email": "sf@fermigier.com",
"download_url": "https://files.pythonhosted.org/packages/63/65/01fc3510ff51132dbebe6d8a0de08a93a54e227c7aa9962ca4d671e21ae0/WhooshAlchemy-0.3.1.tar.gz",
"platform": "any",
"description": "WhooshAlchemy\n=============\n\nSupports the easy text-indexing of SQLAlchemy model fields.\n\nBSD license.\n\nWritten by Stefane Fermigier (http://www.fermigier.com/) based on\nFlask-WhooshAlchemy written by Karl Gyllstromk.\n\n\nQuick-start example\n-------------------\n\nImport this library:\n\n>>> from whooshalchemy import IndexService\n\nStandard SQLAlchemy imports:\n\n>>> from sqlalchemy.ext.declarative import declarative_base\n>>> from sqlalchemy.schema import Column\n>>> from sqlalchemy.types import Integer, Text, DateTime\n>>> from sqlalchemy.engine import create_engine\n>>> from sqlalchemy.orm.session import sessionmaker\n\nSetup SQLAlchemy:\n\n>>> engine = create_engine('sqlite:///:memory:')\n>>> Session = sessionmaker(bind=engine)\n>>> session = Session()\n>>> Base = declarative_base()\n\nOur model:\n\n>>> class BlogPost(Base):\n... __tablename__ = 'blogpost'\n... __searchable__ = ['title', 'content'] # these fields will be indexed by whoosh\n...\n... id = Column(Integer, primary_key=True)\n... title = Column(Text)\n... content = Column(Text)\n...\n... def __repr__(self):\n... return '{0}(title={1})'.format(self.__class__.__name__, self.title)\n...\n>>> Base.metadata.create_all(engine)\n\nCreate and init indexing service:\n\n>>> config = {\"WHOOSH_BASE\": \"/tmp/whoosh\"}\n>>> index_service = IndexService(config=config, session=session)\n>>> index_service.register_class(BlogPost)\nFileIndex(FileStorage('/tmp/whoosh/BlogPost'), 'MAIN')\n\nCreate a blog post:\n\n>>> m = BlogPost(title=u'My cool title', content=u'This is the first post.')\n>>> session.add(m); session.commit()\n\nPerform a few searches:\n\n>>> list(BlogPost.search_query(u'cool'))\n[BlogPost(title=My cool title)]\n>>> list(BlogPost.search_query(u'first'))\n[BlogPost(title=My cool title)]\n\nNote: the response is a ``BaseQuery`` object, so you can append other SQL operations:\n\n>>> list(BlogPost.search_query(u'first').filter(BlogPost.id >= 0))\n[BlogPost(title=My cool title)]\n\nUsing with Flask\n----------------\n\nSetup you Flask app, create the ``db`` object (``db = SQLAlchemy(app)``), import your models.\n\nSet ``WHOOSH_BASE`` to your Whoosh index directory in your Flask , then create the index service\nand register your models:\n\n>>> index_service = IndexService(config=app.config)\n>>> index_service.register_class(MyFirstModel)\n>>> index_service.register_class(MySecondModel)\n\nEtc.\n\nCHANGES\n-------\n\nVersion 0.3.0 (2017/01/09)\n~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n- Python 3 compatibility.\n- Use pytest instead of nose for tests",
"bugtrack_url": null,
"license": "BSD",
"summary": "Whoosh extension to SQLAlchemy",
"version": "0.3.1",
"project_urls": {
"Homepage": "https://github.com/sfermigier/WhooshAlchemy"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "636501fc3510ff51132dbebe6d8a0de08a93a54e227c7aa9962ca4d671e21ae0",
"md5": "73147f4bd8f9ce1e2ed7e73f02f4e33c",
"sha256": "30884a13557b9febbeeb31f8907e47ff7560314de85fede84d64fbe5fe989ec9"
},
"downloads": -1,
"filename": "WhooshAlchemy-0.3.1.tar.gz",
"has_sig": false,
"md5_digest": "73147f4bd8f9ce1e2ed7e73f02f4e33c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 5466,
"upload_time": "2017-01-09T17:57:21",
"upload_time_iso_8601": "2017-01-09T17:57:21.082004Z",
"url": "https://files.pythonhosted.org/packages/63/65/01fc3510ff51132dbebe6d8a0de08a93a54e227c7aa9962ca4d671e21ae0/WhooshAlchemy-0.3.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2017-01-09 17:57:21",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "sfermigier",
"github_project": "WhooshAlchemy",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"tox": true,
"lcname": "whooshalchemy"
}