本文整理匯總了Python中wagtail.search.index.SearchField方法的典型用法代碼示例。如果您正苦於以下問題:Python index.SearchField方法的具體用法?Python index.SearchField怎麽用?Python index.SearchField使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類wagtail.search.index
的用法示例。
在下文中一共展示了index.SearchField方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_search_searchable_fields
# 需要導入模塊: from wagtail.search import index [as 別名]
# 或者: from wagtail.search.index import SearchField [as 別名]
def test_search_searchable_fields(self):
# Find root page
root_page = Page.objects.get(id=2)
# Create a page
root_page.add_child(instance=SimplePage(
title="Hi there!", slug='hello-world', content="good morning",
live=True,
has_unpublished_changes=False,
))
# Confirm the slug is not being searched
response = self.get({'q': "hello"})
self.assertNotContains(response, "There is one matching page")
search_fields = Page.search_fields
# Add slug to the search_fields
Page.search_fields = Page.search_fields + [SearchField('slug', partial_match=True)]
# Confirm the slug is being searched
response = self.get({'q': "hello"})
self.assertContains(response, "There is one matching page")
# Reset the search fields
Page.search_fields = search_fields
示例2: test_select_on_queryset_with_foreign_key
# 需要導入模塊: from wagtail.search import index [as 別名]
# 或者: from wagtail.search.index import SearchField [as 別名]
def test_select_on_queryset_with_foreign_key(self):
fields = index.RelatedFields('protagonist', [
index.SearchField('name'),
])
queryset = fields.select_on_queryset(Novel.objects.all())
# ForeignKey should be select_related
self.assertFalse(queryset._prefetch_related_lookups)
self.assertIn('protagonist', queryset.query.select_related)
示例3: test_select_on_queryset_with_one_to_one
# 需要導入模塊: from wagtail.search import index [as 別名]
# 或者: from wagtail.search.index import SearchField [as 別名]
def test_select_on_queryset_with_one_to_one(self):
fields = index.RelatedFields('book_ptr', [
index.SearchField('title'),
])
queryset = fields.select_on_queryset(Novel.objects.all())
# OneToOneField should be select_related
self.assertFalse(queryset._prefetch_related_lookups)
self.assertIn('book_ptr', queryset.query.select_related)
示例4: test_select_on_queryset_with_many_to_many
# 需要導入模塊: from wagtail.search import index [as 別名]
# 或者: from wagtail.search.index import SearchField [as 別名]
def test_select_on_queryset_with_many_to_many(self):
fields = index.RelatedFields('adverts', [
index.SearchField('title'),
])
queryset = fields.select_on_queryset(ManyToManyBlogPage.objects.all())
# ManyToManyField should be prefetch_related
self.assertIn('adverts', queryset._prefetch_related_lookups)
self.assertFalse(queryset.query.select_related)
示例5: test_select_on_queryset_with_reverse_foreign_key
# 需要導入模塊: from wagtail.search import index [as 別名]
# 或者: from wagtail.search.index import SearchField [as 別名]
def test_select_on_queryset_with_reverse_foreign_key(self):
fields = index.RelatedFields('categories', [
index.RelatedFields('category', [
index.SearchField('name')
])
])
queryset = fields.select_on_queryset(ManyToManyBlogPage.objects.all())
# reverse ForeignKey should be prefetch_related
self.assertIn('categories', queryset._prefetch_related_lookups)
self.assertFalse(queryset.query.select_related)
示例6: test_select_on_queryset_with_reverse_many_to_many
# 需要導入模塊: from wagtail.search import index [as 別名]
# 或者: from wagtail.search.index import SearchField [as 別名]
def test_select_on_queryset_with_reverse_many_to_many(self):
fields = index.RelatedFields('manytomanyblogpage', [
index.SearchField('title'),
])
queryset = fields.select_on_queryset(Advert.objects.all())
# reverse ManyToManyField should be prefetch_related
self.assertIn('manytomanyblogpage', queryset._prefetch_related_lookups)
self.assertFalse(queryset.query.select_related)
示例7: test_select_on_queryset_with_taggable_manager
# 需要導入模塊: from wagtail.search import index [as 別名]
# 或者: from wagtail.search.index import SearchField [as 別名]
def test_select_on_queryset_with_taggable_manager(self):
fields = index.RelatedFields('tags', [
index.SearchField('name'),
])
queryset = fields.select_on_queryset(Novel.objects.all())
# Tags should be prefetch_related
self.assertIn('tags', queryset._prefetch_related_lookups)
self.assertFalse(queryset.query.select_related)
示例8: test_basic
# 需要導入模塊: from wagtail.search import index [as 別名]
# 或者: from wagtail.search.index import SearchField [as 別名]
def test_basic(self):
cls = self.make_dummy_type([
index.SearchField('test', boost=100, partial_match=False),
index.FilterField('filter_test'),
])
self.assertEqual(len(cls.get_search_fields()), 2)
self.assertEqual(len(cls.get_searchable_search_fields()), 1)
self.assertEqual(len(cls.get_filterable_search_fields()), 1)
示例9: test_overriding
# 需要導入模塊: from wagtail.search import index [as 別名]
# 或者: from wagtail.search.index import SearchField [as 別名]
def test_overriding(self):
# If there are two fields with the same type and name
# the last one should override all the previous ones. This ensures that the
# standard convention of:
#
# class SpecificPageType(Page):
# search_fields = Page.search_fields + [some_other_definitions]
#
# ...causes the definitions in some_other_definitions to override Page.search_fields
# as intended.
cls = self.make_dummy_type([
index.SearchField('test', boost=100, partial_match=False),
index.SearchField('test', partial_match=True),
])
self.assertEqual(len(cls.get_search_fields()), 1)
self.assertEqual(len(cls.get_searchable_search_fields()), 1)
self.assertEqual(len(cls.get_filterable_search_fields()), 0)
field = cls.get_search_fields()[0]
self.assertIsInstance(field, index.SearchField)
# Boost should be reset to the default if it's not specified by the override
self.assertIsNone(field.boost)
# Check that the partial match was overridden
self.assertTrue(field.partial_match)
示例10: test_different_field_types_dont_override
# 需要導入模塊: from wagtail.search import index [as 別名]
# 或者: from wagtail.search.index import SearchField [as 別名]
def test_different_field_types_dont_override(self):
# A search and filter field with the same name should be able to coexist
cls = self.make_dummy_type([
index.SearchField('test', boost=100, partial_match=False),
index.FilterField('test'),
])
self.assertEqual(len(cls.get_search_fields()), 2)
self.assertEqual(len(cls.get_searchable_search_fields()), 1)
self.assertEqual(len(cls.get_filterable_search_fields()), 1)
示例11: get_search_fields
# 需要導入模塊: from wagtail.search import index [as 別名]
# 或者: from wagtail.search.index import SearchField [as 別名]
def get_search_fields(search_fields):
for search_field in search_fields:
if isinstance(search_field, SearchField):
yield search_field
elif isinstance(search_field, RelatedFields):
for sub_field in get_search_fields(search_field.fields):
yield sub_field
示例12: prepare_field
# 需要導入模塊: from wagtail.search import index [as 別名]
# 或者: from wagtail.search.index import SearchField [as 別名]
def prepare_field(self, obj, field):
if isinstance(field, SearchField):
yield (field, get_weight(field.boost),
self.prepare_value(field.get_value(obj)))
elif isinstance(field, AutocompleteField):
# AutocompleteField does not define a boost parameter, so use a base weight of 'D'
yield (field, 'D', self.prepare_value(field.get_value(obj)))
elif isinstance(field, RelatedFields):
sub_obj = field.get_value(obj)
if sub_obj is None:
return
if isinstance(sub_obj, Manager):
sub_objs = sub_obj.all()
else:
if callable(sub_obj):
sub_obj = sub_obj()
sub_objs = [sub_obj]
for sub_obj in sub_objs:
for sub_field in field.fields:
yield from self.prepare_field(sub_obj, sub_field)
示例13: title
# 需要導入模塊: from wagtail.search import index [as 別名]
# 或者: from wagtail.search.index import SearchField [as 別名]
def title(self):
"""
Returns all values to index as "title". This is the value of all SearchFields that have the field_name 'title'
"""
texts = []
for field in self.search_fields:
for current_field, boost, value in self.prepare_field(self.obj, field):
if isinstance(current_field, SearchField) and current_field.field_name == 'title':
texts.append((value, boost))
return self.as_vector(texts)
示例14: body
# 需要導入模塊: from wagtail.search import index [as 別名]
# 或者: from wagtail.search.index import SearchField [as 別名]
def body(self):
"""
Returns all values to index as "body". This is the value of all SearchFields excluding the title
"""
texts = []
for field in self.search_fields:
for current_field, boost, value in self.prepare_field(self.obj, field):
if isinstance(current_field, SearchField) and not current_field.field_name == 'title':
texts.append((value, boost))
return self.as_vector(texts)