SearchFieldError - 无法使用字段 "body" 进行搜索。请将 index.SearchField('body') 添加到 Page.search_fields

SearchFieldError - Cannot search with field "body". Please add index.SearchField('body') to Page.search_fields

我有一个 elasticsearch 后端 (7.14.0) 和 wagtail (2.14.1),我想在我页面的正文字段中包含一个全文搜索。在前端搜索内容时,我得到

SearchFieldError at /search/
Cannot search with field "body". Please add index.SearchField('body') to 
Page.search_fields.
Request Method: GET
Request URL:    https://my.site/search/?query=impres
Django Version: 3.2.4
Exception Type: SearchFieldError
Exception Value:    
Cannot search with field "body". Please add index.SearchField('body') to         
Page.search_fields.
Exception Location: /var/www/vhosts/my.site/dev4/venv/lib/python3.6/site-packages/wagtail/search/backends/base.py, line 163, in check
Python Executable:  /var/www/vhosts/my.site/dev4/venv/bin/python
Python Version: 3.6.9
Python Path:    
['/var/www/vhosts/my.site/dev4/venv/bin',
 '/var/www/vhosts/my.site/dev4/mysite/mysite',
 '/var/www/vhosts/my.site/dev4/mysite',
 '/var/www/vhosts/my.site/dev4',
 '/usr/share/passenger/helper-scripts',
 '/usr/lib/python36.zip',
 '/usr/lib/python3.6',
 '/usr/lib/python3.6/lib-dynload',
 '/var/www/vhosts/my.site/dev4/venv/lib/python3.6/site-packages']
Server time:    Mon, 23 Aug 2021 05:24:18 +0000

我的views.py:

from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.template.response import TemplateResponse

from wagtail.core.models import Page
from wagtail.search.models import Query




def search(request):
    search_query = request.GET.get('query', None)
    page = request.GET.get('page', 1)

    # Search
    if search_query:
        #search_results = Page.objects.live().search(search_query)
        # exclude some search results
        search_results = Page.objects.live()\
        .exclude(title='Sie sind nun ausgeloggt.')\
        .exclude(title='Sie sind nun eingeloggt.')\
        .exclude(title='Passwort erfolgreich geändert.')\
        .search(search_query, fields=["title", "body"])

        query = Query.get(search_query)

        # Record hit
        query.add_hit()
    else:
        search_results = Page.objects.none()

    # Pagination
    paginator = Paginator(search_results, 10)
    try:
        search_results = paginator.page(page)
    except PageNotAnInteger:
        search_results = paginator.page(1)
    except EmptyPage:
        search_results = paginator.page(paginator.num_pages)

    return TemplateResponse(request, 'search/search.html', {
        'search_query': search_query,
        'search_results': search_results,
    })

我的models.py:

from django.db import models

from wagtail.core.models import Page
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel

from wagtail.search import index


class HomePage(Page):
    body = RichTextField(blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('body', classname="full"),
    ]

    # Search index configuration

    search_fields = Page.search_fields + [ # Inherit search_fields from Page
        index.SearchField('body'),
    ]

所以我已经 将 index.SearchField('body') 添加到 Page.search_fields,但找不到任何 type-o。 知道我做错了什么吗?

在搜索查询上设置 fields=["title", "body"] 在这里不起作用,因为您的搜索查询是 运行 基本 Page 模型(这是正确的做法,如果您希望搜索涵盖所有页面类型)- 这里的 body 字段特定于 HomePage 模型,因此一般无法识别。

通常,字段集会因一种页面类型而异,如果您要在一种特定页面类型中查找特定内容,则只能在搜索中包含 fields=[...]

EventPage.objects.search('london', fields=['location'])

对于通用搜索,您应该在搜索时省略 fields=[...] 参数 - 然后它将搜索所有页面类型中已命名为 SearchFields 的所有字段。您 应该index.SearchField('body') 行保留在主页定义中,并确保您在添加它后重新运行 ./manage.py update_index