【发布时间】:2016-09-08 22:50:57
【问题描述】:
我在使用 Flask-WhooshAlchemy 和 Flask-Whooshee 时遇到了一个类似于这些问题的奇怪问题:
- https://github.com/gyllstromk/Flask-WhooshAlchemy/issues/13
- https://github.com/miguelgrinberg/flasky/issues/8
我从 Flask-WhooshAlchemy 开始。我知道 Whoosh 只适用于新索引的项目,而不适用于预先存在的项目,所以我将所有内容重新导入我的数据库。那没有用,所以我从这个 Stackoverflow 问题中运行了要点代码:How flask-whooshalchemy index data imported manually?。
我对他的代码做了一点改动。由于 model.query 对我不起作用(我认为不推荐使用这种查询方式,但这只是猜测),我连接了一个引擎并以这种方式调用它。无论哪种情况,它似乎都有效,并且我生成了一个健康大小的 Whoosh 索引。
我已经完成了将它放在我的 schema.py 文件底部的步骤(有些人称之为 models.py):
whooshalchemy.whoosh_index(app, Restaurant)
然后我将可在类定义中搜索的项目列表放入其中。我还发现这个链接描述了开发人员所做的方式重载“查询”的一些缺点:http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvi-debugging-testing-and-profiling/page/3。他写了一些代码修复了这个错误——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
【问题讨论】:
-
您找到解决方案了吗?
标签: python flask flask-sqlalchemy whoosh flask-whooshee