4

I have a strange problem with Flask-WhooshAlchemy as well as Flask-Whooshee that resembles these issues:

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
Community
  • 1
  • 1
szxk
  • 1,769
  • 18
  • 35

2 Answers2

1

I was getting the same error, and then when i re-read the docs, i noticed that creating a post is one of the steps to being able to whoosh_search:

For example, the table needs to be indexed by whoosh first (docs):

Let’s create a post:

db.session.add(
    BlogPost(title='My cool title', content='This is the first post.')
); db.session.commit()

After the session is committed, our new BlogPost is indexed. Similarly, if the post is deleted, it will be removed from the Whoosh index.

Is there a way to add existing db.table to a whoosh index? One solution is to re-insert all rows; this post seems to give directions, but also claims that whooshalchemy is not maintained.

Community
  • 1
  • 1
Chet Meinzer
  • 1,691
  • 2
  • 21
  • 35
0

I have always just used Flask-SQLAlchemy, but not SQLAlchemy directly.

With Flask-SQLAlchemy I would query the Restaurant table like so:

  Restaurant.query.whoosh_search('foo')

From the SQLAlchemy docs, it looks like you need to do something like this:

  q = session.query(Restaurant)
  result = q.whooshee_search(query).all()
  • Hi finnurtorfa, as you can see in my example (views.py), that is exactly what I'm doing. Thank you for pointing me toward Flask-SQLAlchemy though, I didn't realize the syntax was different, and so I've been confused at the different types of syntax that people use in examples. – szxk Jul 13 '14 at 04:40
  • Actually, there is a minor difference between my example and yours. q = session.query(Restaurant) vs. q = session.query(). But I'm not sure if that will solve things for you though – finnurtorfa Jul 13 '14 at 07:28