Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: flask-whooshee invoked whoosh searcher without limit, so whoosh used defalt limit value == 10 #7

Merged
merged 3 commits into from Oct 31, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions flask_whooshee.py
Expand Up @@ -15,14 +15,16 @@ class WhoosheeQuery(BaseQuery):
"""An override for SQLAlchemy query used to do fulltext search."""

# TODO: add an option to override used Whoosheer
def whooshee_search(self, search_string, group=whoosh.qparser.OrGroup, match_substrings=True):
def whooshee_search(self, search_string, group=whoosh.qparser.OrGroup,
match_substrings=True, limit=None):
"""Do a fulltext search on the query.

Args:
search_string: string to search for
group: whoosh group to use for searching, defaults to OrGroup (searches for all
words in all columns)
match_substrings: True if you want to match substrings, False otherwise
limit: number of the top records to be returned, default None returns all records

Returns:
query filtered with results of the fulltext search
Expand Down Expand Up @@ -52,7 +54,8 @@ def whooshee_search(self, search_string, group=whoosh.qparser.OrGroup, match_sub
res = whoosheer.search(search_string=search_string,
values_of=uniq,
group=group,
match_substrings=match_substrings)
match_substrings=match_substrings,
limit=None)
if not res:
return self.filter('null')

Expand Down Expand Up @@ -80,7 +83,7 @@ class AbstractWhoosheer(object):
"""

@classmethod
def search(cls, search_string, values_of='', group=whoosh.qparser.OrGroup, match_substrings=True):
def search(cls, search_string, values_of='', group=whoosh.qparser.OrGroup, match_substrings=True, limit=None):
"""Actually searches the fields for given search_string.

Args:
Expand All @@ -90,14 +93,16 @@ def search(cls, search_string, values_of='', group=whoosh.qparser.OrGroup, match
group: whoosh group to use for searching, defaults to OrGroup (searches for all
words in all columns)
match_substrings: True if you want to match substrings, False otherwise
limit: number of the top records to be returned, default None returns all records

Returns:
Found records if 'not values_of', else values of given column
"""
prepped_string = cls.prep_search_string(search_string, match_substrings)
with cls.index.searcher() as searcher:
parser = whoosh.qparser.MultifieldParser(cls.schema.names(), cls.index.schema, group=group)
query = parser.parse(prepped_string)
results = searcher.search(query)
results = searcher.search(query, limit=limit)
if values_of:
return [x[values_of] for x in results]
return results
Expand Down
19 changes: 19 additions & 0 deletions test.py
Expand Up @@ -113,4 +113,23 @@ def test_cw_result_in_different_tables(self):
self.assertIn(self.e2, found)
self.assertIn(self.e4, found)

def test_more_items(self):
expected_count = 0
# couldn't test for large set due to some bugs either in sqlite or whoosh or SA
# got: OperationalError: (OperationalError) too many SQL variables u'SELECT entry.id
# ... FROM entry \nWHERE entry.id IN (?, ?, .... when whooshee_search is invoked
for batch_size in [2, 5, 7, 20, 50, 300, 500]: # , 1000]:
expected_count += batch_size
self.entry_list = [
self.Entry(title=u'foobar_{}_{}'.format(expected_count, x),
content=u'xxxx', user=self.u1)
for x in range(batch_size)
]

self.db.session.add_all(self.entry_list)
self.db.session.commit()

found = self.Entry.query.whooshee_search('foobar').all()
assert len(found) == expected_count

# TODO: more :)