Flask + Whoosh + SQLAlchemy中的AttributeError:查询没有属性'whoosh_search
我在使用Flask-WhooshAlchemy和Flask-Whooshee时遇到了一个奇怪的问题,跟这些问题有点像:
- https://github.com/gyllstromk/Flask-WhooshAlchemy/issues/13
- https://github.com/miguelgrinberg/flasky/issues/8
我最开始使用的是Flask-WhooshAlchemy。我知道Whoosh只对新添加的项目有效,而对已经存在的项目无效,所以我把所有数据重新导入到数据库里。但这样做没有解决问题,于是我运行了这个Stackoverflow问题中的代码:如何让flask-whooshalchemy索引手动导入的数据?。
我对他的代码做了一个小改动。因为model.query对我不起作用(我猜这种查询方式可能已经过时了,但这只是我的猜测),所以我连接了一个引擎,用这种方式调用。在任何情况下,这似乎都有效,我生成了一个相当不错的Whoosh索引。
我在schema.py文件的底部添加了这段代码(有些人称它为models.py):
whooshalchemy.whoosh_index(app, Restaurant)
然后我在类定义中放入了可以搜索的项目列表。我还找到了一些链接,描述了开发者在重载“query”时的一些不足之处:http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvi-debugging-testing-and-profiling/page/3。他写了一些代码来修复这个bug——https://raw.githubusercontent.com/miguelgrinberg/Flask-WhooshAlchemy/1e17350ea600e247c0094cfa4ae7145f08f4c4a3/flask_whooshalchemy.py——我也尝试安装了这个,但没有帮助,所以我又恢复了原来的代码。
这是错误追踪信息:
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'
我也试过Flask-Whooshee,它看起来非常相似,但我遇到的错误是一样的。
这是相关的代码(在切换到Flask-Whooshee之后,我把Flask-WhooshAlchemy的代码注释掉了):
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)
我注释掉了之前在Flask-WhooshAlchemy版本中使用的代码行。
我的init.py看起来是这样的:
from flask import Flask
app = Flask(__name__)
app.config['WHOOSH_BASE'] = '/home/me/path/to/whoosh/dir'
from app import views
2 个回答
我一直以来都是使用Flask-SQLAlchemy,而不是直接使用SQLAlchemy。
用Flask-SQLAlchemy查询餐厅表格的方法是这样的:
Restaurant.query.whoosh_search('foo')
根据SQLAlchemy的文档,看起来你需要这样做:
q = session.query(Restaurant)
result = q.whooshee_search(query).all()
我之前也遇到过同样的错误,后来我重新看了一下文档,发现创建一个帖子是使用whoosh_search的步骤之一。
比如说,表格需要先通过whoosh进行索引(文档):
Let’s create a post:
db.session.add(
BlogPost(title='My cool title', content='This is the first post.')
); db.session.commit()
在会话提交之后,我们的新博客帖子就会被索引。同样,如果帖子被删除,它也会从Whoosh的索引中移除。
有没有办法把现有的数据库表添加到whoosh索引中呢?一种解决办法是重新插入所有的行;这个帖子似乎提供了一些指引,但也提到whooshalchemy这个库没有在维护了。