From ab363966b39b7b5010391dd82fcccaf6017ca0a0 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 27 Oct 2020 11:26:53 +0000 Subject: [PATCH] Bug 24567: Don't strip spaces along with dangling colons MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This updates the regex used for removing colons to capture those with space on either side, and remove the colon while preserving the space To test: 1 - Have Koha using ES 2 - Search for: ti:chess AND chess 3 - You should get a result in sample data, otherwise replace 'chess' with a title in your catalogue 4 - Search for: ti:chess AND kw:chess 5 - No result 6 - Enable DumpTemplateVarsIntranet and DumpSearchQueryTemplate 7 - Repeate search and check page source 8 - search_query has: title:chess ANDchess 9 - Apply patch 10 - Repeat 11 - Seaerch works! 12 - query is now: title:chess AND chess Signed-off-by: Séverine QUEUNE Signed-off-by: Joonas Kylmälä Bug 24567: (follow-up) Use dollar sign to refer to captures Signed-off-by: Joonas Kylmälä Signed-off-by: Jonathan Druart (cherry picked from commit 6ce56f1134d99280af30e128f57189b285966180) Signed-off-by: Andrew Fuerste-Henry --- Koha/SearchEngine/Elasticsearch/QueryBuilder.pm | 2 +- t/Koha/SearchEngine/Elasticsearch/QueryBuilder.t | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index 8e331d2fb8..605feb5e6c 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -941,7 +941,7 @@ sub _clean_search_term { } # Remove unquoted colons that have whitespace on either side of them - $term =~ s/(\:[:\s]+|[:\s]+:)$lookahead//g; + $term =~ s/((:+)(\s+)|(\s+)(:+))$lookahead/$3$4/g; $term = $self->_query_regex_escape_process($term); diff --git a/t/Koha/SearchEngine/Elasticsearch/QueryBuilder.t b/t/Koha/SearchEngine/Elasticsearch/QueryBuilder.t index 2057138431..3e515bb340 100644 --- a/t/Koha/SearchEngine/Elasticsearch/QueryBuilder.t +++ b/t/Koha/SearchEngine/Elasticsearch/QueryBuilder.t @@ -187,7 +187,7 @@ subtest '_split_query() tests' => sub { }; subtest '_clean_search_term() tests' => sub { - plan tests => 10; + plan tests => 11; my $qb; ok( @@ -208,10 +208,10 @@ subtest '_clean_search_term() tests' => sub { is($res, ' unbalanced quotes ', 'unbalanced quotes removed'); $res = $qb->_clean_search_term('test : query'); - is($res, 'test query', 'dangling colon removed'); + is($res, 'test query', 'dangling colon removed'); $res = $qb->_clean_search_term('test :: query'); - is($res, 'test query', 'dangling double colon removed'); + is($res, 'test query', 'dangling double colon removed'); $res = $qb->_clean_search_term('test "another : query"'); is($res, 'test "another : query"', 'quoted dangling colon not removed'); @@ -221,6 +221,9 @@ subtest '_clean_search_term() tests' => sub { $res = $qb->_clean_search_term('test {another part'); is($res, 'test another part', 'unbalanced curly brackets replaced correctly'); + + $res = $qb->_clean_search_term('ti:test AND kw:test'); + is($res, 'title:test AND test', 'ti converted to title, kw converted to empty string, dangling colon removed with space preserved'); }; subtest '_join_queries' => sub { -- 2.39.5