From fe855843db9cc2e9e8f9849be16c1e3f1f1e974c Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 3 Feb 2021 10:33:35 +0100 Subject: [PATCH] Bug 27597: Remove leading colon in ES query If we are searching on kw there is a leading colon at the beginning of the generated query: kw:foo becomes :foo Note that it only happens when there is no other terms before. Test plan: 0. Don't apply the patch 1. Search for kw:foo 2. Notice the error Error: Unable to perform your search. Please try again. and the logs say Failed to parse query [(:foo*)] 3. Apply the patch 4. Repeat the search and notice that you know get: "12 result(s) found for 'kw:foo'." --- Koha/SearchEngine/Elasticsearch/QueryBuilder.pm | 1 + t/Koha/SearchEngine/Elasticsearch/QueryBuilder.t | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index d551ad93c1..174b93738a 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -947,6 +947,7 @@ sub _clean_search_term { # Remove unquoted colons that have whitespace on either side of them $term =~ s/(:+)(\s+)$lookahead/$2/g; $term =~ s/(\s+)(:+)$lookahead/$1/g; + $term =~ s/^://; $term = $self->_query_regex_escape_process($term); diff --git a/t/Koha/SearchEngine/Elasticsearch/QueryBuilder.t b/t/Koha/SearchEngine/Elasticsearch/QueryBuilder.t index 3e515bb340..b2ce6f4319 100755 --- 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 => 11; + plan tests => 12; my $qb; ok( @@ -224,6 +224,9 @@ subtest '_clean_search_term() tests' => sub { $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'); + + $res = $qb->_clean_search_term('kw:test'); + is($res, 'test', 'kw converted to empty string, dangling colon removed with space preserved'); }; subtest '_join_queries' => sub { -- 2.20.1