From a31ff2e5d84cc1ce22588a4fac8e4fefad4dae1c Mon Sep 17 00:00:00 2001
From: Chris Truter <crisptrutski@users.noreply.github.com>
Date: Wed, 13 Nov 2024 14:11:45 +0200
Subject: [PATCH] Fix search query issue with trailing quotation mark (#49933)

---
 src/metabase/search/postgres/index.clj       | 3 +++
 test/metabase/search/postgres/index_test.clj | 4 +++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/metabase/search/postgres/index.clj b/src/metabase/search/postgres/index.clj
index e80fd33ab6f..72139deb0ef 100644
--- a/src/metabase/search/postgres/index.clj
+++ b/src/metabase/search/postgres/index.clj
@@ -179,6 +179,8 @@
 (defn- process-phrase [word-or-phrase]
   ;; a phrase is quoted even if the closing quotation mark has not been typed yet
   (cond
+    ;; trailing quotation mark
+    (= word-or-phrase "\"") nil
     ;; quoted phrases must be matched sequentially
     (str/starts-with? word-or-phrase "\"")
     (as-> word-or-phrase <>
@@ -206,6 +208,7 @@
   (->> words-and-phrases
        (remove #{"and"})
        (map process-phrase)
+       (remove str/blank?)
        (str/join " & ")))
 
 (defn- complete-last-word
diff --git a/test/metabase/search/postgres/index_test.clj b/test/metabase/search/postgres/index_test.clj
index 408431d5b3b..b916f8336c2 100644
--- a/test/metabase/search/postgres/index_test.clj
+++ b/test/metabase/search/postgres/index_test.clj
@@ -190,7 +190,9 @@
 
   (testing "unbalanced quotes"
     (is (= "'big' <-> 'data' & 'big' <-> 'mistake':*"
-           (search-expr "\"Big Data\" \"Big Mistake"))))
+           (search-expr "\"Big Data\" \"Big Mistake")))
+    (is (= "'something'"
+           (search-expr "something \""))))
 
   (is (= "'partial' <-> 'quoted' <-> 'and' <-> 'or' <-> '-split':*"
          (search-expr "\"partial quoted AND OR -split")))
-- 
GitLab