Browse Source

Fix postgres phrase search to respect language set in settings

Ihor Marhitych 1 year ago
parent
commit
c550173d6c

+ 1 - 0
CHANGELOG.txt

@@ -42,6 +42,7 @@ Changelog
  * Fix: Prevent snippets model index view from crashing when a model does not have an `objects` manager (Jhonatan Lopes)
  * Fix: Fix `get_dummy_request`'s resulting host name when running tests with `ALLOWED_HOSTS = ["*"]` (David Buxton)
  * Fix: Fix timezone handling in the `timesince_last_update` template tag (Matt Westcott)
+ * Fix: Fix Postgres phrase search to respect the language set in settings (Ihar Marhitych)
  * Docs: Add contributing development documentation on how to work with a fork of Wagtail (Nix Asteri, Dan Braghis)
  * Docs: Make sure the settings panel is listed in tabbed interface examples (Tibor Leupold)
  * Docs: Update content and page names to their US spelling instead of UK spelling (Victoria Poromon)

+ 1 - 0
CONTRIBUTORS.md

@@ -810,6 +810,7 @@
 * David Buxton
 * Abdelrahman Hamada
 * Divdeploy
+* Ihar Marhitych
 
 ## Translators
 

+ 1 - 0
docs/releases/6.1.md

@@ -57,6 +57,7 @@ depth: 1
  * Prevent snippets model index view from crashing when a model does not have an `objects` manager (Jhonatan Lopes)
  * Fix `get_dummy_request`'s resulting host name when running tests with `ALLOWED_HOSTS = ["*"]` (David Buxton)
  * Fix timezone handling in the `timesince_last_update` template tag (Matt Westcott)
+ * Fix Postgres phrase search to respect the language set in settings (Ihar Marhitych)
 
 
 ### Documentation

+ 1 - 1
wagtail/search/backends/database/postgres/postgres.py

@@ -434,7 +434,7 @@ class PostgresSearchQueryCompiler(BaseSearchQueryCompiler):
             return SearchQuery(lexemes, search_type="raw", config=config)
 
         elif isinstance(query, Phrase):
-            return SearchQuery(query.query_string, search_type="phrase")
+            return SearchQuery(query.query_string, search_type="phrase", config=config)
 
         elif isinstance(query, Boost):
             # Not supported

+ 50 - 0
wagtail/search/tests/test_postgres_backend.py

@@ -4,6 +4,7 @@ from django.db import connection
 from django.test import TestCase
 from django.test.utils import override_settings
 
+from wagtail.search.query import Phrase
 from wagtail.search.tests.test_backends import BackendTests
 from wagtail.test.search import models
 
@@ -172,3 +173,52 @@ class TestPostgresSearchBackend(BackendTests, TestCase):
             [r.title for r in results],
             ["JavaScript: The good parts", "JavaScript: The Definitive Guide"],
         )
+
+
+@unittest.skipUnless(
+    connection.vendor == "postgresql", "The current database is not PostgreSQL"
+)
+@override_settings(
+    WAGTAILSEARCH_BACKENDS={
+        "default": {
+            "BACKEND": "wagtail.search.backends.database.postgres.postgres",
+            "SEARCH_CONFIG": "dutch",
+        }
+    }
+)
+class TestPostgresLanguageTextSearch(TestCase):
+    backend_path = "wagtail.search.backends.database.postgres.postgres"
+
+    def setUp(self):
+        # get search backend by backend_path
+        BackendTests.setUp(self)
+
+        book = models.Book.objects.create(
+            title="Nu is beter dan nooit",
+            publication_date="1999-05-01",
+            number_of_pages=333,
+        )
+        self.backend.add(book)
+        self.book = book
+
+    def test_search_language_plain_text(self):
+        results = self.backend.search("Nu is beter dan nooit", models.Book)
+        self.assertEqual(list(results), [self.book])
+
+        results = self.backend.search("is beter", models.Book)
+        self.assertEqual(list(results), [self.book])
+
+        # search deals even with variations
+        results = self.backend.search("zijn beter", models.Book)
+        self.assertEqual(list(results), [self.book])
+
+        # search deals even when there are minor typos
+        results = self.backend.search("zij beter dan", models.Book)
+        self.assertEqual(list(results), [self.book])
+
+    def test_search_language_phrase_text(self):
+        results = self.backend.search(Phrase("Nu is beter"), models.Book)
+        self.assertEqual(list(results), [self.book])
+
+        results = self.backend.search(Phrase("Nu zijn beter"), models.Book)
+        self.assertEqual(list(results), [self.book])

+ 0 - 40
wagtail/search/tests/test_postgres_stemming.py

@@ -1,40 +0,0 @@
-import unittest
-
-from django.conf import settings
-from django.db import connection
-from django.test import TestCase
-
-from wagtail.search.backends import get_search_backend
-from wagtail.test.search import models
-
-
-class TestPostgresStemming(TestCase):
-    def setUp(self):
-        backend_name = "wagtail.search.backends.database.postgres"
-        for conf in settings.WAGTAILSEARCH_BACKENDS.values():
-            if conf["BACKEND"] == backend_name:
-                break
-        else:
-            raise unittest.SkipTest("Only for %s" % backend_name)
-
-        self.backend = get_search_backend(backend_name)
-
-    def test_ru_stemming(self):
-        with connection.cursor() as cursor:
-            cursor.execute("SET default_text_search_config TO 'pg_catalog.russian'")
-
-        ru_book = models.Book.objects.create(
-            title="Голубое сало", publication_date="1999-05-01", number_of_pages=352
-        )
-        self.backend.add(ru_book)
-
-        results = self.backend.search("Голубое", models.Book)
-        self.assertEqual(list(results), [ru_book])
-
-        results = self.backend.search("Голубая", models.Book)
-        self.assertEqual(list(results), [ru_book])
-
-        results = self.backend.search("Голубой", models.Book)
-        self.assertEqual(list(results), [ru_book])
-
-        ru_book.delete()