自定义搜索字段中的wagtail搜索无法正常工作

时间:2017-05-19 08:05:17

标签: elasticsearch django-models wagtail

我正在实施一个简单的博客。在博客页面的情况下,搜索还应该查看两个自定义字段'intro'和'body'。我的BlogPage模型如下:

class PageWithSidebar(Page):

    def get_context(self, request):
        context = super(PageWithSidebar, self).get_context(request)
        context['tags'] = BlogPageTag.objects.all().select_related().values('tag_id', 'tag_id__name').annotate(item_count=Count('tag_id')).order_by('-item_count')[:10]
        context['categories'] = BlogCategory.objects.values('name').annotate(Count('name')).values('name').order_by('name')
        context['recent_blogpages'] = Page.objects.filter(content_type__model='blogpage').filter(live='1').order_by('-first_published_at')

        return context

class BlogPage(PageWithSidebar):


date = models.DateField("Post date")
        intro = models.CharField(max_length=250)
        body = RichTextField(blank=True)
        tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
        categories = ParentalManyToManyField('blog.BlogCategory', blank=True)
        social_description = models.CharField(max_length=140, blank=True)

        def main_image(self):
            gallery_item = self.gallery_images.first()
            if gallery_item:
                return gallery_item.image
            else:
                return None

        def main_image_caption(self):
            gallery_item = self.gallery_images.first()
            if gallery_item:
                return gallery_item.caption
            else:
                return None

        search_fields = PageWithSidebar.search_fields + [
            index.SearchField('intro'),
            index.SearchField('body'),
        ]

        content_panels = PageWithSidebar.content_panels + [
            MultiFieldPanel([
                FieldPanel('date'),
                FieldPanel('tags'),
                FieldPanel('categories', widget=forms.CheckboxSelectMultiple),
                FieldPanel('social_description'),
            ], heading="Blog information"),
            FieldPanel('intro'),
            FieldPanel('body'),
            InlinePanel('gallery_images', label="Gallery images"),
        ]

搜索适用于“标题”字段,但不适用于两个自定义字段。如果我搜索“简介”或“正文”字段中包含的单词,我就得不到任何结果。

我缺少什么想法?

1 个答案:

答案 0 :(得分:0)

我不知道默认搜索后端不支持自定义字段。切换到elasticsearch后,包含了自定义字段。