我正在开发一个Python-Flask应用程序,我想实现一个全文搜索,这对于Flask-WhooshAlchemy非常简单,至少我是这么认为的。这让我抓狂,因为显然,没有任何理由说明这是不行的。 这是我的 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)
我自动并手动运行。
>>> wa.whoosh_index(app, Politic)
>>> Out[69]: FileIndex(FileStorage('whoosh/Politic'), 'MAIN')
运行此查询时仍然没有出现:
>>> Politic.query.whoosh_search('Pedro').all()
>>> Out[56]: []
我很感兴趣所以我检查了我的whoosh_index以查看详细信息:
>>> ix = open_dir('whoosh_index/Politic')
>>> ix.schema
>>> Out[73]: <Schema: ['completeName', 'idPolitician', 'publicName']>
更深:
>>> from whoosh.query import Every
>>> results = ix.searcher().search(Every('content'))
结果又是空的..
>>> for result in results:
>>> print result['idPoltician']
我不知道我在哪里搞乱,或者对此有什么困难。 之前有没有人遇到过这个问题并弄清楚是什么问题?
任何帮助都会很大。我在这里度过了很长时间