Flask-WhooshAlchemy returning empty

591 views Asked by At

I'm developing a Python-Flask application and I want to implement a Full Text Search which is pretty simple with Flask-WhooshAlchemy, atleast I thought so. This is driving me crazy because , apparently, there isnt any reason why this isnt working. This is my models.py

from project import app
from flask_sqlalchemy import SQLAlchemy
from werkzeug import generate_password_hash, check_password_hash
import flask_whooshalchemy as wa
import datetime
import flask_whooshalchemyplus

enable_search = True


db = SQLAlchemy(app)


class User(db.Model):
    __tablename__ = 'users'
    __searchable__ = ['firstname']
    uid = db.Column(db.Integer, primary_key=True)
    firstname = db.Column(db.String(100))
    lastname = db.Column(db.String(100))
    email = db.Column(db.String(100), unique=True)
    pwdhash = db.Column(db.String(150))
    urole = db.Column(db.String(80))

    # -----login requirements-----
    def is_active(self):
        # all users are active
        return True

    def get_id(self):
        # returns the user e-mail. not sure who calls this
        return self.email

    def is_authenticated(self):
        return True

    def is_anonymous(self):
        # False as we do not support annonymity
        return False

    def get_urole(self):
        return self.urole

    def __init__(self, firstname, lastname, email, password, urole):
        self.firstname = firstname.title()
        self.lastname = lastname.title()
        self.email = email.lower()
        self.set_password(password)
        self.urole = urole

    def set_password(self, password):
        self.pwdhash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.pwdhash, password)


proposal_politician_association = db.Table('politicsproposals',
                                           db.Column('idPolitician', db.Integer,
                                                     db.ForeignKey('politics.idPolitician')),
                                           db.Column('idProposal', db.Integer, db.ForeignKey('proposals.idProposal')))

wa.whoosh_index(app, User)
class Politic(db.Model):
    __tablename__ = 'politics'
    __searchable__ = ['publicName', 'completeName']

    idPolitician = db.Column(db.Integer, primary_key=True)
    publicName = db.Column(db.String(150))
    completeName = db.Column(db.String(300))
    startDate = db.Column(db.Date, default=datetime.datetime.utcnow)
    endDate = db.Column(db.Date, default=datetime.datetime.utcnow)
    flag = db.relationship('Flag', backref='politics', lazy='dynamic')

    def __init__(self, publicName, completeName, startDate, endDate):
        self.publicName = publicName.title()
        self.completeName = completeName.title()
        self.startDate = startDate.title()
        self.endDate = endDate.title()

    def __repr__(self):
        return '<Politic hello %r>' % (self.completeName)


wa.whoosh_index(app,Politic)

I ran it automatically and manually.

>>> wa.whoosh_index(app, Politic)
>>> Out[69]: FileIndex(FileStorage('whoosh/Politic'), 'MAIN')

Still nothing appeared when Running this query:

>>> Politic.query.whoosh_search('Pedro').all()
>>> Out[56]: []

I was intrigued so I checked my whoosh_index to see details:

>>> ix = open_dir('whoosh_index/Politic')
>>> ix.schema
>>> Out[73]: <Schema: ['completeName', 'idPolitician', 'publicName']>

and deeper:

>>> from whoosh.query import Every
>>> results = ix.searcher().search(Every('content'))

And the result is again, empty..

>>> for result in results:
>>>    print result['idPoltician']

I don't know where I'm messing or what is so hard about this. Did anyone had this problem before and figured out what was the problem?

Any help would be huge. I'm stucked here big time

0

There are 0 answers