我的配置
工厂函数中
def create_app(...):
...
import flask_whooshalchemyplus
flask_whooshalchemyplus.init_app(app)
from app.models import Post
whoosh_index(app,Post)
body=db.Column(db.Text)
我的模型:
class Post(db.Model):
__searchable__=['body']
__analyzer__ = ChineseAnalyzer()
__tablename__='posts'
我的视图函数:
@ser.route('/search',methods=['POST'])
@login_required
def search():
page=request.args.get("page",1,type=int)
form=g.search_form
if not form.validate_on_submit():
flash("Empty result.")
return redirect(url_for('main.index'))
post=Post.query.whoosh_search(form.search.data)
pagination=post.paginate(page,per_page=int(current_app.config['FLASKY_POSTS_PER_PAGE']),error_out=False)
posts=pagination.items
return render_template('/ser/result.html',posts=posts,username=current_user.username,pagination=pagination)
把配置whooshalchemyplus前的POST都删除了,新建的POST英文可以正常搜索,中文只能支持全匹配搜索。
db.session.add(Post(body=u'天气一般般'))
db.session.commit()
a=Post.query.whoosh_search(u'天气').fisrt()
a
[]
a=Post.query.whoosh_search(u'天气一般般').fisrt()
<app.modls........>
该怎么办,新手求助
重置了数据库也无效,数据库用的SQLite
如果用的是 postgresql 数据库的话,检查一下你的数据库的编码是
UTF-8吗?可以在数据库的 shell 中通过 \l 查看数据库信息:数据库 shell 中可以进行中文搜索不?可以通过下面的 sql 检查:
上面 db1 是 UTF-8 所以支持中文搜索,
db2 是 SQL_ASCII 不支持中文搜索