当前位置:网站首页>Using flask_ Whooshalchemyplus Jieba realizes global search of flask
Using flask_ Whooshalchemyplus Jieba realizes global search of flask
2022-07-06 14:36:00 【Xiaobai aYuan】
Preface
because flask_SqlAlchemy Full text search is not yet supported , So use flask_whooshalchemyplus To achieve
for example : With the development of artificial intelligence , Machine learning is becoming more and more important , A lot of people have turned on learning machine learning , This paper introduces the basic content of machine learning .
Whoosh It is a class library containing classes and methods used to index text and search according to the index .
install flask_whooshalchemyplus jieba
pip install flask_whooshalchemyplus
Version is 0.7.6
If directly pip When the installation is unsuccessful, you can take the following methods
git clone https://github.com/Revolution1/Flask-WhooshAlchemyPlus.git
First the Flask-WhooshAlchemyPlus Clone to a local project and
cd Flask-WhooshAlchemyPlus Enter the price asking folder that clones to the local site, and then
pip install flask_whooshalchemyplus
pip install SQLAlchemy==1.3.24
We need to pay attention to SQLAlchemy edition ,1.4 There will be problems
jieba
jieba It is an excellent third-party Chinese Thesaurus , It can avoid problems caused by Chinese coding
pip install jieba
Full text search is equivalent to adding full-text index to the project
therefore flask The location of the full-text index should be added to the project configuration file
Project profile config.py
import os
basedir = os.path.abspath(os.path.dirname(__file__))
# Index storage location
WHOOSH_BASE = os.path.join(basedir, 'WHOOSH_BASE_INDEX')init.py Initialization in factory function
import flask_whooshalchemyplus
#create_app() A function is a factory function of a program , Take a parameter , Is the configuration name used by the program
def create_app(config_name):
app = Flask(__name__)
# After the program is created and configured , You can initialize the extension . Call on the previously created extension object init_app() Initialization can be completed The process .
db.init_app(app)
with app.app_context():
flask_whooshalchemyplus.index_all(app)
# The factory function returns the created program example
return appChanges in model classes
In the model class, specify the fields that need to be used as full-text indexes , And change the default word breaker to jieba Chinese word segmentation in
Join Chinese search support
__analyzer__ = ChineseAnalyzer()
For example, put desc And title As full-text index
from jieba.analyse.analyzer import ChineseAnalyzer
# Indicate the fields that are full-text indexes
__searchable__ = ['title', 'desc']
# Change the default word breaker to jieba The Chinese word segmentation device of
__analyzer__ = ChineseAnalyzer()Achieve the effect in the view
If new data is added to the database , Remember to update the index with the following code
db.session.add(course)
db.session.commit()
flask_whooshalchemyplus.index_one_model(Course)Write an interface to realize the use of full-text indexing
class SearchCourse(Resource):
"""
Get all the information including the search content
"""
def get(self):
parser = reqparse.RequestParser()
# Get the data from the front end
parser.add_argument('q')
args = parser.parse_args()
q = args.get('q')
# Use whoosh_search(), Use the data transmitted from the front end as the search keywords
course_list = Course.query.whoosh_search(q).all()
# Return the obtained data
return marshal(course_list, course_fields)Here you can basically realize full-text indexing and full-text search in Chinese
边栏推荐
- 后台登录系统,JDBC连接数据库,做小案例练习
- [pointer] solve the last person left
- 数据库多表链接的查询方式
- Fundamentals of digital circuit (V) arithmetic operation circuit
- 函数:求两个正数的最大公约数和最小公倍
- 【指针】八进制转换为十进制
- C language learning summary (I) (under update)
- 刷视频的功夫,不如看看这些面试题你掌握了没有,慢慢积累月入过万不是梦。
- Internet Management (Information Collection)
- Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
猜你喜欢

How to earn the first pot of gold in CSDN (we are all creators)

Internet Management (Information Collection)

Résumé des points de connaissance et des réponses aux exercices après la classe du chapitre 7 de Jia junping dans la huitième édition des statistiques

《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案

内网渗透之内网信息收集(二)

Wu Enda's latest interview! Data centric reasons

ES全文索引

循环队列(C语言)

Low income from doing we media? 90% of people make mistakes in these three points

四元数---基本概念(转载)
随机推荐
使用 flask_whooshalchemyplus jieba实现flask的全局搜索
Intranet information collection of Intranet penetration (5)
循环队列(C语言)
Internet Management (Information Collection)
《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
数字电路基础(四) 数据分配器、数据选择器和数值比较器
[pointer] the array is stored in reverse order and output
Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
《英特尔 oneAPI—打开异构新纪元》
【指针】使用插入排序法将n个数从小到大进行排列
《統計學》第八版賈俊平第七章知識點總結及課後習題答案
“人生若只如初见”——RISC-V
链队实现(C语言)
《统计学》第八版贾俊平第四章总结及课后习题答案
图书管理系统
【指针】删除字符串s中的所有空格
函数:计算字符串中大写字母的个数
Keil5-MDK的格式化代码工具及添加快捷方式
[pointer] solve the last person left
Sentinel overall workflow