烧瓶的whoosh扩展/sqlalchemy,用于新浪集装箱
Container-WhooshAlchemyPlus的Python项目详细描述
从gyllstromk/Flask-WhooshAlchemy分叉
flask whooshalchemyplus是一个flask扩展,它集成了Whoosh的文本搜索功能和SQLAlchemy的形式,用于Flask应用程序。
源代码和问题跟踪位于GitHub。
安装
$ pip install flask_whooshalchemyplus
或:
$ git clone https://github.com/Revolution1/Flask-WhooshAlchemyPlus.git $ cd Flask-WhooshAlchemyPlus && python setup.py install
快速启动
让我们设置环境并创建模型:
import flask_whooshalchemyplus # 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 __analyzer__ = SimpleAnalyzer() # configure analyzer; defaults to # StemmingAnalyzer if not specified id = app.db.Column(app.db.Integer, primary_key=True) title = app.db.Column(app.db.Unicode) # Indexed fields are either String, content = app.db.Column(app.db.Text) # Unicode, or Text created = db.Column(db.DateTime, default=datetime.datetime.utcnow) flask_whooshalchemyplus.init_app(app) # initialize
只有两个步骤可以开始:
- 将WHOOSH_BASE设置为whoosh索引的路径。如果未设置,则默认为运行应用程序的目录中名为“whoosh_index”的目录。
- 将__searchable__字段添加到指定要索引的字段(作为strs)的模型中。
让我们创建一个帖子:
db.session.add( BlogPost(title='My cool title', content='This is the first post.') ); db.session.commit()
提交会话后,我们的新BlogPost将被索引。同样,如果帖子被删除,它将从whoosh索引中删除。
手动标引< /H2>
只有在服务器运行时,才能索引by defualt记录。 因此,如果要手动编制索引:
from flask_whooshalchemyplus import index_all index_all(app)
文本搜索
执行简单搜索:
results = BlogPost.query.whoosh_search('cool')
这将返回所有BlogPost实例,其中至少有一个索引字段(即“title”或“content”)是与查询匹配的文本。结果根据相关性得分进行排序,最佳匹配在迭代时首先出现。此调用的结果是一个sqlalchemy.orm.query.Query对象的(子类),因此可以链接其他sql操作。例如:
two_days_ago = datetime.date.today() - datetime.timedelta(2) recent_matches = BlogPost.query.whoosh_search('first').filter( BlogPost.created >= two_days_ago)
或者,以另一种(可能较慢的)顺序:
recent_matches = BlogPost.query.filter( BlogPost.created >= two_days_ago).whoosh_search('first')
我们可以限制结果:
# get 2 best results: results = BlogPost.query.whoosh_search('cool', limit=2)
默认情况下,将对所有索引字段执行作为或连接的搜索。例如,如果模型的“title”和“content”表示为__searchable__,则将对这两个字段检查查询,并返回其标题或内容与查询内容匹配的任何实例。要指定要检查的特定字段,请用所需字段填充fields参数:
results = BlogPost.query.whoosh_search('cool', fields=('title',))
默认情况下,只有包含所有查询条件(和)的结果才会返回。要切换到或分组,请将or_参数设置为True:
results = BlogPost.query.whoosh_search('cool', or_=True)
如果您也想要普通文本匹配结果:
results = BlogPost.query.whoosh_search('cool', like=True)
这就像whoosh_search('cool') + SQL LIKE '%cool%'
纯粹的“嗖”声
如果您想要whoosh.index.searcher().search()结果:
results = BlogPost.pure_whoosh(self, query, limit=None, fields=None, or_=False)
whooshdisabled上下文管理器
暂时禁用WHOSOH索引:
with WhooshDisabled(): do sth.
更改日志
- V0.7.5:
- 功能:添加whooshdisabled上下文管理器
- 功能:添加whoosh_index_all和init_app方法
- :索引方法< /LI>
- 修复:索引错误:模型没有属性'\u searchable'
- V0.7.4:
- 功能:使用类似sql的语句添加模糊搜索
- V0.7.3:
- 修正:中文分析器不起作用
- V0.7.2:
- 修复:index_all无法单独检测可索引模型
- V0.7.1:
-
特性:索引子模块类^ {A6}
- 功能:添加python3 supplot
- 修正:如果调用方在查询github pull request #32上显式使用order_by(),则服从结果排序
- 修正:自定义查询类用法github pull request #35
- 功能:添加WHOOSH_DISABLED选项以在运行时禁用whooshalchemyplus