当前位置:网站首页>使用 flask_whooshalchemyplus jieba实现flask的全局搜索
使用 flask_whooshalchemyplus jieba实现flask的全局搜索
2022-07-06 09:24:00 【小白阿远】
前言
因为flask_SqlAlchemy尚未支持全文搜索,所以使用flask_whooshalchemyplus来实现
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
Whoosh是一个用来索引文本并能根据索引搜索的的包含类和方法的类库。
安装 flask_whooshalchemyplus jieba
pip install flask_whooshalchemyplus
版本为0.7.6
若直接pip安装不成功的时候可以采取以下方法
git clone https://github.com/Revolution1/Flask-WhooshAlchemyPlus.git
先将Flask-WhooshAlchemyPlus克隆到本地项目中然后
cd Flask-WhooshAlchemyPlus进入到克隆到本地的问价夹中再进行
pip install flask_whooshalchemyplus
pip install SQLAlchemy==1.3.24
需要注意SQLAlchemy版本,1.4会出现问题
jieba
jieba是优秀的第三方中文词库,可以避免因为中文编码出现的问题
pip install jieba
全文搜索相当于给项目加上了全文索引
所以flask项目配置文件中应该加上全文索引的位置
项目配置文件config.py
import os
basedir = os.path.abspath(os.path.dirname(__file__))
# 索引存放位置
WHOOSH_BASE = os.path.join(basedir, 'WHOOSH_BASE_INDEX')
init.py工厂函数中的初始化
import flask_whooshalchemyplus
#create_app() 函数就是程序的工厂函数,接受一个参数,是程序使用的配置名
def create_app(config_name):
app = Flask(__name__)
#程序创建并配置好后,就能初始化扩展了。在之前创建的扩展对象上调用 init_app() 可以完成初始化 过程。
db.init_app(app)
with app.app_context():
flask_whooshalchemyplus.index_all(app)
#工厂函数返回创建的程序示例
return app
模型类的变化
在模型类里便写明需要作为全文索引的字段,以及将默认的分词器改为jieba中的中文分词器
加入中文搜索支持
__analyzer__ = ChineseAnalyzer()
例如将表中的desc及title作为全文索引
from jieba.analyse.analyzer import ChineseAnalyzer
# 指明作为全文索引的字段
__searchable__ = ['title', 'desc']
# 将默认的分词器改为jieba的中文分词器
__analyzer__ = ChineseAnalyzer()
在视图中实现效果
如果数据库里增加了新的数据的话,切记一定要使用如下代码更新索引
db.session.add(course)
db.session.commit()
flask_whooshalchemyplus.index_one_model(Course)
写一个接口来实现全文索引的使用
class SearchCourse(Resource):
"""
获取包含搜索内容的全部信息
"""
def get(self):
parser = reqparse.RequestParser()
# 获取前端传过来的数据
parser.add_argument('q')
args = parser.parse_args()
q = args.get('q')
# 使用whoosh_search(), 将前端传过来的数据作为搜索的关键字进行搜索
course_list = Course.query.whoosh_search(q).all()
# 将得到的数据返回出去
return marshal(course_list, course_fields)
到这里基本上就可以实现全文索引并且可以使用中文实现全文搜索
边栏推荐
- 《英特尔 oneAPI—打开异构新纪元》
- Experiment 4 array
- 7-9 make house number 3.0 (PTA program design)
- List and data frame of R language experiment III
- 《统计学》第八版贾俊平第三章课后习题及答案总结
- 记一次edu,SQL注入实战
- Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
- 内网渗透之内网信息收集(四)
- HackMyvm靶机系列(3)-visions
- 7-11 mechanic mustadio (PTA program design)
猜你喜欢
Markdown font color editing teaching
关于超星脚本出现乱码问题
Lintcode logo queries the two nearest saplings
Hackmyvm target series (2) -warrior
Xray and Burp linked Mining
Statistics 8th Edition Jia Junping Chapter 14 summary of index knowledge points and answers to exercises after class
JVM memory model concept
Middleware vulnerability recurrence Apache
《统计学》第八版贾俊平第八章假设检验知识点总结及课后习题答案
sqqyw(淡然点图标系统)漏洞复现和74cms漏洞复现
随机推荐
WEB漏洞-文件操作之文件包含漏洞
内网渗透之内网信息收集(三)
Detailed explanation of network foundation routing
Overview of LNMP architecture and construction of related services
List and data frame of R language experiment III
Interpretation of iterator related "itertools" module usage
XSS unexpected event
Hackmyvm target series (1) -webmaster
《统计学》第八版贾俊平第七章知识点总结及课后习题答案
"Gold, silver and four" job hopping needs to be cautious. Can an article solve the interview?
7-8 7104 Joseph problem (PTA program design)
Experiment 7 use of common classes (correction post)
XSS (cross site scripting attack) for security interview
Experiment 9 input and output stream (excerpt)
7-7 7003 combination lock (PTA program design)
Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
Uibutton status exploration and customization
Attack and defense world misc practice area (GIF lift table ext3)
Record an API interface SQL injection practice
7-4 hash table search (PTA program design)