flask 貌似不多全文搜索的插件,有一個 Flask-WhooshAlchemy , 但試了幾回都用不了,因此參考 Flask-WhooshAlchemy 本身寫了一個python
插件基於 whoosh,純 python 編寫,使用上很簡單git
from flask_msearch import Search [...] search = Search() search.init_app(app) # models.py class Post(db.Model): __tablename__ = 'post' __searchable__ = ['title', 'content'] # views.py @app.route("/search") def w_search(): keyword = request.args.get('keyword') results = search.whoosh_search(Post,query=keyword,fields=['title'],limit=20) return ''
若是要對已存在的數據建立索引(所有)github
# 建立 search.create_index() # 更新 search.create_index(update=True) # 刪除 search.create_index(delete=True)
若是隻想要爲指定的表建立索引flask
search.create_index(Model)
好比使用jieba的中文分詞app
from jieba.analyse import ChineseAnalyzer search = Search(analyzer=ChineseAnalyzer())
WHOOSH_BASE = 'whoosh_index' WHOOSH_ENABLE = True
項目地址:https://github.com/honmaple/f...
演示: demopost