diff --git a/CHANGELOG.txt b/CHANGELOG.txt index b3ffcab860..304b47d91d 100644 --- a/CHANGELOG.txt +++ b/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) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 691a77a63e..626f5b5438 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -810,6 +810,7 @@ * David Buxton * Abdelrahman Hamada * Divdeploy +* Ihar Marhitych ## Translators diff --git a/docs/releases/6.1.md b/docs/releases/6.1.md index cd88f90e79..7d06c28b67 100644 --- a/docs/releases/6.1.md +++ b/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 diff --git a/wagtail/search/backends/database/postgres/postgres.py b/wagtail/search/backends/database/postgres/postgres.py index b8758c7b78..4d04313e1f 100644 --- a/wagtail/search/backends/database/postgres/postgres.py +++ b/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 diff --git a/wagtail/search/tests/test_postgres_backend.py b/wagtail/search/tests/test_postgres_backend.py index 6151e64637..a66a289d00 100644 --- a/wagtail/search/tests/test_postgres_backend.py +++ b/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]) diff --git a/wagtail/search/tests/test_postgres_stemming.py b/wagtail/search/tests/test_postgres_stemming.py deleted file mode 100644 index 7e6562ce52..0000000000 --- a/wagtail/search/tests/test_postgres_stemming.py +++ /dev/null @@ -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()