Bug 18374: (QA follow-up) Fix auto truncation for field:"value"
authorJulian Maurice <julian.maurice@biblibre.com>
Fri, 13 Oct 2017 10:05:08 +0000 (10:05 +0000)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Fri, 13 Oct 2017 16:57:47 +0000 (13:57 -0300)
Signed-off-by: Julian Maurice <julian.maurice@biblibre.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
t/db_dependent/Koha_SearchEngine_Elasticsearch_Search.t

index b8dc257..7092e0f 100644 (file)
@@ -795,9 +795,9 @@ operands and double quoted strings.
 sub _truncate_terms {
     my ( $self, $query ) = @_;
 
-    # '"donald duck" "the mouse" and peter" get split into
-    # ['', '"donald duck"', '', ' ', '', '"the mouse"', '', ' ', 'and', ' ', 'pete']
-    my @tokens = split /("[^"]+"|\s+)/, $query;
+    # '"donald duck" title:"the mouse" and peter" get split into
+    # ['', '"donald duck"', '', ' ', '', 'title:"the mouse"', '', ' ', 'and', ' ', 'pete']
+    my @tokens = split /((?:\w+:)?"[^"]+"|\s+)/, $query;
 
     # Filter out empty tokens
     my @words = grep { $_ !~ /^\s*$/ } @tokens;
@@ -806,7 +806,7 @@ sub _truncate_terms {
     # terminated by '*' and not a keyword
     my @terms = map {
         my $w = $_;
-        (/^"/ or /\*$/ or grep {lc($w) eq $_} qw/and or not/) ? $_ : "$_*";
+        (/"$/ or /\*$/ or grep {lc($w) eq $_} qw/and or not/) ? $_ : "$_*";
     } @words;
 
     return join ' ', @terms;
index b54a5f0..74d5069 100644 (file)
@@ -81,7 +81,7 @@ subtest 'json2marc' => sub {
 };
 
 subtest 'build_query tests' => sub {
-    plan tests => 15;
+    plan tests => 16;
 
     t::lib::Mocks::mock_preference('DisplayLibraryFacets','both');
     my $query = $builder->build_query();
@@ -171,5 +171,12 @@ subtest 'build_query tests' => sub {
         '(barcode:123456*)',
         "query of specific field is truncated"
     );
+
+    ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'] );
+    is(
+        $query->{query}{query_string}{query},
+        '(title:"donald duck")',
+        "query of specific field is not truncated when surrouned by quotes"
+    );
 };