Python 烧瓶嗖嗖炼金术:搜索';不是';

Python 烧瓶嗖嗖炼金术:搜索';不是';,python,flask,full-text-search,whoosh,Python,Flask,Full Text Search,Whoosh,我刚刚读完了Flask mega教程中关于使用Flask WhooshAlchemy()实现全文搜索的部分,我的帖子如下: >>> Post.query.whoosh_search('fourth').all() [Post u'not my fourth', Post u'my fourth and last post'] 我尝试使用Post.query.whoosh\u搜索('fourth AND not')。all()期望返回[Post u'not my fourth'

我刚刚读完了Flask mega教程中关于使用Flask WhooshAlchemy()实现全文搜索的部分,我的帖子如下:

>>> Post.query.whoosh_search('fourth').all()
[Post u'not my fourth', Post u'my fourth and last post']
我尝试使用
Post.query.whoosh\u搜索('fourth AND not')。all()
期望返回
[Post u'not my fourth']
,但我得到的是两篇原始帖子


如何让WhooshAlchemy将
而不是
视为字符串而不是运算符?

根据本页中的最后一段,默认情况下,查询术语被视为AND。因此,将您的搜索更改为

Post.query.whoosh_search("fourth not").all()
如果你仍然有问题,也许你必须这样做

Post.query.whoosh_search("fourth AND 'not'").all()

根据。

我已重新创建了您的设置

>>> Post.query.whoosh_search('fourth not').all()
>>> [<Post u'not my fourth'>, <Post u'my fourth and last post'>]
这篇文章应该是“不是我的第四篇”,对吧

根据中的“停止词”一节,“停止词”是非常常见的词,索引它们通常会适得其反。 有一个链接,显示默认情况下“not”是一个停止词,而whoosh_搜索不会为其编制索引

因此,让我们在另一篇帖子中加上“第四个”和一个不太常见的词——“奶酪”怎么样

>>> p = Post(body='cheese is the fourth food group', timestamp=datetime.datetime.utcnow(), author=u)
>>> db.session.add(p)
>>> db.session.commit()
现在让我们搜索所有正文中带有“第四”和“奶酪”的帖子

>>> Post.query.whoosh_search('fourth cheese').all()
>>> [<Post u'cheese is the fourth food group'>]
>Post.query.whoosh_搜索('fourth cheese').all()
>>> []
太好了

奖励:如果你想获得所有带有“第四”或“奶酪”的帖子,请执行以下操作:

>>> Post.query.whoosh_search('cheese fourth', or_=True).all()
>>> [<Post u'cheese is the fourth food group'>, <Post u'not my fourth'>, <Post u'my fourth and last post'>]
>>Post.query.whoosh\u search('cheese-fourth',或=True).all()
>>> [, ]

Post.query.whoosh搜索(“第四个非”).all()
Post.query.whoosh搜索(“第四个非”).all()
返回两个帖子。还有其他想法吗?可能是
.all()
?如果忽略了这一点,您会得到一个查询或whoosh对象,还是得到您需要的结果?
>>> Post.query.whoosh_search('cheese fourth', or_=True).all()
>>> [<Post u'cheese is the fourth food group'>, <Post u'not my fourth'>, <Post u'my fourth and last post'>]