whooshee = Whooshee()
def create_app():
app = Flask(name)
whooshee.init_app(app)
return app
app = Flask(name)
whooshee = Whooshee(app)
Option Description WHOOSHEE_DIR The path for the whoosh index (defaults to whooshee) WHOOSHEE_MIN_STRING_LEN Min. characters for the search string (defaults to 3) WHOOSHEE_WRITER_TIMEOUT How long should whoosh try to acquire write lock? (defaults to 2) WHOOSHEE_MEMORY_STORAGE Use the memory as storage. Useful for tests. (defaults to False) WHOOSHEE_ENABLE_INDEXING Specify whether or not to actually do any operations with the Whoosh index (defaults to True
db = SQLAlchemy()
whooshee = Whooshee()
def create_app():
app = Flask(name)
db.init_app(app)
whooshee.init_app(app)
return app
@whooshee.register_model('text')
class Article(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.UnicodeText)
@whooshee.register_model('title', 'content')
class Entry(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String)
content = db.Column(db.Text)
from flask_sqlalchemy import SQLAlchemy
from flask_whooshee import Whooshee, AbstractWhoosheer
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
you can still keep the model whoosheer
@whooshee.register_model('title', 'content') class Entry(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String) content = db.Column(db.Text) user = db.relationship(User, backref=db.backref('entries')) user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
@whooshee.register_whoosheer class EntryUserWhoosheer(AbstractWhoosheer): # create schema, the unique attribute must be in form of # model.name.lower() + '_' + 'id' (name of model primary key) schema = whoosh.fields.Schema( entry_id = whoosh.fields.NUMERIC(stored=True, unique=True), user_id = whoosh.fields.NUMERIC(stored=True), username = whoosh.fields.TEXT(), title = whoosh.fields.TEXT(), content = whoosh.fields.TEXT())
# don't forget to list the included models
models = [Entry, User]
# create insert_* and update_* methods for all models
# if you have camel case names like FooBar,
# just lowercase them: insert_foobar, update_foobar
@classmethod
def update_user(cls, writer, user):
pass # TODO: update all users entries
@classmethod
def update_entry(cls, writer, entry):
writer.update_document(entry_id=entry.id,
user_id=entry.user.id,
username=entry.user.name,
title=entry.title,
content=entry.content)
@classmethod
def insert_user(cls, writer, user):
pass # nothing, user doesn't have entries yet
@classmethod
def insert_entry(cls, writer, entry):
writer.add_document(entry_id=entry.id,
user_id=entry.user.id,
username=entry.user.name,
title=entry.title,
content=entry.content)
@classmethod
def delete_user(cls, writer, user):
pass # TODO: delete all users entries
@classmethod
def delete_entry(cls, writer, entry):
writer.delete_by_term('entry_id', entry.id)
Entry.query. whooshee_search('chuck norris'). order_by(Entry.id.desc()). all()
Entry.query.join(User). whooshee_search('chuck norris'). order_by(Entry.id.desc()). all()
Entry.query.join(User). whooshee_search('chuck norris', order_by_relevance=25). all()