将流场添加到 Wagtail 中的搜索索引

Add streamfield to search index in Wagtail

我很难将流场添加到鹡鸰搜索索引中。这会影响可用块或自定义块。

根据我在邮件列表中发现的内容,自定义块应该实现 get_searchable_content 所有块都执行的操作。

这是我想要索引的模型:

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)

感谢任何提示:)

由于您尚未配置搜索后端,因此您使用的是默认数据库后端。这只允许您搜索默认字段,您要在搜索中使用的任何自定义字段都需要更高级的搜索引擎。这是解释here