Skip to content

Commit

Permalink
Ensure that when fetching the length of a result set that the whole i…
Browse files Browse the repository at this point in the history
…ndex isn't consumed (especially on Whoosh & Xapian).
  • Loading branch information
toastdriven committed Oct 28, 2010
1 parent 3b473cd commit 03279e3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions haystack/backends/__init__.py
Expand Up @@ -396,6 +396,11 @@ def get_count(self):
the results.
"""
if self._hit_count is None:
# Limit the slice to 10 so we get a count without consuming
# everything.
if not self.end_offset:
self.end_offset = 10

if self._more_like_this:
# Special case for MLT.
self.run_mlt()
Expand Down
3 changes: 0 additions & 3 deletions haystack/backends/whoosh_backend.py
Expand Up @@ -344,9 +344,6 @@ def search(self, query_string, sort_by=None, start_offset=0, end_offset=None,
if narrowed_results:
raw_results.filter(narrowed_results)

# Make sure we don't process bits we already have.
# import pdb; pdb.set_trace()

# Determine the page.
page_num = 0

Expand Down
4 changes: 2 additions & 2 deletions tests/core/tests/mocks.py
Expand Up @@ -43,7 +43,7 @@ def search(self, query_string, sort_by=None, start_offset=0, end_offset=None,
hits = len(self.mock_search_results)
indexed_models = site.get_indexed_models()

sliced = self.mock_search_results[start_offset:end_offset]
sliced = self.mock_search_results

for result in sliced:
model = get_model('core', self.model_name)
Expand All @@ -57,7 +57,7 @@ def search(self, query_string, sort_by=None, start_offset=0, end_offset=None,
hits -= 1

return {
'results': results,
'results': results[start_offset:end_offset],
'hits': hits,
}

Expand Down
1 change: 1 addition & 0 deletions tests/core/tests/query.py
Expand Up @@ -478,6 +478,7 @@ def test_load_all(self):

# If nothing is registered, you get nothing.
haystack.site.unregister(MockModel)
haystack.site.unregister(CharPKMockModel)
sqs = self.msqs.load_all()
self.assert_(isinstance(sqs, SearchQuerySet))
self.assertEqual(len(sqs), 0)
Expand Down
19 changes: 19 additions & 0 deletions tests/whoosh_tests/tests/whoosh_backend.py
Expand Up @@ -580,6 +580,25 @@ def test_cache_is_full(self):
fire_the_iterator_and_fill_cache = [result for result in results]
self.assertEqual(results._cache_is_full(), True)
self.assertEqual(len(backends.queries), 1)

def test_count(self):
more_samples = []

for i in xrange(1, 50):
mock = MockModel()
mock.id = i
mock.author = 'daniel%s' % i
mock.pub_date = date(2009, 2, 25) - timedelta(days=i)
more_samples.append(mock)

self.sb.update(self.smmi, more_samples)

backends.reset_search_queries()
self.assertEqual(len(backends.queries), 0)
results = self.sqs.all()
self.assertEqual(len(results), 49)
self.assertEqual(results._cache_is_full(), False)
self.assertEqual(len(backends.queries), 1)


class WhooshRoundTripSearchIndex(SearchIndex):
Expand Down

0 comments on commit 03279e3

Please sign in to comment.