0

我有一个 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

我的意见.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,
    })

我的模型.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并且找不到任何类型-o。知道我做错了什么吗?

4

1 回答 1

1

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

通常,字段集会因一种页面类型而异,并且只有fields=[...]在您正在寻找一种特定页面类型中的特定内容时才会包含在搜索中:

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

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

于 2021-08-23T09:21:04.923 回答