我正在建立一个笔记制作应用程序,用户可以创建笔记,添加照片,标签和与朋友分享它。我使用python烧瓶框架来构建这个工具。
我想提供一个功能,用户可以搜索他们的笔记。如何构建索引,是否有任何库可以快速完成此操作?
我使用MongoDB作为后端DB
发布于 2014-04-26 08:22:14
你试过用烧瓶吗?
烧瓶-WhooshAlchemy是一个酒瓶扩展,它集成了Whoosh的文本搜索功能和SQLAlchemy的ORM,以便在烧瓶应用程序中使用。
安装
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)创建一个post
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)来源:烧瓶-炼金术
https://stackoverflow.com/questions/23308104
复制相似问题