简介
本文介绍如何在flask框架中实现中文全文搜索,本博客也已经支持中文全文搜索,欢迎在导航栏输入框中输入“tensorflow”、“算法”等关键词进行尝试。
依赖模块
- jieba
- whooshalchemyplus
安装依赖
pip install jieba pip install flask_whooshalchemyplus
起始文件
from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) # 配置 sqlalchemy 数据库驱动://数据库用户名:密码@主机地址:端口/数据库?编码 app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:passwd@localhost:3306/blog?charset=utf8' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True # set the location for the whoosh index app.config['WHOOSH_BASE'] = 'path/to/whoosh/base' # 初始化 db = SQLAlchemy(app)
model文件
from jieba.analyse.analyzer import ChineseAnalyzer import flask_whooshalchemyplus class BlogPost(db.Model): __tablename__ = 'blogpost' __searchable__ = ['title', 'content'] # these fields will be indexed by whoosh __analyzer__ = ChineseAnalyzer() # configure analyzer; defaults to # StemmingAnalyzer if not specified id = app.db.Column(app.db.Integer, primary_key=True) title = app.db.Column(app.db.Unicode) # Indexed fields are either String, content = app.db.Column(app.db.Text) # Unicode, or Text created = db.Column(db.DateTime, default=datetime.datetime.utcnow) flask_whooshalchemyplus.init_app(app) # initialize
建立索引
旧博客批量建索引
from flask_whooshalchemyplus import index_all index_all(app)
新博客单独建索引
db.session.add(BlogPost(title='My cool title', content='This is the first post.')) db.session.commit()
开始搜索
results = BlogPost.query.whoosh_search(query).all()