Python wagtail.search.index.SearchField() Examples

The following are 14 code examples of wagtail.search.index.SearchField(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module wagtail.search.index , or try the search function .
Example #1
Source File: test_page_search.py    From wagtail with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 
Example #2
Source File: test_related_fields.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #3
Source File: test_related_fields.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #4
Source File: test_related_fields.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #5
Source File: test_related_fields.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #6
Source File: test_related_fields.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #7
Source File: test_related_fields.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #8
Source File: test_indexed_class.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #9
Source File: test_indexed_class.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #10
Source File: test_indexed_class.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #11
Source File: utils.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #12
Source File: backend.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #13
Source File: backend.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #14
Source File: backend.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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)