本文整理汇总了Python中haystack.backends.whoosh_backend.SearchBackend类的典型用法代码示例。如果您正苦于以下问题:Python SearchBackend类的具体用法?Python SearchBackend怎么用?Python SearchBackend使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SearchBackend类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: LiveWhooshMultiSearchQuerySetTestCase
class LiveWhooshMultiSearchQuerySetTestCase(TestCase):
fixtures = ['bulk_data.json']
def setUp(self):
super(LiveWhooshMultiSearchQuerySetTestCase, self).setUp()
# Stow.
temp_path = os.path.join('tmp', 'test_whoosh_query')
self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH', temp_path)
settings.HAYSTACK_WHOOSH_PATH = temp_path
self.site = SearchSite()
self.sb = SearchBackend(site=self.site)
self.smmi = WhooshMockSearchIndex(MockModel, backend=self.sb)
self.sammi = WhooshAnotherMockSearchIndex(AnotherMockModel, backend=self.sb)
self.site.register(MockModel, WhooshMockSearchIndex)
self.site.register(AnotherMockModel, WhooshAnotherMockSearchIndex)
# Stow.
import haystack
self.old_debug = settings.DEBUG
settings.DEBUG = True
self.old_site = haystack.site
haystack.site = self.site
self.sb.setup()
self.raw_whoosh = self.sb.index
self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
self.sb.delete_index()
self.sq = SearchQuery(backend=self.sb)
self.sqs = SearchQuerySet(site=self.site)
self.smmi.update()
self.sammi.update()
def tearDown(self):
if os.path.exists(settings.HAYSTACK_WHOOSH_PATH):
shutil.rmtree(settings.HAYSTACK_WHOOSH_PATH)
settings.HAYSTACK_WHOOSH_PATH = self.old_whoosh_path
import haystack
haystack.site = self.old_site
settings.DEBUG = self.old_debug
super(LiveWhooshMultiSearchQuerySetTestCase, self).tearDown()
def test_searchquerysets_with_models(self):
sqs = self.sqs.all()
self.assertEqual(sqs.query.build_query(), u'*')
self.assertEqual(len(sqs), 25)
sqs = self.sqs.models(MockModel)
self.assertEqual(sqs.query.build_query(), u'django_ct:core.mockmodel')
self.assertEqual(len(sqs), 23)
sqs = self.sqs.models(AnotherMockModel)
self.assertEqual(sqs.query.build_query(), u'django_ct:core.anothermockmodel')
self.assertEqual(len(sqs), 2)
示例2: LiveWhooshAutocompleteTestCase
class LiveWhooshAutocompleteTestCase(TestCase):
fixtures = ['bulk_data.json']
def setUp(self):
super(LiveWhooshAutocompleteTestCase, self).setUp()
# Stow.
temp_path = os.path.join('tmp', 'test_whoosh_query')
self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH', temp_path)
settings.HAYSTACK_WHOOSH_PATH = temp_path
self.site = SearchSite()
self.sb = SearchBackend(site=self.site)
self.wacsi = WhooshAutocompleteMockModelSearchIndex(MockModel, backend=self.sb)
self.site.register(MockModel, WhooshAutocompleteMockModelSearchIndex)
# Stow.
import haystack
self.old_debug = settings.DEBUG
settings.DEBUG = True
self.old_site = haystack.site
haystack.site = self.site
self.sb.setup()
self.sqs = SearchQuerySet(site=self.site)
# Wipe it clean.
self.sqs.query.backend.clear()
for mock in MockModel.objects.all():
self.wacsi.update_object(mock)
def tearDown(self):
if os.path.exists(settings.HAYSTACK_WHOOSH_PATH):
shutil.rmtree(settings.HAYSTACK_WHOOSH_PATH)
settings.HAYSTACK_WHOOSH_PATH = self.old_whoosh_path
import haystack
haystack.site = self.old_site
settings.DEBUG = self.old_debug
super(LiveWhooshAutocompleteTestCase, self).tearDown()
def test_autocomplete(self):
autocomplete = self.sqs.autocomplete(text_auto='mod')
self.assertEqual(autocomplete.count(), 5)
self.assertEqual([result.pk for result in autocomplete], [u'1', u'12', u'6', u'7', u'14'])
self.assertTrue('mod' in autocomplete[0].text.lower())
self.assertTrue('mod' in autocomplete[1].text.lower())
self.assertTrue('mod' in autocomplete[2].text.lower())
self.assertTrue('mod' in autocomplete[3].text.lower())
self.assertTrue('mod' in autocomplete[4].text.lower())
self.assertEqual(len([result.pk for result in autocomplete]), 5)
def test_edgengram_regression(self):
autocomplete = self.sqs.autocomplete(text_auto='ngm')
self.assertEqual(autocomplete.count(), 0)
示例3: setUp
def setUp(self):
super(LiveWhooshRamStorageTestCase, self).setUp()
# Stow.
self.old_whoosh_storage = getattr(settings, 'HAYSTACK_WHOOSH_STORAGE', 'file')
settings.HAYSTACK_WHOOSH_STORAGE = 'ram'
self.site = SearchSite()
self.sb = SearchBackend(site=self.site)
self.wrtsi = WhooshRoundTripSearchIndex(MockModel, backend=self.sb)
self.site.register(MockModel, WhooshRoundTripSearchIndex)
# Stow.
import haystack
self.old_debug = settings.DEBUG
settings.DEBUG = True
self.old_site = haystack.site
haystack.site = self.site
self.sb.setup()
self.raw_whoosh = self.sb.index
self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
self.sqs = SearchQuerySet(site=self.site)
# Wipe it clean.
self.sqs.query.backend.clear()
# Fake indexing.
mock = MockModel()
mock.id = 1
self.sb.update(self.wrtsi, [mock])
示例4: setUp
def setUp(self):
super(LiveWhooshMultiSearchQuerySetTestCase, self).setUp()
# Stow.
temp_path = os.path.join('tmp', 'test_whoosh_query')
self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH', temp_path)
settings.HAYSTACK_WHOOSH_PATH = temp_path
self.site = SearchSite()
self.sb = SearchBackend(site=self.site)
self.smmi = WhooshMockSearchIndex(MockModel, backend=self.sb)
self.sammi = WhooshAnotherMockSearchIndex(AnotherMockModel, backend=self.sb)
self.site.register(MockModel, WhooshMockSearchIndex)
self.site.register(AnotherMockModel, WhooshAnotherMockSearchIndex)
# Stow.
import haystack
self.old_debug = settings.DEBUG
settings.DEBUG = True
self.old_site = haystack.site
haystack.site = self.site
self.sb.setup()
self.raw_whoosh = self.sb.index
self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
self.sb.delete_index()
self.sq = SearchQuery(backend=self.sb)
self.sqs = SearchQuerySet(site=self.site)
self.smmi.update()
self.sammi.update()
示例5: setUp
def setUp(self):
super(WhooshSearchBackendTestCase, self).setUp()
# Stow.
temp_path = os.path.join("tmp", "test_whoosh_query")
self.old_whoosh_path = getattr(settings, "HAYSTACK_WHOOSH_PATH", temp_path)
settings.HAYSTACK_WHOOSH_PATH = temp_path
self.site = WhooshSearchSite()
self.sb = SearchBackend(site=self.site)
self.smmi = WhooshMockSearchIndex(MockModel, backend=self.sb)
self.wmtmmi = WhooshMaintainTypeMockSearchIndex(MockModel, backend=self.sb)
self.site.register(MockModel, WhooshMockSearchIndex)
# With the models registered, you get the proper bits.
import haystack
# Stow.
self.old_site = haystack.site
haystack.site = self.site
self.sb.setup()
self.raw_whoosh = self.sb.index
self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
self.sb.delete_index()
self.sample_objs = []
for i in xrange(1, 4):
mock = MockModel()
mock.id = i
mock.author = "daniel%s" % i
mock.pub_date = date(2009, 2, 25) - timedelta(days=i)
self.sample_objs.append(mock)
示例6: setUp
def setUp(self):
super(WhooshBoostBackendTestCase, self).setUp()
# Stow.
temp_path = os.path.join('tmp', 'test_whoosh_query')
self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH', temp_path)
settings.HAYSTACK_WHOOSH_PATH = temp_path
self.site = SearchSite()
self.sb = SearchBackend(site=self.site)
self.smmi = WhooshBoostMockSearchIndex(AFourthMockModel, backend=self.sb)
self.site.register(AFourthMockModel, WhooshBoostMockSearchIndex)
# With the models registered, you get the proper bits.
import haystack
# Stow.
self.old_site = haystack.site
haystack.site = self.site
self.sb.setup()
self.raw_whoosh = self.sb.index
self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
self.sb.delete_index()
self.sample_objs = []
for i in xrange(1, 5):
mock = AFourthMockModel()
mock.id = i
if i % 2:
mock.author = 'daniel'
mock.editor = 'david'
else:
mock.author = 'david'
mock.editor = 'daniel'
mock.pub_date = date(2009, 2, 25) - timedelta(days=i)
self.sample_objs.append(mock)
示例7: LiveWhooshSearchQueryTestCase
class LiveWhooshSearchQueryTestCase(TestCase):
def setUp(self):
super(LiveWhooshSearchQueryTestCase, self).setUp()
# Stow.
temp_path = os.path.join('tmp', 'test_whoosh_query')
self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH', temp_path)
settings.HAYSTACK_WHOOSH_PATH = temp_path
self.site = WhooshSearchSite()
self.sb = SearchBackend(site=self.site)
self.smmi = WhooshMockSearchIndex(MockModel, backend=self.sb)
self.site.register(MockModel, WhooshMockSearchIndex)
self.sb.setup()
self.raw_whoosh = self.sb.index
self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
self.sb.delete_index()
self.sample_objs = []
for i in xrange(1, 4):
mock = MockModel()
mock.id = i
mock.author = 'daniel%s' % i
mock.pub_date = date(2009, 2, 25) - timedelta(days=i)
self.sample_objs.append(mock)
self.sq = SearchQuery(backend=self.sb)
def tearDown(self):
if os.path.exists(settings.HAYSTACK_WHOOSH_PATH):
shutil.rmtree(settings.HAYSTACK_WHOOSH_PATH)
settings.HAYSTACK_WHOOSH_PATH = self.old_whoosh_path
super(LiveWhooshSearchQueryTestCase, self).tearDown()
def test_get_spelling(self):
self.sb.update(self.smmi, self.sample_objs)
self.sq.add_filter('content', 'Indx')
self.assertEqual(self.sq.get_spelling_suggestion(), u'index')
示例8: WhooshSearchBackendTestCase
class WhooshSearchBackendTestCase(TestCase):
def setUp(self):
super(WhooshSearchBackendTestCase, self).setUp()
# Stow.
temp_path = os.path.join("tmp", "test_whoosh_query")
self.old_whoosh_path = getattr(settings, "HAYSTACK_WHOOSH_PATH", temp_path)
settings.HAYSTACK_WHOOSH_PATH = temp_path
self.site = WhooshSearchSite()
self.sb = SearchBackend(site=self.site)
self.smmi = WhooshMockSearchIndex(MockModel, backend=self.sb)
self.wmtmmi = WhooshMaintainTypeMockSearchIndex(MockModel, backend=self.sb)
self.site.register(MockModel, WhooshMockSearchIndex)
# With the models registered, you get the proper bits.
import haystack
# Stow.
self.old_site = haystack.site
haystack.site = self.site
self.sb.setup()
self.raw_whoosh = self.sb.index
self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
self.sb.delete_index()
self.sample_objs = []
for i in xrange(1, 4):
mock = MockModel()
mock.id = i
mock.author = "daniel%s" % i
mock.pub_date = date(2009, 2, 25) - timedelta(days=i)
self.sample_objs.append(mock)
def tearDown(self):
if os.path.exists(settings.HAYSTACK_WHOOSH_PATH):
shutil.rmtree(settings.HAYSTACK_WHOOSH_PATH)
settings.HAYSTACK_WHOOSH_PATH = self.old_whoosh_path
# Restore.
import haystack
haystack.site = self.old_site
super(WhooshSearchBackendTestCase, self).tearDown()
def whoosh_search(self, query):
self.raw_whoosh = self.raw_whoosh.refresh()
searcher = self.raw_whoosh.searcher()
return searcher.search(self.parser.parse(query))
def test_update(self):
self.sb.update(self.smmi, self.sample_objs)
# Check what Whoosh thinks is there.
self.assertEqual(len(self.whoosh_search(u"*")), 3)
self.assertEqual(
[dict(doc) for doc in self.whoosh_search(u"*")],
[
{
"django_id": u"3",
"django_ct": u"core.mockmodel",
"name": u"daniel3",
"text": u"Indexed!\n3",
"pub_date": u"2009-02-22T00:00:00",
"id": u"core.mockmodel.3",
},
{
"django_id": u"2",
"django_ct": u"core.mockmodel",
"name": u"daniel2",
"text": u"Indexed!\n2",
"pub_date": u"2009-02-23T00:00:00",
"id": u"core.mockmodel.2",
},
{
"django_id": u"1",
"django_ct": u"core.mockmodel",
"name": u"daniel1",
"text": u"Indexed!\n1",
"pub_date": u"2009-02-24T00:00:00",
"id": u"core.mockmodel.1",
},
],
)
def test_remove(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search(u"*")), 3)
self.sb.remove(self.sample_objs[0])
self.assertEqual(len(self.whoosh_search(u"*")), 2)
self.assertEqual(
[dict(doc) for doc in self.whoosh_search(u"*")],
[
{
"django_id": u"3",
#.........这里部分代码省略.........
示例9: WhooshSearchBackendTestCase
class WhooshSearchBackendTestCase(TestCase):
fixtures = ['bulk_data.json']
def setUp(self):
super(WhooshSearchBackendTestCase, self).setUp()
# Stow.
temp_path = os.path.join('tmp', 'test_whoosh_query')
self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH', temp_path)
settings.HAYSTACK_WHOOSH_PATH = temp_path
self.site = SearchSite()
self.sb = SearchBackend(site=self.site)
self.smmi = WhooshMockSearchIndex(MockModel, backend=self.sb)
self.wmtmmi = WhooshMaintainTypeMockSearchIndex(MockModel, backend=self.sb)
self.site.register(MockModel, WhooshMockSearchIndex)
# With the models registered, you get the proper bits.
import haystack
# Stow.
self.old_site = haystack.site
haystack.site = self.site
self.sb.setup()
self.raw_whoosh = self.sb.index
self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
self.sb.delete_index()
self.sample_objs = MockModel.objects.all()
def tearDown(self):
if os.path.exists(settings.HAYSTACK_WHOOSH_PATH):
shutil.rmtree(settings.HAYSTACK_WHOOSH_PATH)
settings.HAYSTACK_WHOOSH_PATH = self.old_whoosh_path
# Restore.
import haystack
haystack.site = self.old_site
super(WhooshSearchBackendTestCase, self).tearDown()
def whoosh_search(self, query):
self.raw_whoosh = self.raw_whoosh.refresh()
searcher = self.raw_whoosh.searcher()
return searcher.search(self.parser.parse(query), limit=1000)
def test_update(self):
self.sb.update(self.smmi, self.sample_objs)
# Check what Whoosh thinks is there.
self.assertEqual(len(self.whoosh_search(u'*')), 23)
self.assertEqual([doc.fields()['id'] for doc in self.whoosh_search(u'*')], [u'core.mockmodel.%s' % i for i in xrange(1, 24)])
def test_remove(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(self.sb.index.doc_count(), 23)
self.sb.remove(self.sample_objs[0])
self.assertEqual(self.sb.index.doc_count(), 22)
def test_clear(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(self.sb.index.doc_count(), 23)
self.sb.clear()
self.assertEqual(self.sb.index.doc_count(), 0)
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(self.sb.index.doc_count(), 23)
self.sb.clear([AnotherMockModel])
self.assertEqual(self.sb.index.doc_count(), 23)
self.sb.clear([MockModel])
self.assertEqual(self.sb.index.doc_count(), 0)
self.sb.index.refresh()
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(self.sb.index.doc_count(), 23)
self.sb.clear([AnotherMockModel, MockModel])
self.assertEqual(self.raw_whoosh.doc_count(), 0)
def test_search(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search(u'*')), 23)
# No query string should always yield zero results.
self.assertEqual(self.sb.search(u''), {'hits': 0, 'results': []})
# A one letter query string gets nabbed by a stopwords filter. Should
# always yield zero results.
self.assertEqual(self.sb.search(u'a'), {'hits': 0, 'results': []})
# Possible AttributeError?
# self.assertEqual(self.sb.search(u'a b'), {'hits': 0, 'results': [], 'spelling_suggestion': '', 'facets': {}})
self.assertEqual(self.sb.search(u'*')['hits'], 23)
#.........这里部分代码省略.........
示例10: LiveWhooshSearchQuerySetTestCase
class LiveWhooshSearchQuerySetTestCase(TestCase):
def setUp(self):
super(LiveWhooshSearchQuerySetTestCase, self).setUp()
# Stow.
temp_path = os.path.join('tmp', 'test_whoosh_query')
self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH', temp_path)
settings.HAYSTACK_WHOOSH_PATH = temp_path
self.site = SearchSite()
self.sb = SearchBackend(site=self.site)
self.smmi = WhooshMockSearchIndex(MockModel, backend=self.sb)
self.site.register(MockModel, WhooshMockSearchIndex)
# Stow.
import haystack
self.old_debug = settings.DEBUG
settings.DEBUG = True
self.old_site = haystack.site
haystack.site = self.site
self.sb.setup()
self.raw_whoosh = self.sb.index
self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
self.sb.delete_index()
self.sample_objs = []
for i in xrange(1, 4):
mock = MockModel()
mock.id = i
mock.author = 'daniel%s' % i
mock.pub_date = date(2009, 2, 25) - timedelta(days=i)
self.sample_objs.append(mock)
self.sq = SearchQuery(backend=self.sb)
self.sqs = SearchQuerySet(site=self.site)
def tearDown(self):
if os.path.exists(settings.HAYSTACK_WHOOSH_PATH):
shutil.rmtree(settings.HAYSTACK_WHOOSH_PATH)
settings.HAYSTACK_WHOOSH_PATH = self.old_whoosh_path
import haystack
haystack.site = self.old_site
settings.DEBUG = self.old_debug
super(LiveWhooshSearchQuerySetTestCase, self).tearDown()
def test_various_searchquerysets(self):
self.sb.update(self.smmi, self.sample_objs)
sqs = self.sqs.filter(content='Index')
self.assertEqual(sqs.query.build_query(), u'Index')
self.assertEqual(len(sqs), 3)
sqs = self.sqs.auto_query('Indexed!')
self.assertEqual(sqs.query.build_query(), u"'Indexed!'")
self.assertEqual(len(sqs), 3)
sqs = self.sqs.auto_query('Indexed!').filter(pub_date__lte=date(2009, 8, 31))
self.assertEqual(sqs.query.build_query(), u"('Indexed!' AND pub_date:[TO 20090831T000000])")
self.assertEqual(len(sqs), 3)
sqs = self.sqs.auto_query('Indexed!').filter(pub_date__lte=date(2009, 2, 23))
self.assertEqual(sqs.query.build_query(), u"('Indexed!' AND pub_date:[TO 20090223T000000])")
self.assertEqual(len(sqs), 2)
sqs = self.sqs.auto_query('Indexed!').filter(pub_date__lte=date(2009, 2, 25)).filter(django_id__in=[1, 2]).exclude(name='daniel1')
self.assertEqual(sqs.query.build_query(), u"('Indexed!' AND pub_date:[TO 20090225T000000] AND (django_id:\"1\" OR django_id:\"2\") AND NOT (name:daniel1))")
self.assertEqual(len(sqs), 1)
sqs = self.sqs.auto_query('re-inker')
self.assertEqual(sqs.query.build_query(), u"'re-inker'")
self.assertEqual(len(sqs), 0)
sqs = self.sqs.auto_query('0.7 wire')
self.assertEqual(sqs.query.build_query(), u"('0.7' AND wire)")
self.assertEqual(len(sqs), 0)
sqs = self.sqs.auto_query("daler-rowney pearlescent 'bell bronze'")
self.assertEqual(sqs.query.build_query(), u"('daler-rowney' AND pearlescent AND 'bell AND bronze')")
self.assertEqual(len(sqs), 0)
sqs = self.sqs.models(MockModel)
self.assertEqual(sqs.query.build_query(), u'django_ct:core.mockmodel')
self.assertEqual(len(sqs), 3)
def test_all_regression(self):
sqs = SearchQuerySet()
self.assertEqual([result.pk for result in sqs], [])
self.sb.update(self.smmi, self.sample_objs)
self.assert_(self.sb.index.doc_count() > 0)
sqs = SearchQuerySet()
self.assertEqual(len(sqs), 3)
self.assertEqual(sorted([result.pk for result in sqs]), [u'1', u'2', u'3'])
#.........这里部分代码省略.........
示例11: LiveWhooshSearchQueryTestCase
class LiveWhooshSearchQueryTestCase(TestCase):
def setUp(self):
super(LiveWhooshSearchQueryTestCase, self).setUp()
# Stow.
temp_path = os.path.join('tmp', 'test_whoosh_query')
self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH', temp_path)
settings.HAYSTACK_WHOOSH_PATH = temp_path
self.site = SearchSite()
self.sb = SearchBackend(site=self.site)
self.smmi = WhooshMockSearchIndex(MockModel, backend=self.sb)
self.site.register(MockModel, WhooshMockSearchIndex)
self.sb.setup()
self.raw_whoosh = self.sb.index
self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
self.sb.delete_index()
self.sample_objs = []
for i in xrange(1, 4):
mock = MockModel()
mock.id = i
mock.author = 'daniel%s' % i
mock.pub_date = date(2009, 2, 25) - timedelta(days=i)
self.sample_objs.append(mock)
self.sq = SearchQuery(backend=self.sb)
def tearDown(self):
if os.path.exists(settings.HAYSTACK_WHOOSH_PATH):
shutil.rmtree(settings.HAYSTACK_WHOOSH_PATH)
settings.HAYSTACK_WHOOSH_PATH = self.old_whoosh_path
super(LiveWhooshSearchQueryTestCase, self).tearDown()
def test_get_spelling(self):
self.sb.update(self.smmi, self.sample_objs)
self.sq.add_filter(SQ(content='Indx'))
self.assertEqual(self.sq.get_spelling_suggestion(), u'index')
def test_log_query(self):
from django.conf import settings
from haystack import backends
backends.reset_search_queries()
self.assertEqual(len(backends.queries), 0)
# Stow.
old_debug = settings.DEBUG
settings.DEBUG = False
len(self.sq.get_results())
self.assertEqual(len(backends.queries), 0)
settings.DEBUG = True
# Redefine it to clear out the cached results.
self.sq = SearchQuery(backend=self.sb)
self.sq.add_filter(SQ(name='bar'))
len(self.sq.get_results())
self.assertEqual(len(backends.queries), 1)
self.assertEqual(backends.queries[0]['query_string'], 'name:bar')
# And again, for good measure.
self.sq = SearchQuery(backend=self.sb)
self.sq.add_filter(SQ(name='baz'))
self.sq.add_filter(SQ(text='foo'))
len(self.sq.get_results())
self.assertEqual(len(backends.queries), 2)
self.assertEqual(backends.queries[0]['query_string'], 'name:bar')
self.assertEqual(backends.queries[1]['query_string'], u'(name:baz AND text:foo)')
# Restore.
settings.DEBUG = old_debug
示例12: test_non_silent
def test_non_silent(self):
old_silent = getattr(settings, 'HAYSTACK_SILENTLY_FAIL', True)
settings.HAYSTACK_SILENTLY_FAIL = False
bad_sb = SearchBackend(site=self.site)
bad_sb.use_file_storage = False
bad_sb.storage = 'omg.wtf.bbq'
try:
bad_sb.update(self.wmmi, self.sample_objs)
self.fail()
except:
pass
try:
bad_sb.remove('core.mockmodel.1')
self.fail()
except:
pass
try:
bad_sb.clear()
self.fail()
except:
pass
try:
bad_sb.search('foo')
self.fail()
except:
pass
settings.HAYSTACK_SILENTLY_FAIL = old_silent
示例13: LiveWhooshRamStorageTestCase
class LiveWhooshRamStorageTestCase(TestCase):
def setUp(self):
super(LiveWhooshRamStorageTestCase, self).setUp()
# Stow.
self.old_whoosh_storage = getattr(settings, 'HAYSTACK_WHOOSH_STORAGE', 'file')
settings.HAYSTACK_WHOOSH_STORAGE = 'ram'
self.site = SearchSite()
self.sb = SearchBackend(site=self.site)
self.wrtsi = WhooshRoundTripSearchIndex(MockModel, backend=self.sb)
self.site.register(MockModel, WhooshRoundTripSearchIndex)
# Stow.
import haystack
self.old_debug = settings.DEBUG
settings.DEBUG = True
self.old_site = haystack.site
haystack.site = self.site
self.sb.setup()
self.raw_whoosh = self.sb.index
self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
self.sqs = SearchQuerySet(site=self.site)
# Wipe it clean.
self.sqs.query.backend.clear()
# Fake indexing.
mock = MockModel()
mock.id = 1
self.sb.update(self.wrtsi, [mock])
def tearDown(self):
self.sqs.query.backend.clear()
settings.HAYSTACK_WHOOSH_STORAGE = self.old_whoosh_storage
import haystack
haystack.site = self.old_site
settings.DEBUG = self.old_debug
super(LiveWhooshRamStorageTestCase, self).tearDown()
def test_ram_storage(self):
results = self.sqs.filter(id='core.mockmodel.1')
# Sanity check.
self.assertEqual(results.count(), 1)
# Check the individual fields.
result = results[0]
self.assertEqual(result.id, 'core.mockmodel.1')
self.assertEqual(result.text, 'This is some example text.')
self.assertEqual(result.name, 'Mister Pants')
self.assertEqual(result.is_active, True)
self.assertEqual(result.post_count, 25)
self.assertEqual(result.average_rating, 3.6)
self.assertEqual(result.pub_date, datetime(2009, 11, 21, 0, 0))
self.assertEqual(result.created, datetime(2009, 11, 21, 21, 31, 00))
self.assertEqual(result.tags, ['staff', 'outdoor', 'activist', 'scientist'])
self.assertEqual(result.sites, [u'3', u'5', u'1'])
self.assertEqual(result.empty_list, [])
示例14: WhooshSearchBackendTestCase
class WhooshSearchBackendTestCase(TestCase):
def setUp(self):
super(WhooshSearchBackendTestCase, self).setUp()
# Stow.
temp_path = os.path.join('tmp', 'test_whoosh_query')
self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH', temp_path)
settings.HAYSTACK_WHOOSH_PATH = temp_path
self.site = WhooshSearchSite()
self.sb = SearchBackend(site=self.site)
self.smmi = WhooshMockSearchIndex(MockModel, backend=self.sb)
self.wmtmmi = WhooshMaintainTypeMockSearchIndex(MockModel, backend=self.sb)
self.site.register(MockModel, WhooshMockSearchIndex)
# With the models registered, you get the proper bits.
import haystack
# Stow.
self.old_site = haystack.site
haystack.site = self.site
self.sb.setup()
self.raw_whoosh = self.sb.index
self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
self.sb.delete_index()
self.sample_objs = []
for i in xrange(1, 4):
mock = MockModel()
mock.id = i
mock.author = 'daniel%s' % i
mock.pub_date = date(2009, 2, 25) - timedelta(days=i)
self.sample_objs.append(mock)
def tearDown(self):
if os.path.exists(settings.HAYSTACK_WHOOSH_PATH):
shutil.rmtree(settings.HAYSTACK_WHOOSH_PATH)
settings.HAYSTACK_WHOOSH_PATH = self.old_whoosh_path
# Restore.
import haystack
haystack.site = self.old_site
super(WhooshSearchBackendTestCase, self).tearDown()
def whoosh_search(self, query):
self.raw_whoosh = self.raw_whoosh.refresh()
searcher = self.raw_whoosh.searcher()
return searcher.search(self.parser.parse(query))
def test_update(self):
self.sb.update(self.smmi, self.sample_objs)
# Check what Whoosh thinks is there.
self.assertEqual(len(self.whoosh_search(u'*')), 3)
self.assertEqual([dict(doc) for doc in self.whoosh_search(u'*')], [{'django_id': u'3', 'django_ct': u'core.mockmodel', 'name': u'daniel3', 'text': u'Indexed!\n3', 'pub_date': u'2009-02-22T00:00:00', 'id': u'core.mockmodel.3'}, {'django_id': u'2', 'django_ct': u'core.mockmodel', 'name': u'daniel2', 'text': u'Indexed!\n2', 'pub_date': u'2009-02-23T00:00:00', 'id': u'core.mockmodel.2'}, {'django_id': u'1', 'django_ct': u'core.mockmodel', 'name': u'daniel1', 'text': u'Indexed!\n1', 'pub_date': u'2009-02-24T00:00:00', 'id': u'core.mockmodel.1'}])
def test_remove(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search(u'*')), 3)
self.sb.remove(self.sample_objs[0])
self.assertEqual(len(self.whoosh_search(u'*')), 2)
self.assertEqual([dict(doc) for doc in self.whoosh_search(u'*')], [{'django_id': u'3', 'django_ct': u'core.mockmodel', 'name': u'daniel3', 'text': u'Indexed!\n3', 'pub_date': u'2009-02-22T00:00:00', 'id': u'core.mockmodel.3'}, {'django_id': u'2', 'django_ct': u'core.mockmodel', 'name': u'daniel2', 'text': u'Indexed!\n2', 'pub_date': u'2009-02-23T00:00:00', 'id': u'core.mockmodel.2'}])
def test_clear(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search(u'*')), 3)
self.sb.clear()
self.raw_whoosh = self.sb.index
self.assertEqual(self.raw_whoosh.doc_count(), 0)
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search(u'*')), 3)
self.sb.clear([AnotherMockModel])
self.assertEqual(len(self.whoosh_search(u'*')), 3)
self.sb.clear([MockModel])
self.raw_whoosh = self.sb.index
self.assertEqual(self.raw_whoosh.doc_count(), 0)
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search(u'*')), 3)
self.sb.clear([AnotherMockModel, MockModel])
self.raw_whoosh = self.sb.index
self.assertEqual(self.raw_whoosh.doc_count(), 0)
def test_search(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search(u'*')), 3)
# No query string should always yield zero results.
self.assertEqual(self.sb.search(u''), {'hits': 0, 'results': []})
#.........这里部分代码省略.........
示例15: WhooshBoostBackendTestCase
class WhooshBoostBackendTestCase(TestCase):
def setUp(self):
super(WhooshBoostBackendTestCase, self).setUp()
# Stow.
temp_path = os.path.join('tmp', 'test_whoosh_query')
self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH', temp_path)
settings.HAYSTACK_WHOOSH_PATH = temp_path
self.site = SearchSite()
self.sb = SearchBackend(site=self.site)
self.smmi = WhooshBoostMockSearchIndex(AFourthMockModel, backend=self.sb)
self.site.register(AFourthMockModel, WhooshBoostMockSearchIndex)
# With the models registered, you get the proper bits.
import haystack
# Stow.
self.old_site = haystack.site
haystack.site = self.site
self.sb.setup()
self.raw_whoosh = self.sb.index
self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
self.sb.delete_index()
self.sample_objs = []
for i in xrange(1, 5):
mock = AFourthMockModel()
mock.id = i
if i % 2:
mock.author = 'daniel'
mock.editor = 'david'
else:
mock.author = 'david'
mock.editor = 'daniel'
mock.pub_date = date(2009, 2, 25) - timedelta(days=i)
self.sample_objs.append(mock)
def tearDown(self):
if os.path.exists(settings.HAYSTACK_WHOOSH_PATH):
shutil.rmtree(settings.HAYSTACK_WHOOSH_PATH)
settings.HAYSTACK_WHOOSH_PATH = self.old_whoosh_path
# Restore.
import haystack
haystack.site = self.old_site
def test_boost(self):
self.sb.update(self.smmi, self.sample_objs)
self.raw_whoosh = self.raw_whoosh.refresh()
searcher = self.raw_whoosh.searcher()
self.assertEqual(len(searcher.search(self.parser.parse(u'*'), limit=1000)), 4)
results = SearchQuerySet().filter(SQ(author='daniel') | SQ(editor='daniel'))
self.assertEqual([result.id for result in results], [
'core.afourthmockmodel.1',
'core.afourthmockmodel.3',
'core.afourthmockmodel.2',
'core.afourthmockmodel.4'
])