简体   繁体   English

Flask,Python,SQLalchemy,SQLalchemy-searchable不返回匹配的文本

[英]Flask, Python, SQLalchemy, SQLalchemy-searchable doesn't return matching text

I am writing a web application in Python based on Flask, SQLalchemy, postgresql. 我正在用基于Flask,SQLalchemy,postgresql的Python编写Web应用程序。 I have created a database and am trying to implement a search tool. 我创建了一个数据库,并试图实现一个搜索工具。 I first referred to https://sqlalchemy-searchable.readthedocs.org/en/latest/installation.html#quickstart and tried the example given there. 我首先提到了https://sqlalchemy-searchable.readthedocs.org/en/latest/installation.html#quickstart并尝试了那里给出的例子。 The call to search() always returned None. 对search()的调用始终返回None。 When I searched for a similar issue on stackoverflow, I found the exact same question at Flask-Sqlalchemy + Sqlalchemy-searchable returning empty list . 当我在stackoverflow上搜索类似的问题时,我在Flask-Sqlalchemy + Sqlalchemy-searchable返回空列表时找到了完全相同的问题。 So I copied and pasted that code, word-to-word (with some relevant changes), but I still seem to have problem with search query returning None. 所以我复制并粘贴了这个代码,单词到单词(带有一些相关的变化),但我似乎仍然有问题,搜索查询返回无。 Here is the code: 这是代码:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy, BaseQuery
from sqlalchemy_searchable import SearchQueryMixin
from sqlalchemy_utils.types import TSVectorType
from sqlalchemy_searchable import make_searchable

app = Flask(__name__)
#app.config['SQLALCHEMY_DATABASE_URI'] =    'postgresql+psycopg2://usr:admin@localhost/dev'
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres:///dbuser_db1'
#app.config['SECRET_KEY'] = 'notreallyasecret'
db = SQLAlchemy(app)
make_searchable()


class ArticleQuery(BaseQuery, SearchQueryMixin):
    pass


class Article(db.Model):
    query_class = ArticleQuery
    __tablename__ = 'article'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode(255))
    content = db.Column(db.UnicodeText)
    search_vector = db.Column(TSVectorType('name', 'content'))

def test_db():
    article1 = Article(name=u'finland', content=u'finland')

    db.session.add(article1)
    db.session.commit()

    result1 = Article.query.search(u'finland').limit(5).all()
    result_blank = Article.query.search(u'').limit(5).all()
    result = Article.query.search(u'finland').first()
    print "RESULT: ", result1, result_blank, result


if __name__ == "__main__":
    test_db()

As per the conversation in the above stackoverflow link ( Flask-Sqlalchemy + Sqlalchemy-searchable returning empty list ), the person originating the question says the search queries started working when he/she re-instantiated the records with unicode values. 根据上述stackoverflow链接( Flask-Sqlalchemy + Sqlalchemy-searchable返回空列表 )中的对话,发起问题的人说,当他/她使用unicode值重新实例化记录时,搜索查询开始工作。 In my case I always used unicode for the records, as given in the code above, but it never seems to work for me. 在我的情况下,我总是使用unicode作为记录,如上面的代码所示,但它似乎永远不适合我。 The print statement above prints the following: RESULT: [] [< main .Article object at 0x7f9d4351bf90>, < main .Article object at 0x7f9d4342d090>, < main .Article object at 0x7f9d434ae550>] None 上面的print语句打印如下:RESULT:[] [< main .Article对象位于0x7f9d4351bf90>,< main .Article对象位于0x7f9d4342d090>,< main .Article对象位于0x7f9d434ae550>]无

Also as per the observation of the originator of the referred question, I too see that in the article table, the 'search_vector tsvector' column is completely empty even though there is data in the content and name columns. 另外,根据对引用问题的发起者的观察,我也看到在文章表中,即使内容和名称列中有数据,'search_vector tsvector'列也是完全空的。 Not sure if this is the root cause. 不确定这是否是根本原因。 I wonder if he/she is able to see the search_vector column populated after the search query started working. 我想知道他/她是否能够在搜索查询开始工作后看到填充的search_vector列。

dbuser_db1=> SELECT article.id AS article_id, article.name AS article_name, article.content AS article_content, article.search_vector AS article_search_vector FROM article ; dbuser_db1 => SELECT article.id AS article_id,article.name AS article_name,article.content AS article_content,art​​icle.search_vector AS article_search_vector FROM article;

article_id | article_id | article_name | article_name | article_content | article_content | article_search_vector article_search_vector

------------+--------------+-----------------+----------------------- ------------ + -------------- + ----------------- + ---- -------------------

    127 | finland      | finland         | 
    128 | finland      | finland         | 
    129 | finland      | finland         | 

(3 rows) (3排)

I appreciate any help here. 我感谢任何帮助。

I'm wondering if you need to initialize the new entry by specifying an init method for your model. 我想知道您是否需要通过为模型指定init方法来初始化新条目。 Where are the "name" and "content" kwargs coming from? “名字”和“内容”kwargs来自哪里?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM