Flask-WhooshAlchemyPlus 搜索

夏侯鹏
2023-12-01

一、下载flask_whooshalchemyplus和jieba分词

$ git clone https://github.com/Revolution1/Flask-WhooshAlchemyPlus.git
$ cd Flask-WhooshAlchemyPlus && python setup.py install
pip install jieba
注:直接pip install flask_whooshalchemyplus 由于版本问题会出现各种问题,所以下载git上的0.7.6版

二、在models.py和views.py文件中加入以下内容

models.py

from jieba.analyse.analyzer import ChineseAnalyzer
class News(BaseModel, db.Model):
    """新闻"""
    __tablename__ = "news"

     __searchable__ = ['title', 'content']
    
    __analyzer__ = ChineseAnalyzer()

views.py

from flask_whooshalchemyplus import index_all
from info import app
@index_blu.route('/news_search/', methods=["GET", "POST"])
def news_search():
	keywords = request.json.get('keywords', '')
	index_all(app)
	paginate = News.query.whoosh_search(keywords).filter(News.status == 0). \
	            order_by(News.create_time.desc()).paginate(page, per_page, False)

最后:搜索功能是挺强大,但是效率狠尴尬,有大神解决了请留言,谢谢。

 类似资料: