Fixed #112 - more_like_this raises InvalidIndexError for unindexed instance.
This doesn't happen if silently_fail is True, in which case returns an empty result. Added test to cover both cases.
This commit is contained in:
parent
65c6e9a71b
commit
01805d08f8
|
@ -12,7 +12,7 @@ from django.test import TestCase
|
|||
|
||||
from haystack import connections, reset_search_queries
|
||||
from haystack import indexes
|
||||
from haystack.backends.xapian_backend import _marshal_value
|
||||
from haystack.backends.xapian_backend import InvalidIndexError, _marshal_value
|
||||
from haystack.models import SearchResult
|
||||
from haystack.query import SearchQuerySet, SQ
|
||||
from haystack.utils.loading import UnifiedIndex
|
||||
|
@ -454,6 +454,24 @@ class XapianSearchBackendTestCase(HaystackBackendTestCase, TestCase):
|
|||
self.assertEqual(str(self.backend.parse_query('popularity:25.5..100.0')),
|
||||
b'Xapian::Query(VALUE_RANGE 7 \xb2` \xba@)')
|
||||
|
||||
def test_more_like_this_with_unindexed_model(self):
|
||||
"""
|
||||
Tests that more_like_this raises an error when it is called
|
||||
with an unindexed model and if silently_fail is True.
|
||||
Also tests the other way around.
|
||||
"""
|
||||
mock = XapianMockModel()
|
||||
mock.id = 10
|
||||
mock.author = 'david10'
|
||||
|
||||
try:
|
||||
self.assertEqual(self.backend.more_like_this(mock)['results'], [])
|
||||
except InvalidIndexError:
|
||||
self.fail("InvalidIndexError raised when silently_fail is True")
|
||||
|
||||
self.backend.silently_fail = False
|
||||
self.assertRaises(InvalidIndexError, self.backend.more_like_this, mock)
|
||||
|
||||
|
||||
class LiveXapianMockSearchIndex(indexes.SearchIndex):
|
||||
text = indexes.CharField(document=True, use_template=True)
|
||||
|
|
|
@ -530,9 +530,18 @@ class XapianSearchBackend(BaseSearchBackend):
|
|||
if not end_offset:
|
||||
end_offset = database.get_doccount()
|
||||
|
||||
match = None
|
||||
for match in self._get_enquire_mset(database, enquire, 0, end_offset):
|
||||
rset.add_document(match.docid)
|
||||
|
||||
if match is None:
|
||||
if not self.silently_fail:
|
||||
raise InvalidIndexError('Instance %s with id "%d" not indexed' %
|
||||
(get_identifier(model_instance), model_instance.id))
|
||||
else:
|
||||
return {'results': [],
|
||||
'hits': 0}
|
||||
|
||||
query = xapian.Query(
|
||||
xapian.Query.OP_ELITE_SET,
|
||||
[expand.term for expand in enquire.get_eset(match.document.termlist_count(), rset, XHExpandDecider())],
|
||||
|
|
Loading…
Reference in New Issue