我需要帮助使用 Flask-msearch 在 Flask 中实现关键字搜索

I need help implementing a keyword search in Flask with Flask-msearch

我正在使用 python 的 3.9 版并尝试创建关键字搜索,用户可以在其中输入关键字字符串,然后可以针对包含字符串的数据库查询该字符串。编译没有错误,一切都在运行,但查询结果不是我所期望的。 returns 以字符串形式进行查询,而不是从描述列返回包含与关键字相似词的所有行。

这是我的 init.py 文件:

import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_msearch import Search
...
app = Flask(__name__)
searched = Search(app)
searched.init_app(app)
searched.create_index(update=True)
MSEARCH_INDEX_NAME =  os.path.join(app.root_path,'msearch')
MSEARCH_ENABLE = True
from .search import search as search_blueprint
app.register_blueprint(search_blueprint)

return app

这是我的 models.py 文件

class Service (db.Model):
    __searchable__  = ['description']
    provider_id     = db.Column(db.Integer, db.ForeignKey('provider.provider_id'), nullable=False)
    service_id      = db.Column(db.Integer, nullable=False, primary_key=True)
    rating_avg      = db.Column(db.Float(precision=2), nullable=False)
    service_name    = db.Column(db.String(20), nullable=False)
    cost            = db.Column(db.Float(precision=2), nullable=False)
    description     = db.Column(db.String(255), nullable=False)
    category        = db.Column(db.String(20), nullable=False)
    
    def __repr__(self):
        return 'String representation of Service'

这是我的 search.py 文件:


search = Blueprint('search', __name__)

@search.route('/search')
def searching():
    return render_template('search.html')

@search.route('/search', methods=['POST'])
def searching_post():
    searchInput = request.form.get('searchInput')
    results = Service.query.msearch(searchInput, fields= ['description'])
    return render_template('result.html', results=results)

这是我的 search.html 模板文件:

{% extends "base.html" %}

{% block content %}
<div class="column is-8 is-offset-2">
    <h1 class ="title">
    Enter keywords and minimum rating
    </h1>
    <div class="box">
        <form method="POST" action="/search">
        <div class="field">
                    <div class="form-group">
                    <input class="input is-large" type="text" name="searchInput" placeholder=" " autofocus="">
                    </div>
                </div>
        <button class="button is-block is-info is-large is-fullwidth">Search</button>
         </form>
    </div>
</div>
{% endblock %}

这是我在搜索栏中搜索任何内容时得到的结果:

Results: SELECT service.provider_id AS service_provider_id, service.service_id AS service_service_id, service.rating_avg AS service_rating_avg, service.service_name AS service_service_name, service.cost AS service_cost, service.description AS service_description, service.category AS service_category FROM service WHERE (service.description LIKE '%' || ? || '%')!

msearch returns 一个 Query 对象,当您打印 results 变量时,您会看到它的字符串表示 - 底层 SQL 查询。

要获得实际结果,您需要 'execute' 查询,例如

results = Service.query.msearch(searchInput, fields= ['description']).all()

以下方法调用也会导致执行基础查询:

results = Service.query.msearch(searchInput, fields= ['description']).first()
results = Service.query.msearch(searchInput, fields= ['description']).one()
results = Service.query.msearch(searchInput, fields= ['description']).one_or_none()