本文整理汇总了Python中haystack.backends.whoosh_backend.SearchBackend.delete_index方法的典型用法代码示例。如果您正苦于以下问题:Python SearchBackend.delete_index方法的具体用法?Python SearchBackend.delete_index怎么用?Python SearchBackend.delete_index使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类haystack.backends.whoosh_backend.SearchBackend
的用法示例。
在下文中一共展示了SearchBackend.delete_index方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: LiveWhooshMultiSearchQuerySetTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import delete_index [as 别名]
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: LiveWhooshSearchQueryTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import delete_index [as 别名]
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')
示例3: LiveWhooshRoundTripTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import delete_index [as 别名]
class LiveWhooshRoundTripTestCase(TestCase):
def setUp(self):
super(LiveWhooshRoundTripTestCase, 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.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.sb.delete_index()
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):
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(LiveWhooshRoundTripTestCase, self).tearDown()
def test_round_trip(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, [])
示例4: WhooshSearchBackendTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import delete_index [as 别名]
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)
#.........这里部分代码省略.........
示例5: LiveWhooshSearchQuerySetTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import delete_index [as 别名]
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'])
#.........这里部分代码省略.........
示例6: LiveWhooshSearchQueryTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import delete_index [as 别名]
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
示例7: WhooshSearchBackendTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import delete_index [as 别名]
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",
#.........这里部分代码省略.........
示例8: LiveWhooshSearchQuerySetTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import delete_index [as 别名]
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 = WhooshSearchSite()
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 2009\-08\-31T00\:00\:00]")
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 2009\\-02\\-23T00\\:00\\:00]")
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 2009\\-02\\-25T00\\:00\\:00] AND (django_id:"1" OR django_id:"2") 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'"bell bronze" AND daler\\-rowney AND pearlescent')
self.assertEqual(len(sqs), 0)
def test_all_regression(self):
sqs = SearchQuerySet()
self.assertEqual([result.pk for result in sqs], [])
self.sb.update(self.smmi, self.sample_objs)
#.........这里部分代码省略.........
示例9: WhooshSearchBackendTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import delete_index [as 别名]
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': []})
#.........这里部分代码省略.........
示例10: WhooshBoostBackendTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import delete_index [as 别名]
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'
])
示例11: WhooshSearchBackendTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import delete_index [as 别名]
#.........这里部分代码省略.........
if os.path.exists(settings.HAYSTACK_WHOOSH_PATH):
index_files = os.listdir(settings.HAYSTACK_WHOOSH_PATH)
for index_file in index_files:
os.remove(os.path.join(settings.HAYSTACK_WHOOSH_PATH, index_file))
os.removedirs(settings.HAYSTACK_WHOOSH_PATH)
settings.HAYSTACK_WHOOSH_PATH = self.old_whoosh_path
super(WhooshSearchBackendTestCase, self).tearDown()
def whoosh_search(self, query):
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('*')), 3)
self.assertEqual([dict(doc) for doc in self.whoosh_search('*')], [{'django_id_s': u'3', 'django_ct_s': u'core.mockmodel', 'name': u'daniel3', 'text': u'Indexed!\n3', 'pub_date': u'2009-02-22', 'id': u'core.mockmodel.3'}, {'django_id_s': u'2', 'django_ct_s': u'core.mockmodel', 'name': u'daniel2', 'text': u'Indexed!\n2', 'pub_date': u'2009-02-23', 'id': u'core.mockmodel.2'}, {'django_id_s': u'1', 'django_ct_s': u'core.mockmodel', 'name': u'daniel1', 'text': u'Indexed!\n1', 'pub_date': u'2009-02-24', 'id': u'core.mockmodel.1'}])
def test_remove(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search('*')), 3)
self.sb.remove(self.sample_objs[0])
self.assertEqual(len(self.whoosh_search('*')), 2)
self.assertEqual([dict(doc) for doc in self.whoosh_search('*')], [{'django_id_s': u'3', 'django_ct_s': u'core.mockmodel', 'name': u'daniel3', 'text': u'Indexed!\n3', 'pub_date': u'2009-02-22', 'id': u'core.mockmodel.3'}, {'django_id_s': u'2', 'django_ct_s': u'core.mockmodel', 'name': u'daniel2', 'text': u'Indexed!\n2', 'pub_date': u'2009-02-23', 'id': u'core.mockmodel.2'}])
def test_clear(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search('*')), 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('*')), 3)
self.sb.clear([AnotherMockModel])
self.assertEqual(len(self.whoosh_search('*')), 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('*')), 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('*')), 3)
self.assertEqual(self.sb.search(''), [])
self.assertEqual(self.sb.search('*')['hits'], 3)
self.assertEqual([result.pk for result in self.sb.search('*')['results']], [u'3', u'2', u'1'])
self.assertEqual(self.sb.search('', highlight=True), [])
self.assertEqual(self.sb.search('Index*', highlight=True)['hits'], 3)
# DRL_FIXME: Uncomment once highlighting works.
# self.assertEqual([result.highlighted['text'][0] for result in self.sb.search('Index*', highlight=True)['results']], ['<em>Indexed</em>!\n3', '<em>Indexed</em>!\n2', '<em>Indexed</em>!\n1'])
self.assertEqual(self.sb.search('', facets=['name']), [])
results = self.sb.search('Index*', facets=['name'])
self.assertEqual(results['hits'], 3)
self.assertEqual(results['facets'], {})
self.assertEqual(self.sb.search('', date_facets={'pub_date': {'start_date': datetime.date(2008, 2, 26), 'end_date': datetime.date(2008, 2, 26), 'gap': '/MONTH'}}), [])
results = self.sb.search('Index*', date_facets={'pub_date': {'start_date': datetime.date(2008, 2, 26), 'end_date': datetime.date(2008, 2, 26), 'gap': '/MONTH'}})
self.assertEqual(results['hits'], 3)
self.assertEqual(results['facets'], {})
self.assertEqual(self.sb.search('', query_facets={'name': '[* TO e]'}), [])
results = self.sb.search('Index*', query_facets={'name': '[* TO e]'})
self.assertEqual(results['hits'], 3)
self.assertEqual(results['facets'], {})
# self.assertEqual(self.sb.search('', narrow_queries=['name:daniel1']), [])
# results = self.sb.search('Index*', narrow_queries=['name:daniel1'])
# self.assertEqual(results['hits'], 1)
def test_more_like_this(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search('*')), 3)
# Unsupported by Whoosh. Should see empty results.
self.assertEqual(self.sb.more_like_this(self.sample_objs[0])['hits'], 0)
def test_delete_index(self):
self.sb.update(self.smmi, self.sample_objs)
self.assert_(self.sb.index.doc_count() > 0)
self.sb.delete_index()
self.assertEqual(self.sb.index.doc_count(), 0)
示例12: LiveWhooshSearchQuerySetTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import delete_index [as 别名]
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 = WhooshSearchSite()
self.sb = SearchBackend(site=self.site)
self.smmi = WhooshMockSearchIndex(MockModel, backend=self.sb)
self.site.register(MockModel, WhooshMockSearchIndex)
# Stow.
import haystack
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
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 2009\-08\-31T00\:00\:00]')
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 2009\\-02\\-23T00\\:00\\:00]')
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 2009\\-02\\-25T00\\:00\\:00] AND (django_id:"1" OR django_id:"2") 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'"bell bronze" AND daler\\-rowney AND pearlescent')
self.assertEqual(len(sqs), 0)
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)