1

I am using Flask (python), creating whooshe search, I have followed all steps from this link, also tried from other links too, but every time at end I came to this error:

results = BlogPost.query.whoosh_search('cool')
AttributeError:'BaseQuery' object has no attribute 'whoosh_search' 

Here is my model code:

class BlogPost(db.Model):
    __tablename__ = 'blogpost'
    __searchable__ = ['title', 'content']
    __analyzer__ = StemmingAnalyzer()
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.Unicode) # Indexed fields are either String,
    content = db.Column(db.Text) # Unicode, or Text
    created = db.Column(db.DateTime, default=datetime.utcnow)

I am having error on this:

@app.route('/search')
def search():
    results = BlogPost.query.whoosh_search('cool')
return results
Fejs
  • 2,734
  • 3
  • 21
  • 40
Muhammad Aadil Banaras
  • 1,134
  • 1
  • 11
  • 21

2 Answers2

2

I have also seen this error.Then I found the error is in models.py.You should add the whooshalchemy.whoosh_index(app, BlogPost) at the end of the model code.

大佬安
  • 21
  • 2
0

I ran in to a similar issue if anyone falls down thsi rabbit hole.

In my case, i found that in my models.py i was importing from sqlalchemy and using the declarative_base as a base for each model. ie:

from sqlalchemy import Column, Integer, DateTime, Text, Boolean
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Book(Base):

while in my routes.py I was importing flask-sqlalchemy where you use db.Model as the base:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
db.init_app(app)

class Book(db.Model):

There have been some similar answers on some other posts as well but it looks like when using flask-whooshalchemy or its similar variants (whooshalchemy3, whooshalchemyplus) using db.Model as the base is the correct way to init the model to have the appropriate attributes.

hopefully that helps someone down the road.