From 21b2ec2f04f16571b4fafc4edb42dfd702393a98 Mon Sep 17 00:00:00 2001 From: Petro Vashchuk Date: Thu, 10 Jun 2021 13:43:03 +0300 Subject: [PATCH] Bug 28316: escape ES ranges if QueryAutoTruncate is enabled if QueryAutoTruncate enabled we will have any special operators ruined for example: "test [6 TO 7]" will be converted to "test* [6* TO* 7]" so no reason to keep ranges when QueryAutoTruncate set to "enabled" --- Koha/SearchEngine/Elasticsearch/QueryBuilder.pm | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index 4843f8d2e0..380cc3456d 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -940,6 +940,16 @@ sub _clean_search_term { $term = $self->_query_regex_escape_process($term); + # because of _truncate_terms and if QueryAutoTruncate enabled + # we will have any special operators ruined by _truncate_terms: + # for ex. search for "test [6 TO 7]" will be converted to "test* [6* TO* 7]" + # so no reason to keep ranges in QueryAutoTruncate==true case: + my $truncate = C4::Context->preference("QueryAutoTruncate") || 0; + unless($truncate) { + # replace all ranges with any square/curly brackets combinations to temporary substitutions (ex: "{a TO b]"" -> "~~LC~~a TO b~~RS~~") + $term =~ s/(\{|\[)([^\s\[\]\{\}]+ TO [^\s\[\]\{\}]+)(\}|\])/'~~L'.($1 eq '[' ? 'S':'C').'~~'.$2.'~~R'.($3 eq ']' ? 'S':'C').'~~'/ge; + } + # save all regex contents away before escaping brackets: my @saved_regexes; my $rgx_i = 0; @@ -974,6 +984,11 @@ sub _clean_search_term { $term =~ s/~~XI$i~~/$saved_regexes[$i]/; } + unless($truncate) { + # restore temporary weird substitutions back to normal brackets + $term =~ s/~~L(C|S)~~([^\s\[\]\{\}]+ TO [^\s\[\]\{\}]+)~~R(C|S)~~/($1 eq 'S' ? '[':'{').$2.($3 eq 'S' ? ']':'}')/ge; + } + return $term; } -- 2.31.1