I have a strange problem with Flask-WhooshAlchemy as well as Flask-Whooshee that resembles these issues:
- https://github.com/gyllstromk/Flask-WhooshAlchemy/issues/13
- https://github.com/miguelgrinberg/flasky/issues/8
I started off with Flask-WhooshAlchemy. I know that Whoosh only works on newly indexed items, and not on pre-existing items, so I reimported everything into my database. That didn't work, so I ran the gist code from this Stackoverflow question: How flask-whooshalchemy index data imported manually?.
I made one slight change to his code. Since model.query didn't work for me (I assume that style of making queries is deprecated, but that is just guesswork), I hooked up an engine and called it that way. In either case, it seemed to have worked, and I generated a healthy-sized Whoosh index.
I have done the step where I place this at the bottom of my schema.py file (some people call it models.py):
whooshalchemy.whoosh_index(app, Restaurant)
and I put in the list of items that are searchable inside the class definition. I also found this link describing some shortcomings about overloading "query" the way the developer did: http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvi-debugging-testing-and-profiling/page/3. He wrote some code that patched the bug--https://raw.githubusercontent.com/miguelgrinberg/Flask-WhooshAlchemy/1e17350ea600e247c0094cfa4ae7145f08f4c4a3/flask_whooshalchemy.py -- and I tried installing that too, but reverted when it didn't help.
Here is the traceback:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/<omitted>/App/api/app/views.py", line 96, in instant
result = q.whoosh_search(query).all()
AttributeError: 'Query' object has no attribute 'whoosh_search'
I tried it with Flask-Whooshee, which looked very, very similar, and I am getting the same error.
Here is the code in question (after switching to Flask-Whooshee, but I've left the Flask-WhooshAlchemy code commented out):
views.py:
@app.route('/search')
def search():
query = request.args.get('query', '', type=str)
q = session.query()
result = q.whooshee_search(query).all()
#result = q.whoosh_search(query).all()
return Response(json.dumps(result), mimetype='text/json')
schema.py:
from app import app
from flask.ext.whooshee import Whooshee
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, Float, String, Date
# import flask.ext.whooshalchemy as whooshalchemy
from settings import WHOOSH_BASE
Base = declarative_base()
whooshee = Whooshee(app)
@whooshee.register_model('db_name', 'db_addr', 'google_name', 'yelp_name',
'yelp_address')
class Restaurant(Base):
__tablename__ = 'restaurant_indexed'
#__searchable__ = ['db_name', 'db_addr',
# 'google_name', 'yelp_name', 'yelp_address']
restaurant_id = Column(Integer, primary_key=True)
google_id = Column(String)
db_name = Column(String)
db_addr = Column(String)
# whooshalchemy.whoosh_index(app, Restaurant)
I commented out the lines that were previously used in the Flask-WhooshAlchemy version of the code.
My init.py looks like this:
from flask import Flask
app = Flask(__name__)
app.config['WHOOSH_BASE'] = '/home/me/path/to/whoosh/dir'
from app import views