From e1fa378ec50fde01b87d6ee2598f7360bb2795bf Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Fri, 13 Oct 2017 10:05:08 +0000 Subject: [PATCH] Bug 18374: (QA follow-up) Fix auto truncation for field:"value" Signed-off-by: Julian Maurice Signed-off-by: Jonathan Druart (cherry picked from commit c991ac6b6184e299108b5f69d3e0e50dcfc6548d) Signed-off-by: Fridolin Somers --- Koha/SearchEngine/Elasticsearch/QueryBuilder.pm | 8 ++++---- t/db_dependent/Koha_SearchEngine_Elasticsearch_Search.t | 9 ++++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index b8dc257278..7092e0fae4 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -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; diff --git a/t/db_dependent/Koha_SearchEngine_Elasticsearch_Search.t b/t/db_dependent/Koha_SearchEngine_Elasticsearch_Search.t index ba93e683be..d3d482b563 100644 --- a/t/db_dependent/Koha_SearchEngine_Elasticsearch_Search.t +++ b/t/db_dependent/Koha_SearchEngine_Elasticsearch_Search.t @@ -96,7 +96,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(); @@ -186,5 +186,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" + ); }; -- 2.39.5