【问题标题】:"Query has no attribute 'whoosh_search" AttributeError in Flask + Whoosh + SQLAlchemyFlask + Whoosh + SQLAlchemy中的“查询没有属性'whoosh_search”AttributeError
【发布时间】:2016-09-08 22:50:57
【问题描述】:

我在使用 Flask-WhooshAlchemy 和 Flask-Whooshee 时遇到了一个类似于这些问题的奇怪问题:

我从 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


【解决方案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 添加到 whoosh 索引?一种解决方案是重新插入所有行;这个post 似乎给出了指示,但也声称没有维护whooshalchemy。

【讨论】:

    【解决方案2】:

    我一直只是使用 Flask-SQLAlchemy,而不是直接使用 SQLAlchemy。

    使用 Flask-SQLAlchemy,我会像这样查询 Restaurant 表:

      Restaurant.query.whoosh_search('foo')
    

    SQLAlchemy docs 看来,您需要执行以下操作:

      q = session.query(Restaurant)
      result = q.whooshee_search(query).all()
    

    【讨论】:

    • 嗨 finnurtorfa,正如您在我的示例 (views.py) 中所见,这正是我正在做的。谢谢你把我引向 Flask-SQLAlchemy,我没有意识到语法是不同的,所以我对人们在示例中使用的不同类型的语法感到困惑。
    • 其实我的例子和你的有细微的差别。 q = session.query(Restaurant) 与 q = session.query()。但我不确定这是否会为你解决问题
    猜你喜欢
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 2021-08-31
    相关资源
    最近更新 更多