From 622a68f695c3f8e2a9736cd9cf281c49d1b1b8ec 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'." Signed-off-by: Fridolin Somers Signed-off-by: Nick Clemens Signed-off-by: Jonathan Druart (cherry picked from commit 1ff9160a620519e1626044ff7d9205a0015ac1e4) Signed-off-by: Fridolin Somers (cherry picked from commit 988e1198298f3281850613d34f2c48e748915c12) Signed-off-by: Andrew Fuerste-Henry --- 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 bb5763e990..235b96f7a9 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -943,6 +943,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 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 => 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.39.5