4
votes

I struggle to add my streamfield to the wagtail search index. This affects boths the available or the custom blocks.

From what I've found in the mailing list, a custom block should implement get_searchable_content which all blocks do.

here is my model which I'd like to index:

class BlogPage(Page):
    author = models.ForeignKey(User, on_delete=models.PROTECT)
    date = models.DateField("Post date")
    main_category = models.ForeignKey('blog.BlogCategory', related_name='main_category', default=1, on_delete=models.PROTECT)
    categories = ParentalManyToManyField('blog.BlogCategory', blank=True, related_name='categories')
    readtime = models.IntegerField(blank=True, null=True)
    subtitle = models.CharField(
        verbose_name=_('subtitle'),
        max_length=255,
        help_text=_("The page subtitle as you'd like it to be seen by the public - also the blog post teaser."),
    )
    body = StreamField([
        ('heading', general.TitleBlock()),
        ('paragraph', blocks.RichTextBlock()),
        ('image', general.FillImageChooserBlock()),
        ('subtitle', general.SubTitleBlock()),
        ('pullquote', general.PullQuoteBlock()),
        ('blockquote', general.BlockQuoteBlock()),
    ])
    cover = models.ForeignKey(
        'wagtailimages.Image',
        on_delete=models.PROTECT,
        related_name='+'
    )

    content_panels = Page.content_panels + [
        MultiFieldPanel([
            FieldPanel('subtitle'),
            FieldPanel('date'),
            FieldPanel('main_category', widget=forms.Select),
            FieldPanel('categories', widget=forms.CheckboxSelectMultiple),
            ImageChooserPanel('cover'),
            FieldPanel('author'),
        ], heading="Blog information"),
        StreamFieldPanel('body'),
    ]

    search_fields = Page.search_fields + [
        index.FilterField('date'),
        index.FilterField('main_category'),
        index.SearchField('body'),
        index.SearchField('subtitle'),
    ]

    def get_context(self, request):
        context = super().get_context(request)
        context['posts'] = BlogPage.objects.exclude(id=self.id).order_by('-date')[:10]
        return context

    def save(self, *args, **kwargs):
        if self.body.stream_data:
            self.readtime = read_time_as_minutes(self.body.stream_data)
        return super().save(*args, **kwargs)

thx for any hints :)

1
Are you getting an error, or is the streamfield just not returned in the search results? Also, which search backend are you using?dentemm
Hey dentemm, I just don't get results, so no error message. I didn't configure a search backend so far, so I guess it's SQLite?elmcrest

1 Answers

6
votes

Since you have not yet configured a search backend, you are using the default database backend. This only allows you to search the default fields, any custom fields you want to use in search need a more advanced search engine. This is explained here.