How to do full text search in flask sqlalchemy with an sqlite database

Viewed 208

I am trying to do a simple full-text search in flask with flask sqlalchemy, and until now I have found:

  • Flask-WhooshAlchemy. Only works in python2. It's really simple and "it just works" but I am using Python3.
  • Flask-WhooshAlchemy3. Just can't get it to work. Instructions at github ( https://github.com/blakev/Flask-WhooshAlchemy3 ) don't show full instructions. I was able to hobble it together, have it indexing but get an 'AttributeError: 'BaseQuery' object has no attribute 'search'' error (that's meant to be resolved) and can't progress.

Using the instructions for Flask-WhooshAlchemy3 I got this so far


from datetime import datetime

import flask_sqlalchemy
import flask_whooshalchemy3
from whoosh.analysis import StemmingAnalyzer

db = flask_sqlalchemy.SQLAlchemy()


class BlogPost(db.Model):
    __tablename__ = 'posts'
    __searchable__ = ['title', 'content', 'summary']  # indexed fields
    __analyzer__ = StemmingAnalyzer()

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255), unique=True)
    content = db.Column(db.Text(32 * 1024))
    summary = db.Column(db.String(1024))
    created = db.Column(db.DateTime, default=datetime.utcnow)

@app.route('/posts')
def posts():
    num_posts = min(request.args.get('limit', 10), 50)
    query = request.args.get('q', '')
    results = BlogPost.query.search(query, limit=num_posts)

and this is the error I get: AttributeError: 'BaseQuery' object has no attribute 'search'

Am i missing some configuration, or is there a better option than Flask-WhooshAlchemy3

0 Answers
Related