Exclude singleton pages from search results

This commit is contained in:
Jake Howard 2022-10-04 21:12:55 +01:00
parent 27a5d8891b
commit c09669693f
Signed by: jake
GPG Key ID: 57AFB45680EDD477

View File

@ -10,8 +10,7 @@ from wagtail.models import Page
from wagtail.search.utils import parse_query_string
from website.common.models import BaseContentPage
from website.common.utils import TocEntry
from website.home.models import HomePage
from website.common.utils import TocEntry, get_page_models
from .serializers import MIN_SEARCH_LENGTH, SearchParamsSerializer
@ -22,6 +21,9 @@ class SearchPage(RoutablePageMixin, BaseContentPage):
parent_page_types = ["home.HomePage"]
PAGE_SIZE = 15
# Exclude singleton pages from search results
EXCLUDED_PAGE_TYPES = {page for page in get_page_models() if page.max_count == 1}
@cached_property
def show_reading_time(self) -> bool:
return False
@ -65,7 +67,7 @@ class SearchPage(RoutablePageMixin, BaseContentPage):
pages = (
Page.objects.live()
.public()
.not_type(self.__class__, HomePage)
.not_type(self.__class__, *self.EXCLUDED_PAGE_TYPES)
.search(query, order_by_relevance=True)
)