From 96b94d01d9af35bb172a6d54d8fd58afeedfb7b0 Mon Sep 17 00:00:00 2001
From: bbeniamin <beniamin@noom.com>
Date: Wed, 11 Jan 2023 14:38:01 +0200
Subject: [PATCH] Fix parsing of queries with multiple filters that contain
 quotes

---
 CHANGELOG.txt                        | 1 +
 CONTRIBUTORS.rst                     | 1 +
 docs/releases/4.2.md                 | 1 +
 wagtail/search/tests/test_queries.py | 8 ++++++++
 wagtail/search/utils.py              | 2 +-
 5 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index b619a6e2a0..73a29984f1 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -69,6 +69,7 @@ Changelog
  * Fix: Use the correct color for placeholders in rich text fields (Thibaud Colas)
  * Fix: Prevent obstructing the outline around rich text fields (Thibaud Colas)
  * Fix: Page editor dropdowns now use indigo backgrounds like elsewhere in the admin interface (Thibaud Colas)
+ * Fix: Allow parsing of multiple key/value pairs from string in `wagtail.search.utils.parse_query_string` (Beniamin Bucur)
  * Docs: Add custom permissions section to permissions documentation page (Dan Hayden)
  * Docs: Add documentation for how to get started with contributing translations for the Wagtail admin (Ogunbanjo Oluwadamilare)
  * Docs: Officially recommend `fnm` over `nvm` in development documentation (LB (Ben) Johnston)
diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst
index b1d6eaebc0..ba8aa85279 100644
--- a/CONTRIBUTORS.rst
+++ b/CONTRIBUTORS.rst
@@ -683,6 +683,7 @@ Contributors
 * Alex Simpson
 * GLEF1X
 * Nick Lee
+* Beniamin Bucur
 
 Translators
 ===========
diff --git a/docs/releases/4.2.md b/docs/releases/4.2.md
index 62e5aa3dec..e67aa44629 100644
--- a/docs/releases/4.2.md
+++ b/docs/releases/4.2.md
@@ -106,6 +106,7 @@ This feature was developed by Matt Westcott, and sponsored by [YouGov](https://y
  * Use the correct color for placeholders in rich text fields (Thibaud Colas)
  * Prevent obstructing the outline around rich text fields (Thibaud Colas)
  * Page editor dropdowns now use indigo backgrounds like elsewhere in the admin interface (Thibaud Colas)
+ * Allow parsing of multiple key/value pairs from string in `wagtail.search.utils.parse_query_string` (Beniamin Bucur)
 
 ### Documentation
 
diff --git a/wagtail/search/tests/test_queries.py b/wagtail/search/tests/test_queries.py
index 60e41fd096..6f1da55f41 100644
--- a/wagtail/search/tests/test_queries.py
+++ b/wagtail/search/tests/test_queries.py
@@ -248,6 +248,14 @@ class TestSeparateFiltersFromQuery(SimpleTestCase):
         self.assertDictEqual(filters, {"author": "foo bar", "bar": "beer"})
         self.assertEqual(query, "hello world")
 
+    def test_two_filters_with_quotation_marks_and_query(self):
+        filters, query = separate_filters_from_query(
+            'author:"foo bar" hello world bar:"two beers"'
+        )
+
+        self.assertDictEqual(filters, {"author": "foo bar", "bar": "two beers"})
+        self.assertEqual(query, "hello world")
+
 
 class TestParseQueryString(SimpleTestCase):
     def test_simple_query(self):
diff --git a/wagtail/search/utils.py b/wagtail/search/utils.py
index a3d3049c29..3e20b3dd5d 100644
--- a/wagtail/search/utils.py
+++ b/wagtail/search/utils.py
@@ -82,7 +82,7 @@ def normalise_query_string(query_string):
 
 
 def separate_filters_from_query(query_string):
-    filters_regexp = r'(\w+):(\w+|".+")'
+    filters_regexp = r'(\w+):(\w+|"[^"]+")'
 
     filters = {}
     for match_object in re.finditer(filters_regexp, query_string):