From 82ddf9bfb0c57fe80c0aca28de28db2a83db1c61 Mon Sep 17 00:00:00 2001
From: Alex Arnaud <alex.arnaud@biblibre.com>
Date: Thu, 29 Aug 2019 15:06:14 +0000
Subject: [PATCH] Bug 23521: Put parentheses arround limit terms
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Test plan:
  - Use Elasticsearch 6 (you'll need Bug 18969),
  - create a biblio (#1) with "Dillinger Girl" in author and what you
    want in title,
  - create another biblio (#2) with the word "girl" in the title and
    "Dillinger Escaplan" as author
  - reindex
  - search * and refine on "Dillinger Girl"
  - Ko => Biblio #1 and #2 appear
  - Apply this patch,
  - search * and refine on "Dillinger Girl"
  - Ok => anly biblio #1 appears
  - use Elasticsearch 5 again
  - check for no search regression

Signed-off-by: Séverine QUEUNE <severine.queune@bulac.fr>
Signed-off-by: Séverine QUEUNE <severine.queune@bulac.fr>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Ere Maijala <ere.maijala@helsinki.fi>
---
 Koha/SearchEngine/Elasticsearch/QueryBuilder.pm  |  8 +++++++-
 .../SearchEngine/Elasticsearch/QueryBuilder.t    | 16 +++++++++++++++-
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
index 3e63086730..2ce8328562 100644
--- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
+++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
@@ -968,7 +968,13 @@ sub _fix_limit_special_cases {
             push @new_lim, 'onloan:0';
         }
         else {
-            push @new_lim, $l;
+            my ( $field, $term ) = $l =~ /^\s*([\w,-]*?):(.*)/;
+            if ( defined($field) && defined($term) ) {
+                push @new_lim, "$field:($term)";
+            }
+            else {
+                push @new_lim, $l;
+            }
         }
     }
     return \@new_lim;
diff --git a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t
index fd983498aa..1afa8d8c29 100644
--- a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t
+++ b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t
@@ -192,7 +192,7 @@ subtest 'build_authorities_query_compat() tests' => sub {
 };
 
 subtest 'build_query tests' => sub {
-    plan tests => 40;
+    plan tests => 42;
 
     my $qb;
 
@@ -437,6 +437,20 @@ subtest 'build_query tests' => sub {
         '(title:"donald duck")',
         "query of specific field is not added AND suppress:0"
     );
+
+    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan'] );
+    is(
+        $query->{query}{query_string}{query},
+        '(title:"donald duck") AND author:(Dillinger Escaplan)',
+        "Simplle query with limit's term in parentheses"
+    );
+
+    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan', 'itype:BOOK'] );
+    is(
+        $query->{query}{query_string}{query},
+        '(title:"donald duck") AND (author:(Dillinger Escaplan)) AND (itype:(BOOK))',
+        "Simplle query with each limit's term in parentheses"
+    );
     is($query_cgi, 'idx=&q=title%3A%22donald%20duck%22', 'query cgi');
     is($query_desc, 'title:"donald duck"', 'query desc ok');
 };
-- 
2.17.1