Skip to main content

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

Metadata

Release files for WhooshAlchemy 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for WhooshAlchemy 0.3.1
File Size Uploaded
WhooshAlchemy-0.3.1.tar.gz 5.5 kB Details

Release files / WhooshAlchemy-0.3.1.tar.gz

Download URL WhooshAlchemy-0.3.1.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
30884a13557b9febbeeb31f8907e47ff7560314de85fede84d64fbe5fe989ec9
BLAKE2b-256 checksum
How to use checksums
636501fc3510ff51132dbebe6d8a0de08a93a54e227c7aa9962ca4d671e21ae0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3.1 This release

1 release file

0.3.0

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page