我对Flask-WhooshAlchemy以及类似这些问题的Flask-Whooshee有一个奇怪的问题:
我从Flask-WhooshAlchemy开始。我知道Whoosh只适用于新索引的项目,而不适用于预先存在的项目,因此我将所有内容重新导入到我的数据库中。这没有用,所以我从Stackoverflow问题中运行了gist代码:How flask-whooshalchemy index data imported manually?。
我对他的代码做了一点改动。由于model.query对我不起作用(我假设不推荐使用查询的方式,但这只是猜测),我连接引擎并以这种方式调用它。在任何一种情况下,它似乎都有效,我生成了一个健康大小的飞快指数。
我已经完成了将其放在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
答案 0 :(得分:1)
我遇到了同样的错误,然后当我重新阅读文档时,我注意到创建帖子是能够进行whoosh_search的步骤之一:
例如,表格需要先由whoosh(docs)索引:
Let’s create a post:
db.session.add(
BlogPost(title='My cool title', content='This is the first post.')
); db.session.commit()
会话提交后,我们的新BlogPost会被编入索引。同样,如果删除帖子,它将从Whoosh索引中删除。
有没有办法将现有的db.table添加到嗖嗖索引?一种解决方案是重新插入所有行;这post似乎给出了指示,但也声称没有维持whooshalchemy。
答案 1 :(得分:0)
我一直只使用Flask-SQLAlchemy,但不是直接使用SQLAlchemy。
使用Flask-SQLAlchemy,我会像这样查询Restaurant表:
Restaurant.query.whoosh_search('foo')
从SQLAlchemy docs开始,您需要执行以下操作:
q = session.query(Restaurant)
result = q.whooshee_search(query).all()