
本文将介绍如何在 Flask 应用中集成全文搜索引擎,以优化基于 Flask-SQLAlchemy 的产品搜索功能。如摘要所述,当需要处理复杂的搜索条件,例如同时搜索多个品牌、类别等,手动构建搜索逻辑不仅复杂,而且性能难以保证。因此,采用全文搜索引擎是更高效的选择。
传统的数据库查询方式,例如使用 LIKE 语句进行模糊匹配,在数据量较大时性能会显著下降。而全文搜索引擎,例如 Elasticsearch,通过对文本数据进行预处理(例如分词、索引),能够快速地进行文本搜索,并支持复杂的搜索条件,例如布尔搜索、模糊搜索、权重排序等。
以下步骤概述了如何在 Flask 应用中集成 Elasticsearch。
安装 Elasticsearch:
首先,需要在服务器上安装并运行 Elasticsearch。可以从 Elasticsearch 官网下载安装包,按照官方文档进行安装配置。
安装 Elasticsearch Python 客户端:
在 Flask 项目中,使用 Elasticsearch 的 Python 客户端与 Elasticsearch 服务器进行通信。可以使用 pip 安装:
pip install elasticsearch
创建 Elasticsearch 索引:
需要为产品数据创建一个 Elasticsearch 索引。索引定义了数据的结构和分析方式。可以使用 Elasticsearch 的 API 或者 Kibana 来创建索引。以下是一个示例索引配置:
系统特点:功能简洁实用。目前互联网上最简洁的企业网站建设系统!原创程序代码。非网络一般下载后修改的代码。更安全。速度快!界面模版分离。原创的分离思路,完全不同于其他方式,不一样的简单感受!搜索引擎优化。做了基础的seo优化。对搜索引擎更友好系统功能关于我们:介绍企业介绍类信息,可自由添加多个介绍栏目!资讯中心:公司或行业资讯类内容展示。可自由添加多个资讯内容!产品展示:支持类别设置,可添加产品图片
0
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"type": "standard",
"stopwords": "_english_"
}
}
}
},
"mappings": {
"properties": {
"brand": {
"type": "text",
"analyzer": "my_analyzer"
},
"title": {
"type": "text",
"analyzer": "my_analyzer"
},
"description": {
"type": "text",
"analyzer": "my_analyzer"
},
"collection": {
"type": "keyword"
},
"division": {
"type": "keyword"
},
"category": {
"type": "keyword"
},
"price": {
"type": "float"
},
"size_id": {
"type": "integer"
}
}
}
}注意: 上面的配置中,my_analyzer 使用了 standard 分析器,并移除了英文停用词。根据实际需求,可以选择更合适的分析器。keyword 类型适用于不需要分词的字段,例如 collection、division 和 category。
将数据同步到 Elasticsearch:
当产品数据发生变化时(例如新增、修改、删除),需要将数据同步到 Elasticsearch 索引。可以使用 Flask-SQLAlchemy 的事件监听器来实现自动同步。
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from elasticsearch import Elasticsearch
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://user:password@host:port/database'
db = SQLAlchemy(app)
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
class Product(db.Model):
id = db.Column(db.Integer, primary_key=True)
brand = db.Column(db.String(255))
title = db.Column(db.String(255))
description = db.Column(db.Text)
collection = db.Column(db.String(255))
division = db.Column(db.String(255))
category = db.Column(db.String(255))
price = db.Column(db.Float)
size_id = db.Column(db.Integer)
def after_insert(mapper, connection, target):
es.index(index='products', doc_type='_doc', id=target.id, body={
'brand': target.brand,
'title': target.title,
'description': target.description,
'collection': target.collection,
'division': target.division,
'category': target.category,
'price': target.price,
'size_id': target.size_id
})
def after_update(mapper, connection, target):
es.update(index='products', doc_type='_doc', id=target.id, body={
'doc': {
'brand': target.brand,
'title': target.title,
'description': target.description,
'collection': target.collection,
'division': target.division,
'category': target.category,
'price': target.price,
'size_id': target.size_id
}
})
def after_delete(mapper, connection, target):
es.delete(index='products', doc_type='_doc', id=target.id)
from sqlalchemy import event
event.listen(Product, 'after_insert', after_insert)
event.listen(Product, 'after_update', after_update)
event.listen(Product, 'after_delete', after_delete)注意: 上面的代码片段展示了如何使用 SQLAlchemy 的事件监听器,在 Product 模型新增、更新、删除后,自动同步数据到 Elasticsearch。
执行搜索:
使用 Elasticsearch 的 Python 客户端执行搜索。可以将用户输入的搜索条件转换为 Elasticsearch 的查询语句。
def search_products(query):
search_results = es.search(index='products', body={
'query': {
'multi_match': {
'query': query,
'fields': ['brand', 'title', 'description'],
'fuzziness': 'AUTO'
}
}
})
return search_results['hits']['hits']注意: 上面的代码片段使用了 multi_match 查询,在 brand、title 和 description 字段中搜索用户输入的关键词。fuzziness 参数允许一定的拼写错误。
通过集成 Elasticsearch,可以显著提升 Flask-SQLAlchemy 应用的产品搜索性能,并支持复杂的搜索条件。虽然集成过程需要一定的学习成本,但带来的收益是巨大的。希望本文能够帮助你更好地理解和应用全文搜索引擎。
以上就是Flask-SQLAlchemy 产品搜索优化:集成全文搜索引擎的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号