本文整理汇总了Python中haystack.backends.whoosh_backend.SearchQuery.set_result_class方法的典型用法代码示例。如果您正苦于以下问题:Python SearchQuery.set_result_class方法的具体用法?Python SearchQuery.set_result_class怎么用?Python SearchQuery.set_result_class使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类haystack.backends.whoosh_backend.SearchQuery
的用法示例。
在下文中一共展示了SearchQuery.set_result_class方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: WhooshSearchQueryTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchQuery [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchQuery import set_result_class [as 别名]
#.........这里部分代码省略.........
def test_build_query_multiple_words_and(self):
self.sq.add_filter(SQ(content="hello"))
self.sq.add_filter(SQ(content="world"))
self.assertEqual(self.sq.build_query(), u"(hello AND world)")
def test_build_query_multiple_words_not(self):
self.sq.add_filter(~SQ(content="hello"))
self.sq.add_filter(~SQ(content="world"))
self.assertEqual(self.sq.build_query(), u"(NOT (hello) AND NOT (world))")
def test_build_query_multiple_words_or(self):
self.sq.add_filter(SQ(content="hello") | SQ(content="world"))
self.assertEqual(self.sq.build_query(), u"(hello OR world)")
def test_build_query_multiple_words_mixed(self):
self.sq.add_filter(SQ(content="why") | SQ(content="hello"))
self.sq.add_filter(~SQ(content="world"))
self.assertEqual(self.sq.build_query(), u"((why OR hello) AND NOT (world))")
def test_build_query_phrase(self):
self.sq.add_filter(SQ(content="hello world"))
self.assertEqual(self.sq.build_query(), '"hello world"')
def test_build_query_boost(self):
self.sq.add_filter(SQ(content="hello"))
self.sq.add_boost("world", 5)
self.assertEqual(self.sq.build_query(), "hello world^5")
def test_build_query_multiple_filter_types(self):
self.sq.add_filter(SQ(content="why"))
self.sq.add_filter(SQ(pub_date__lte=datetime.datetime(2009, 2, 10, 1, 59)))
self.sq.add_filter(SQ(author__gt="daniel"))
self.sq.add_filter(SQ(created__lt=datetime.datetime(2009, 2, 12, 12, 13)))
self.sq.add_filter(SQ(title__gte="B"))
self.sq.add_filter(SQ(id__in=[1, 2, 3]))
self.sq.add_filter(SQ(rating__range=[3, 5]))
self.assertEqual(
self.sq.build_query(),
u'(why AND pub_date:[TO 20090210T015900] AND author:{daniel TO} AND created:{TO 20090212T121300} AND title:[B TO] AND (id:"1" OR id:"2" OR id:"3") AND rating:[3 TO 5])',
)
def test_build_query_in_filter_multiple_words(self):
self.sq.add_filter(SQ(content="why"))
self.sq.add_filter(SQ(title__in=["A Famous Paper", "An Infamous Article"]))
self.assertEqual(self.sq.build_query(), u'(why AND (title:"A Famous Paper" OR title:"An Infamous Article"))')
def test_build_query_in_filter_datetime(self):
self.sq.add_filter(SQ(content="why"))
self.sq.add_filter(SQ(pub_date__in=[datetime.datetime(2009, 7, 6, 1, 56, 21)]))
self.assertEqual(self.sq.build_query(), u'(why AND (pub_date:"20090706T015621"))')
def test_build_query_wildcard_filter_types(self):
self.sq.add_filter(SQ(content="why"))
self.sq.add_filter(SQ(title__startswith="haystack"))
self.assertEqual(self.sq.build_query(), u"(why AND title:haystack*)")
def test_clean(self):
self.assertEqual(self.sq.clean("hello world"), "hello world")
self.assertEqual(self.sq.clean("hello AND world"), "hello and world")
self.assertEqual(
self.sq.clean('hello AND OR NOT TO + - && || ! ( ) { } [ ] ^ " ~ * ? : \ world'),
"hello and or not to '+' '-' '&&' '||' '!' '(' ')' '{' '}' '[' ']' '^' '\"' '~' '*' '?' ':' '\\' world",
)
self.assertEqual(
self.sq.clean("so please NOTe i am in a bAND and bORed"), "so please NOTe i am in a bAND and bORed"
)
def test_build_query_with_models(self):
self.sq.add_filter(SQ(content="hello"))
self.sq.add_model(MockModel)
self.assertEqual(self.sq.build_query(), "(hello) AND (django_ct:core.mockmodel)")
self.sq.add_model(AnotherMockModel)
self.assertEqual(
self.sq.build_query(), u"(hello) AND (django_ct:core.anothermockmodel OR django_ct:core.mockmodel)"
)
def test_build_query_with_datetime(self):
self.sq.add_filter(SQ(pub_date=datetime.datetime(2009, 5, 9, 16, 20)))
self.assertEqual(self.sq.build_query(), u"pub_date:20090509T162000")
def test_build_query_with_sequence_and_filter_not_in(self):
self.sq.add_filter(SQ(id__exact=[1, 2, 3]))
self.assertEqual(self.sq.build_query(), u"id:1,2,3")
def test_set_result_class(self):
# Assert that we're defaulting to ``SearchResult``.
self.assertTrue(issubclass(self.sq.result_class, SearchResult))
# Custom class.
class IttyBittyResult(object):
pass
self.sq.set_result_class(IttyBittyResult)
self.assertTrue(issubclass(self.sq.result_class, IttyBittyResult))
# Reset to default.
self.sq.set_result_class(None)
self.assertTrue(issubclass(self.sq.result_class, SearchResult))
示例2: WhooshSearchQueryTestCase
# 需要导入模块: from haystack.backends.whoosh_backend import SearchQuery [as 别名]
# 或者: from haystack.backends.whoosh_backend.SearchQuery import set_result_class [as 别名]
#.........这里部分代码省略.........
settings.HAYSTACK_WHOOSH_PATH = self.old_whoosh_path
super(WhooshSearchQueryTestCase, self).tearDown()
def test_build_query_all(self):
self.assertEqual(self.sq.build_query(), '*')
def test_build_query_single_word(self):
self.sq.add_filter(SQ(content='hello'))
self.assertEqual(self.sq.build_query(), 'hello')
def test_build_query_multiple_words_and(self):
self.sq.add_filter(SQ(content='hello'))
self.sq.add_filter(SQ(content='world'))
self.assertEqual(self.sq.build_query(), u'(hello AND world)')
def test_build_query_multiple_words_not(self):
self.sq.add_filter(~SQ(content='hello'))
self.sq.add_filter(~SQ(content='world'))
self.assertEqual(self.sq.build_query(), u'(NOT (hello) AND NOT (world))')
def test_build_query_multiple_words_or(self):
self.sq.add_filter(SQ(content='hello') | SQ(content='world'))
self.assertEqual(self.sq.build_query(), u'(hello OR world)')
def test_build_query_multiple_words_mixed(self):
self.sq.add_filter(SQ(content='why') | SQ(content='hello'))
self.sq.add_filter(~SQ(content='world'))
self.assertEqual(self.sq.build_query(), u'((why OR hello) AND NOT (world))')
def test_build_query_phrase(self):
self.sq.add_filter(SQ(content='hello world'))
self.assertEqual(self.sq.build_query(), '"hello world"')
def test_build_query_boost(self):
self.sq.add_filter(SQ(content='hello'))
self.sq.add_boost('world', 5)
self.assertEqual(self.sq.build_query(), "hello world^5")
def test_build_query_multiple_filter_types(self):
self.sq.add_filter(SQ(content='why'))
self.sq.add_filter(SQ(pub_date__lte=datetime.datetime(2009, 2, 10, 1, 59)))
self.sq.add_filter(SQ(author__gt='daniel'))
self.sq.add_filter(SQ(created__lt=datetime.datetime(2009, 2, 12, 12, 13)))
self.sq.add_filter(SQ(title__gte='B'))
self.sq.add_filter(SQ(id__in=[1, 2, 3]))
self.sq.add_filter(SQ(rating__range=[3, 5]))
self.assertEqual(self.sq.build_query(), u'(why AND pub_date:[to 20090210015900] AND author:{daniel to} AND created:{to 20090212121300} AND title:[B to] AND (id:"1" OR id:"2" OR id:"3") AND rating:[3 to 5])')
def test_build_query_in_filter_multiple_words(self):
self.sq.add_filter(SQ(content='why'))
self.sq.add_filter(SQ(title__in=["A Famous Paper", "An Infamous Article"]))
self.assertEqual(self.sq.build_query(), u'(why AND (title:"A Famous Paper" OR title:"An Infamous Article"))')
def test_build_query_in_filter_datetime(self):
self.sq.add_filter(SQ(content='why'))
self.sq.add_filter(SQ(pub_date__in=[datetime.datetime(2009, 7, 6, 1, 56, 21)]))
self.assertEqual(self.sq.build_query(), u'(why AND (pub_date:"20090706015621"))')
def test_build_query_wildcard_filter_types(self):
self.sq.add_filter(SQ(content='why'))
self.sq.add_filter(SQ(title__startswith='haystack'))
self.assertEqual(self.sq.build_query(), u'(why AND title:haystack*)')
def test_clean(self):
self.assertEqual(self.sq.clean('hello world'), 'hello world')
self.assertEqual(self.sq.clean('hello AND world'), 'hello and world')
self.assertEqual(self.sq.clean('hello AND OR NOT TO + - && || ! ( ) { } [ ] ^ " ~ * ? : \ world'), 'hello and or not to \'+\' \'-\' \'&&\' \'||\' \'!\' \'(\' \')\' \'{\' \'}\' \'[\' \']\' \'^\' \'"\' \'~\' \'*\' \'?\' \':\' \'\\\' world')
self.assertEqual(self.sq.clean('so please NOTe i am in a bAND and bORed'), 'so please NOTe i am in a bAND and bORed')
def test_build_query_with_models(self):
self.sq.add_filter(SQ(content='hello'))
self.sq.add_model(MockModel)
self.assertEqual(self.sq.build_query(), '(hello) AND (django_ct:core.mockmodel)')
self.sq.add_model(AnotherMockModel)
self.assertEqual(self.sq.build_query(), u'(hello) AND (django_ct:core.anothermockmodel OR django_ct:core.mockmodel)')
def test_build_query_with_datetime(self):
self.sq.add_filter(SQ(pub_date=datetime.datetime(2009, 5, 9, 16, 20)))
self.assertEqual(self.sq.build_query(), u'pub_date:20090509162000')
def test_build_query_with_sequence_and_filter_not_in(self):
self.sq.add_filter(SQ(id__exact=[1, 2, 3]))
self.assertEqual(self.sq.build_query(), u'id:1,2,3')
def test_set_result_class(self):
# Assert that we're defaulting to ``SearchResult``.
self.assertTrue(issubclass(self.sq.result_class, SearchResult))
# Custom class.
class IttyBittyResult(object):
pass
self.sq.set_result_class(IttyBittyResult)
self.assertTrue(issubclass(self.sq.result_class, IttyBittyResult))
# Reset to default.
self.sq.set_result_class(None)
self.assertTrue(issubclass(self.sq.result_class, SearchResult))