本文整理汇总了Python中haystack.backends.whoosh_backend.SearchBackend.clear方法的典型用法代码示例。如果您正苦于以下问题:Python SearchBackend.clear方法的具体用法?Python SearchBackend.clear怎么用?Python SearchBackend.clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类haystack.backends.whoosh_backend.SearchBackend
的用法示例。
在下文中一共展示了SearchBackend.clear方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_non_silent
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import clear [as 别名]
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
示例2: WhooshSearchBackendTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import clear [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)
#.........这里部分代码省略.........
示例3: WhooshSearchBackendTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import clear [as 别名]
#.........这里部分代码省略.........
"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": []})
# A one letter query string gets nabbed by a stopwords filter. Should
示例4: WhooshSearchBackendTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import clear [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': []})
#.........这里部分代码省略.........
示例5: WhooshSearchBackendTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import clear [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.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.raw_whoosh.delete_by_query(q=self.parser.parse('*'))
self.sample_objs = []
for i in xrange(1, 4):
mock = MockModel()
mock.id = i
mock.author = 'daniel%s' % i
mock.pub_date = datetime.date(2009, 2, 25) - datetime.timedelta(days=i)
self.sample_objs.append(mock)
def tearDown(self):
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)
#.........这里部分代码省略.........