From b2931c62268e713605522977d6e1345ebe0c4172 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 --- 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 b54a5f0401..74d5069150 100644 --- a/t/db_dependent/Koha_SearchEngine_Elasticsearch_Search.t +++ b/t/db_dependent/Koha_SearchEngine_Elasticsearch_Search.t @@ -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" + ); }; -- 2.11.0