Browse Source

Workaround for MySQL search backend bug (#610)

Workaround for: https://github.com/wagtail/wagtail/issues/11273

Basically, using `.type()` + `.search()` + MySQL search backend +
paginate results, creates a bug in which there are no search results.

* Run a slightly different query to avoid the bug which returns no
search results.
* Also add a fallback search result template for vanilla Wagtail pages
which do not inherit from CoderedPage.
Vince Salvino 1 year ago
parent
commit
e7f786761d
2 changed files with 13 additions and 1 deletions
  1. 4 0
      coderedcms/templates/coderedcms/pages/search.html
  2. 9 1
      coderedcms/views.py

+ 4 - 0
coderedcms/templates/coderedcms/pages/search.html

@@ -63,7 +63,11 @@
   {% for page in results_paginated %}
   <div class="mb-5">
     {% with page=page.specific %}
+    {% if page.search_template %}
     {% include page.search_template %}
+    {% else %}
+    {% include "coderedcms/pages/search_result.html" %}
+    {% endif %}
     {% endwith %}
   </div>
   {% endfor %}

+ 9 - 1
coderedcms/views.py

@@ -22,6 +22,8 @@ from django.views.decorators.http import require_POST
 from icalendar import Calendar
 from wagtail.admin import messages
 from wagtail.models import Page, get_page_models
+from wagtail.search.backends import get_search_backend
+from wagtail.search.backends.database.mysql.mysql import MySQLSearchBackend
 from coderedcms import utils
 from coderedcms.forms import SearchForm
 from coderedcms.models import CoderedPage, LayoutSettings
@@ -61,7 +63,13 @@ def search(request):
                 model = ContentType.objects.get(
                     model=search_model
                 ).model_class()
-                results = results.type(model)
+                # Workaround for Wagtail MySQL search bug.
+                # See: https://github.com/wagtail/wagtail/issues/11273
+                backend = get_search_backend()
+                if type(backend) is MySQLSearchBackend:
+                    results = model.objects.live()
+                else:
+                    results = results.type(model)
             except ContentType.DoesNotExist:
                 # Maintain existing behavior of only returning objects if the page type is real
                 results = None