本文整理汇总了Python中haystack.backends.whoosh_backend.SearchBackend.build_schema方法的典型用法代码示例。如果您正苦于以下问题:Python SearchBackend.build_schema方法的具体用法?Python SearchBackend.build_schema怎么用?Python SearchBackend.build_schema使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类haystack.backends.whoosh_backend.SearchBackend
的用法示例。
在下文中一共展示了SearchBackend.build_schema方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: WhooshSearchBackendTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import build_schema [as 别名]
#.........这里部分代码省略.........
results = self.sb.search(u"*", sort_by=["pub_date"])
self.assertEqual([result.pk for result in results["results"]], [u"3", u"2", u"1"])
results = self.sb.search(u"*", sort_by=["-pub_date"])
self.assertEqual([result.pk for result in results["results"]], [u"1", u"2", u"3"])
results = self.sb.search(u"*", sort_by=["id"])
self.assertEqual([result.pk for result in results["results"]], [u"1", u"2", u"3"])
results = self.sb.search(u"*", sort_by=["-id"])
self.assertEqual([result.pk for result in results["results"]], [u"3", u"2", u"1"])
def test__from_python(self):
self.assertEqual(self.sb._from_python("abc"), u"abc")
self.assertEqual(self.sb._from_python(1), u"1")
self.assertEqual(self.sb._from_python(2653), u"2653")
self.assertEqual(self.sb._from_python(25.5), u"25.5")
self.assertEqual(self.sb._from_python([1, 2, 3]), u"[1, 2, 3]")
self.assertEqual(self.sb._from_python((1, 2, 3)), u"(1, 2, 3)")
self.assertEqual(self.sb._from_python({"a": 1, "c": 3, "b": 2}), u"{'a': 1, 'c': 3, 'b': 2}")
self.assertEqual(self.sb._from_python(datetime(2009, 5, 9, 16, 14)), u"2009-05-09T16:14:00")
self.assertEqual(self.sb._from_python(datetime(2009, 5, 9, 0, 0)), u"2009-05-09T00:00:00")
self.assertEqual(self.sb._from_python(datetime(1899, 5, 18, 0, 0)), u"1899-05-18T00:00:00")
self.assertEqual(
self.sb._from_python(datetime(2009, 5, 18, 1, 16, 30, 250)), u"2009-05-18T01:16:30"
) # Sorry, we shed the microseconds.
def test__to_python(self):
self.assertEqual(self.sb._to_python("abc"), "abc")
self.assertEqual(self.sb._to_python("1"), 1)
self.assertEqual(self.sb._to_python("2653"), 2653)
self.assertEqual(self.sb._to_python("25.5"), 25.5)
self.assertEqual(self.sb._to_python("[1, 2, 3]"), [1, 2, 3])
self.assertEqual(self.sb._to_python("(1, 2, 3)"), (1, 2, 3))
self.assertEqual(self.sb._to_python('{"a": 1, "b": 2, "c": 3}'), {"a": 1, "c": 3, "b": 2})
self.assertEqual(self.sb._to_python("2009-05-09T16:14:00"), datetime(2009, 5, 9, 16, 14))
self.assertEqual(self.sb._to_python("2009-05-09T00:00:00"), datetime(2009, 5, 9, 0, 0))
self.assertEqual(self.sb._to_python(None), None)
def test_range_queries(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search(u"[d TO]")), 3)
self.assertEqual(len(self.whoosh_search(u"name:[d TO]")), 3)
self.assertEqual(len(self.whoosh_search(u"Ind* AND name:[d TO]")), 3)
self.assertEqual(len(self.whoosh_search(u"Ind* AND name:[TO c]")), 0)
def test_date_queries(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search(u"pub_date:2009\-02\-24T00\:00\:00")), 1)
self.assertEqual(len(self.whoosh_search(u"pub_date:2009\-08\-30T00\:00\:00")), 0)
self.assertEqual(len(self.whoosh_search(u"Ind* AND pub_date:[TO 2009\-02\-24T00\:00\:00]")), 3)
def test_escaped_characters_queries(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search(u"Indexed\!")), 3)
self.assertEqual(len(self.whoosh_search(u"http\:\/\/www\.example\.com")), 0)
def test_build_schema(self):
self.site.unregister(MockModel)
self.site.register(MockModel, AllTypesWhooshMockSearchIndex)
(content_field_name, schema) = self.sb.build_schema(self.site.all_searchfields())
self.assertEqual(content_field_name, "text")
self.assertEqual(len(schema._names), 8)
self.assertEqual(
schema._names, ["django_ct", "django_id", "id", "name", "pub_date", "seen_count", "sites", "text"]
)
self.assert_(isinstance(schema._by_name["text"], TEXT))
self.assert_(isinstance(schema._by_name["pub_date"], ID))
self.assert_(isinstance(schema._by_name["seen_count"], STORED))
self.assert_(isinstance(schema._by_name["sites"], KEYWORD))
def test_verify_type(self):
import haystack
haystack.site.unregister(MockModel)
haystack.site.register(MockModel, WhooshMaintainTypeMockSearchIndex)
self.sb.setup()
self.sb.update(self.wmtmmi, self.sample_objs)
self.assertEqual(self.sb.search(u"*")["hits"], 3)
self.assertEqual([result.month for result in self.sb.search(u"*")["results"]], [u"02", u"02", u"02"])
def test_writable(self):
if not os.path.exists(settings.HAYSTACK_WHOOSH_PATH):
os.makedirs(settings.HAYSTACK_WHOOSH_PATH)
os.chmod(settings.HAYSTACK_WHOOSH_PATH, 0400)
try:
self.sb.setup()
self.fail()
except IOError:
# Yay. We failed
pass
os.chmod(settings.HAYSTACK_WHOOSH_PATH, 0755)
示例2: WhooshSearchBackendTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import build_schema [as 别名]
#.........这里部分代码省略.........
self.assertEqual(self.sb._from_python({'a': 1, 'c': 3, 'b': 2}), u"{'a': 1, 'c': 3, 'b': 2}")
self.assertEqual(self.sb._from_python(datetime(2009, 5, 9, 16, 14)), datetime(2009, 5, 9, 16, 14))
self.assertEqual(self.sb._from_python(datetime(2009, 5, 9, 0, 0)), datetime(2009, 5, 9, 0, 0))
self.assertEqual(self.sb._from_python(datetime(1899, 5, 18, 0, 0)), datetime(1899, 5, 18, 0, 0))
self.assertEqual(self.sb._from_python(datetime(2009, 5, 18, 1, 16, 30, 250)), datetime(2009, 5, 18, 1, 16, 30, 250))
def test__to_python(self):
self.assertEqual(self.sb._to_python('abc'), 'abc')
self.assertEqual(self.sb._to_python('1'), 1)
self.assertEqual(self.sb._to_python('2653'), 2653)
self.assertEqual(self.sb._to_python('25.5'), 25.5)
self.assertEqual(self.sb._to_python('[1, 2, 3]'), [1, 2, 3])
self.assertEqual(self.sb._to_python('{"a": 1, "b": 2, "c": 3}'), {'a': 1, 'c': 3, 'b': 2})
self.assertEqual(self.sb._to_python('2009-05-09T16:14:00'), datetime(2009, 5, 9, 16, 14))
self.assertEqual(self.sb._to_python('2009-05-09T00:00:00'), datetime(2009, 5, 9, 0, 0))
self.assertEqual(self.sb._to_python(None), None)
def test_range_queries(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search(u'[d TO]')), 23)
self.assertEqual(len(self.whoosh_search(u'name:[d TO]')), 23)
self.assertEqual(len(self.whoosh_search(u'Ind* AND name:[d TO]')), 23)
self.assertEqual(len(self.whoosh_search(u'Ind* AND name:[TO c]')), 0)
def test_date_queries(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search(u"pub_date:20090717T003000")), 1)
self.assertEqual(len(self.whoosh_search(u"pub_date:20090717T000000")), 0)
self.assertEqual(len(self.whoosh_search(u'Ind* AND pub_date:[TO 20090717T003000]')), 3)
def test_escaped_characters_queries(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search(u"Indexed\!")), 23)
self.assertEqual(len(self.whoosh_search(u"http\:\/\/www\.example\.com")), 0)
def test_build_schema(self):
self.site.unregister(MockModel)
self.site.register(MockModel, AllTypesWhooshMockSearchIndex)
(content_field_name, schema) = self.sb.build_schema(self.site.all_searchfields())
self.assertEqual(content_field_name, 'text')
self.assertEqual(len(schema.names()), 8)
self.assertEqual(schema.names(), ['django_ct', 'django_id', 'id', 'name', 'pub_date', 'seen_count', 'sites', 'text'])
self.assert_(isinstance(schema._fields['text'], TEXT))
self.assert_(isinstance(schema._fields['pub_date'], DATETIME))
self.assert_(isinstance(schema._fields['seen_count'], NUMERIC))
self.assert_(isinstance(schema._fields['sites'], KEYWORD))
def test_verify_type(self):
import haystack
haystack.site.unregister(MockModel)
haystack.site.register(MockModel, WhooshMaintainTypeMockSearchIndex)
self.sb.setup()
self.sb.update(self.wmtmmi, self.sample_objs)
self.assertEqual(self.sb.search(u'*')['hits'], 23)
self.assertEqual([result.month for result in self.sb.search(u'*')['results']], [u'06', u'07', u'06', u'07', u'07', u'07', u'07', u'07', u'07', u'07', u'07', u'07', u'07', u'07', u'07', u'07', u'07', u'07', u'07', u'07', u'07', u'07', u'07'])
def test_writable(self):
if getattr(settings, 'HAYSTACK_WHOOSH_STORAGE', 'file') == 'file':
if not os.path.exists(settings.HAYSTACK_WHOOSH_PATH):
os.makedirs(settings.HAYSTACK_WHOOSH_PATH)
os.chmod(settings.HAYSTACK_WHOOSH_PATH, 0400)
try:
self.sb.setup()
self.fail()
except IOError:
# Yay. We failed
pass
os.chmod(settings.HAYSTACK_WHOOSH_PATH, 0755)
def test_slicing(self):
self.sb.update(self.smmi, self.sample_objs)
page_1 = self.sb.search(u'*', start_offset=0, end_offset=20)
page_2 = self.sb.search(u'*', start_offset=20, end_offset=30)
self.assertEqual(len(page_1['results']), 20)
self.assertEqual([result.pk for result in page_1['results']], [u'%s' % i for i in xrange(1, 21)])
self.assertEqual(len(page_2['results']), 3)
self.assertEqual([result.pk for result in page_2['results']], [u'21', u'22', u'23'])
# This used to throw an error.
page_0 = self.sb.search(u'*', start_offset=0, end_offset=0)
self.assertEqual(len(page_0['results']), 1)
def test_scoring(self):
self.sb.update(self.smmi, self.sample_objs)
page_1 = self.sb.search(u'index', start_offset=0, end_offset=20)
page_2 = self.sb.search(u'index', start_offset=20, end_offset=30)
self.assertEqual(len(page_1['results']), 20)
self.assertEqual(["%0.2f" % result.score for result in page_1['results']], ['0.51', '0.51', '0.51', '0.51', '0.51', '0.51', '0.51', '0.51', '0.51', '0.40', '0.40', '0.40', '0.40', '0.40', '0.40', '0.40', '0.40', '0.40', '0.40', '0.40'])
self.assertEqual(len(page_2['results']), 3)
self.assertEqual(["%0.2f" % result.score for result in page_2['results']], ['0.40', '0.40', '0.40'])
示例3: WhooshSearchBackendTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchBackend [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchBackend import build_schema [as 别名]
#.........这里部分代码省略.........
self.assertEqual(self.sb.index.doc_count(), 0)
def test_order_by(self):
self.sb.update(self.smmi, self.sample_objs)
results = self.sb.search(u'*', sort_by=['pub_date'])
self.assertEqual([result.pk for result in results['results']], [u'3', u'2', u'1'])
results = self.sb.search(u'*', sort_by=['-pub_date'])
self.assertEqual([result.pk for result in results['results']], [u'1', u'2', u'3'])
results = self.sb.search(u'*', sort_by=['id'])
self.assertEqual([result.pk for result in results['results']], [u'1', u'2', u'3'])
results = self.sb.search(u'*', sort_by=['-id'])
self.assertEqual([result.pk for result in results['results']], [u'3', u'2', u'1'])
def test__from_python(self):
self.assertEqual(self.sb._from_python('abc'), u'abc')
self.assertEqual(self.sb._from_python(1), u'1')
self.assertEqual(self.sb._from_python(2653), u'2653')
self.assertEqual(self.sb._from_python(25.5), u'25.5')
self.assertEqual(self.sb._from_python([1, 2, 3]), u'[1, 2, 3]')
self.assertEqual(self.sb._from_python((1, 2, 3)), u'(1, 2, 3)')
self.assertEqual(self.sb._from_python({'a': 1, 'c': 3, 'b': 2}), u"{'a': 1, 'c': 3, 'b': 2}")
self.assertEqual(self.sb._from_python(datetime(2009, 5, 9, 16, 14)), u'2009-05-09T16:14:00')
self.assertEqual(self.sb._from_python(datetime(2009, 5, 9, 0, 0)), u'2009-05-09T00:00:00')
self.assertEqual(self.sb._from_python(datetime(1899, 5, 18, 0, 0)), u'1899-05-18T00:00:00')
self.assertEqual(self.sb._from_python(datetime(2009, 5, 18, 1, 16, 30, 250)), u'2009-05-18T01:16:30') # Sorry, we shed the microseconds.
def test__to_python(self):
self.assertEqual(self.sb._to_python('abc'), 'abc')
self.assertEqual(self.sb._to_python('1'), 1)
self.assertEqual(self.sb._to_python('2653'), 2653)
self.assertEqual(self.sb._to_python('25.5'), 25.5)
self.assertEqual(self.sb._to_python('[1, 2, 3]'), [1, 2, 3])
self.assertEqual(self.sb._to_python('(1, 2, 3)'), (1, 2, 3))
self.assertEqual(self.sb._to_python('{"a": 1, "b": 2, "c": 3}'), {'a': 1, 'c': 3, 'b': 2})
self.assertEqual(self.sb._to_python('2009-05-09T16:14:00'), datetime(2009, 5, 9, 16, 14))
self.assertEqual(self.sb._to_python('2009-05-09T00:00:00'), datetime(2009, 5, 9, 0, 0))
self.assertEqual(self.sb._to_python(None), None)
def test_range_queries(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search(u'[d TO]')), 3)
self.assertEqual(len(self.whoosh_search(u'name:[d TO]')), 3)
self.assertEqual(len(self.whoosh_search(u'Ind* AND name:[d TO]')), 3)
self.assertEqual(len(self.whoosh_search(u'Ind* AND name:[TO c]')), 0)
def test_date_queries(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search(u"pub_date:2009\-02\-24T00\:00\:00")), 1)
self.assertEqual(len(self.whoosh_search(u"pub_date:2009\-08\-30T00\:00\:00")), 0)
self.assertEqual(len(self.whoosh_search(u'Ind* AND pub_date:[TO 2009\-02\-24T00\:00\:00]')), 3)
def test_escaped_characters_queries(self):
self.sb.update(self.smmi, self.sample_objs)
self.assertEqual(len(self.whoosh_search(u"Indexed\!")), 3)
self.assertEqual(len(self.whoosh_search(u"http\:\/\/www\.example\.com")), 0)
def test_build_schema(self):
self.site.unregister(MockModel)
self.site.register(MockModel, AllTypesWhooshMockSearchIndex)
(content_field_name, schema) = self.sb.build_schema(self.site.all_searchfields())
self.assertEqual(content_field_name, 'text')
self.assertEqual(len(schema._names), 8)
self.assertEqual(schema._names, ['django_ct', 'django_id', 'id', 'name', 'pub_date', 'seen_count', 'sites', 'text'])
self.assert_(isinstance(schema._by_name['text'], TEXT))
self.assert_(isinstance(schema._by_name['pub_date'], ID))
self.assert_(isinstance(schema._by_name['seen_count'], STORED))
self.assert_(isinstance(schema._by_name['sites'], KEYWORD))
def test_verify_type(self):
import haystack
haystack.site.unregister(MockModel)
haystack.site.register(MockModel, WhooshMaintainTypeMockSearchIndex)
self.sb.setup()
self.sb.update(self.wmtmmi, self.sample_objs)
self.assertEqual(self.sb.search(u'*')['hits'], 3)
self.assertEqual([result.month for result in self.sb.search(u'*')['results']], [u'02', u'02', u'02'])
def test_writable(self):
if not os.path.exists(settings.HAYSTACK_WHOOSH_PATH):
os.makedirs(settings.HAYSTACK_WHOOSH_PATH)
os.chmod(settings.HAYSTACK_WHOOSH_PATH, 0400)
try:
self.sb.setup()
self.fail()
except IOError:
# Yay. We failed
pass
os.chmod(settings.HAYSTACK_WHOOSH_PATH, 0755)