1

I just got through the Flask mega-tutorial's section on implementing full text search with Flask-WhooshAlchemy (http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-x-full-text-search) and I have the posts below:

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

I tried using Post.query.whoosh_search('fourth AND not').all() expecting to get back [Post u'not my fourth'] as a result but I'm getting both of the original posts instead.

How do I get WhooshAlchemy to treat the not as a string rather than an operator?

mvwi
  • 243
  • 1
  • 2
  • 7

2 Answers2

0

According to the last paragraph on this page in the Flask-WhooshAlchemy docs, query terms are treated like an AND by default. So change your search to be

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

If you are still having issues with it, perhaps you have to do

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

as per Whoosh's docs on making a term from literal text.

Eric Workman
  • 1,425
  • 12
  • 13
  • Both `Post.query.whoosh_search("fourth not").all()` and `Post.query.whoosh_search("fourth AND 'not'").all()` return both of the posts. Any other ideas? – mvwi May 10 '14 at 19:04
  • Could it be the `.all()`? If you omit that, do you get a query or whoosh object back, or the results you need? – Eric Workman May 12 '14 at 12:17
0

I have recreated your setup.

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

The question you should have asked is: Why can't whoosh_search find not? Try this.

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

This should have returned the post 'not my fourth', right?

According to the "Stop Words" section in this document, “Stop” words are words that are so common it’s often counter-productive to index them. This question has a link which shows that by default 'not' is a stop word, and whoosh_search does not index it.

So lets add another post with 'fourth' and a less common word - how about 'cheese'.

>>> p = Post(body='cheese is the fourth food group', timestamp=datetime.datetime.utcnow(), author=u)
>>> db.session.add(p)
>>> db.session.commit()

And now lets search for all posts with 'fourth' AND 'cheese' in the body.

>>> Post.query.whoosh_search('fourth cheese').all()
>>> [<Post u'cheese is the fourth food group'>]

Perfect.

BONUS: if you want to get all posts with 'fourth' OR 'cheese', do this:

>>> 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'>]
Community
  • 1
  • 1
amath
  • 1,279
  • 9
  • 14