2014-05-10 49 views
1

我剛剛經歷了瓶大型教程的部分得到了關於實施與燒瓶WhooshAlchemy(http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-x-full-text-search)全文搜索,我有以下職位:燒瓶WhooshAlchemy:搜索「而不是」

>>> Post.query.whoosh_search('fourth').all() 
[Post u'not my fourth', Post u'my fourth and last post'] 

我試着使用Post.query.whoosh_search('fourth AND not').all()期待得到[Post u'not my fourth']作爲結果,但我得到的原始帖子,而不是。

如何讓WhooshAlchemy將not作爲字符串而不是操作符對待?

回答

0

根據Flask-WhooshAlchemy docs中此頁面上的最後一段,查詢條件在默認情況下被視爲AND。因此,改變你的搜索是

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

如果你仍然有它的問題,也許你需要做的

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

Whoosh's docs on making a term from literal text

+0

兩個'Post.query.whoosh_search( 「第四不」)所有()'和'Post.query.whoosh_search(「第四AND'不'')。all()'返回這兩個帖子。任何其他想法? – mvwi

+0

難道是'.all()'?如果你忽略了這個問題,你是否會收到一個查詢或whoosh對象,或者你需要的結果? –

0

我重新創建了您的設置。

>>> Post.query.whoosh_search('fourth not').all() 
>>> [<Post u'not my fourth'>, <Post u'my fourth and last post'>] 

你應該問的問題是:爲什麼不能whoosh_search找不到?嘗試這個。

>>> Post.query.whoosh_search('not').all() 
>>> [] 

這應該返回帖子'不是我的第四',對吧?

根據this document中的「停用詞」部分,「停用」詞是如此常見的詞,它們通常會對索引它們起反作用。 This question有一個鏈接,顯示默認情況下'不是'是停用詞,而whoosh_search不會將其編入索引。

因此,讓我們添加另一篇文章'第四'和一個不太常見的詞 - '奶酪'怎麼樣。

>>> 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'>] 

完美。

好處:如果你想獲得與 '第四' OR '奶酪',這樣做的所有帖子:

>>> 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'>]