Have you tried using Flask-WhooshAlchemy.
Flask-WhooshAlchemy is a Flask extension that integrates the text-search functionality of Whoosh with the ORM of SQLAlchemy for use in Flask applications.
Install
pip install flask_whooshalchemy
Quickstart
import flask.ext.whooshalchemy
# set the location for the whoosh index
app.config['WHOOSH_BASE'] = 'path/to/whoosh/base'
class BlogPost(db.Model):
__tablename__ = 'blogpost'
__searchable__ = ['title', 'content'] # these fields will be indexed by whoosh
id = app.db.Column(app.db.Integer, primary_key=True)
title = app.db.Column(app.db.Text)
content = app.db.Column(app.db.Text)
created = db.Column(db.DateTime, default=datetime.datetime.utcnow)
def __repr__(self):
return '{0}(title={1})'.format(self.__class__.__name__, self.title)
Create a post
db.session.add(
BlogPost(title='My cool title', content='This is the first post.')
); db.session.commit()
Text searching
results = BlogPost.query.whoosh_search('cool')
We can limit results:
# get 2 best results:
results = BlogPost.query.whoosh_search('cool', limit=2)
source: Flask-WhooshAlchemy