【问题标题】:Search Index for user articles用户文章搜索索引
【发布时间】:2014-06-12 01:15:38
【问题描述】:

我正在构建一个笔记制作应用程序,用户可以在其中创建笔记、添加照片、标记并与朋友分享。 我正在使用 python 烧瓶框架来构建工具。

我想提供一个功能,用户可以在其中搜索他们的笔记。 我该如何构建索引,有没有库可以快速做到这一点?

我使用 MongoDB 作为后端数据库

【问题讨论】:

标签: python search indexing flask


【解决方案1】:

您是否尝试过使用 Flask-WhooshAlchemy。

Flask-WhooshAlchemy 是一个 Flask 扩展,它将 Whoosh 的文本搜索功能与 SQLAlchemy 的 ORM 相集成,用于 Flask 应用程序。

安装

pip install flask_whooshalchemy

快速入门

import flask.ext.whooshalchemy

# set the location for the whoosh index
app.config['WHOOSH_BASE'] = 'path/to/whoosh/base'


class BlogPost(db.Model):
  __tablename__ = 'blogpost'
  __searchable__ = ['title', 'content']  # these fields will be indexed by whoosh

  id = app.db.Column(app.db.Integer, primary_key=True)
  title = app.db.Column(app.db.Text)
  content = app.db.Column(app.db.Text)
  created = db.Column(db.DateTime, default=datetime.datetime.utcnow)

  def __repr__(self):
     return '{0}(title={1})'.format(self.__class__.__name__, self.title)

创建帖子

db.session.add(
    BlogPost(title='My cool title', content='This is the first post.')
); db.session.commit()

文本搜索

results = BlogPost.query.whoosh_search('cool')

我们可以限制结果:

# get 2 best results:
results = BlogPost.query.whoosh_search('cool', limit=2)

来源:Flask-WhooshAlchemy

【讨论】:

  • Flask-WhooshAlchemy 是一个 Flask 扩展,它将 Whoosh 的文本搜索功能与 SQLAlchemyORM 集成在一起,用于 Flask 应用程序。根据给定的描述,我会说不是...
猜你喜欢
  • 2011-02-01
  • 2017-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-04
  • 2018-09-27
  • 2018-10-26
  • 1970-01-01
相关资源
最近更新 更多