From 192cf0d583dd22ea14c66bfab7c6697917ddd630 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Joonas=20Kylm=C3=A4l=C3=A4?= <joonas.kylmala@helsinki.fi>
Date: Tue, 24 Nov 2020 19:19:04 +0200
Subject: [PATCH] Bug 11175: Search using double quotes to support
 Elasticsearch

With Elasticsearch if you would use the search string here without the
change to double quotes, i.e.

(rcn='1234' AND cni='FI-XXX') OR rcn='FI-XXX 1234'

then the search would somehow manage to return a record with 001 =
1234 and 003 = FI-XXX. Using double quotes prevents that. Also using
parenthesis around the latter part of the OR seems to work:

(rcn='1234' AND cni='FI-XXX') OR (rcn='FI-XXX 1234')

Signed-off-by: Pasi Kallinen <pasi.kallinen@koha-suomi.fi>
---
 Koha/Util/Search.pm  | 6 +++---
 t/Koha/Util/Search.t | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/Koha/Util/Search.pm b/Koha/Util/Search.pm
index 4e1c9706a8..c9d9f62e59 100644
--- a/Koha/Util/Search.pm
+++ b/Koha/Util/Search.pm
@@ -45,11 +45,11 @@ sub get_component_part_query {
 
         if (!defined($pf003)) {
             # search for 773$w='Host001'
-            $searchstr = "rcn='".$pf001->data()."'";
+            $searchstr = "rcn=\"".$pf001->data()."\"";
         } else {
             # search for (773$w='Host001' and 003='Host003') or 773$w='Host003 Host001')
-            $searchstr = "(rcn='".$pf001->data()."' AND cni='".$pf003->data()."')";
-            $searchstr .= " OR rcn='".$pf003->data()." ".$pf001->data()."'";
+            $searchstr = "(rcn=\"".$pf001->data()."\" AND cni=\"".$pf003->data()."\")";
+            $searchstr .= " OR rcn=\"".$pf003->data()." ".$pf001->data()."\"";
         }
     }
 }
diff --git a/t/Koha/Util/Search.t b/t/Koha/Util/Search.t
index c56b9404e2..e5cde2900b 100755
--- a/t/Koha/Util/Search.t
+++ b/t/Koha/Util/Search.t
@@ -38,5 +38,5 @@ subtest 'get_component_part_query' => sub {
     $record->append_fields($marc_001_field);
     ModBiblioMarc($record, $biblionumber);
 
-    is(Koha::Util::Search::get_component_part_query($biblionumber), "rcn='$biblionumber'");
+    is(Koha::Util::Search::get_component_part_query($biblionumber), "rcn=\"$biblionumber\"");
 };
-- 
2.20.1