我有一个 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。知道我做错了什么吗?