Search code examples
pythonflasksqlalchemywhoosh

difficulty setting up and using Flask_whooshalchemy


I'm currently working on a flask web app with a large database which meant that I had to discard the code I had initially written for WhooshAlchemy. The first time I tested it I got the following error.

   Post.query.whoosh_search('phone').all()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Abdul\Anaconda3\lib\site-packages\flask_whooshalchemy.py", line 103, in whoosh_search
    if not isinstance(query, unicode):
NameError: name 'unicode' is not defined

does this have anything to do with python 3 renaming Unicode to str a couple of years ago or is it something else? I tried changing the Unicode into str but that did not seem to fix the problem? any useful input would be appreciated.


Solution

  • Disclaimer: Flask_whooshalchemy is no longer supported and does not work with python 3.0+.

    Solution: you either have to use a custom solution or use Flask m-search which works just fine.

    Here's a link to flask m-search just in case anybody was facing the same problem.

    A quick example would be like the following:

    from flask import Flask
    from flask_sqlalchemy import SQLAlchemy
    from flask_msearch import Search
    
    app = Flask(__name__)
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
    db = SQLAlchemy(app)
    search = Search()
    search.init_app(app)
    
    class User(db.Model):
        __searchable__ = ['id', 'username', 'email']
        id = db.Column(db.Integer, primary_key=True)
        username = db.Column(db.String(80), unique=True, nullable=False)
        email = db.Column(db.String(120), unique=True, nullable=False)
    
        def __repr__(self):
            return '<User %r>' % self.username