View | Details | Raw Unified | Return to bug 28316
Collapse All | Expand All

(-)a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm (-3 / +24 lines)
Lines 943-956 sub _clean_search_term { Link Here
943
    $term =~ s/(\s+)(:+)$lookahead/$1/g;
943
    $term =~ s/(\s+)(:+)$lookahead/$1/g;
944
    $term =~ s/^://;
944
    $term =~ s/^://;
945
945
946
    $term = $self->_query_regex_escape_process($term);
947
946
    # replace all ranges with any square/curly brackets combinations to temporary substitutions (ex: "{a TO b]"" -> "~~LC~~a TO b~~RS~~")
948
    # replace all ranges with any square/curly brackets combinations to temporary substitutions (ex: "{a TO b]"" -> "~~LC~~a TO b~~RS~~")
947
    $term =~ s/(\{|\[)([^\s\[\]\{\}]+ TO [^\s\[\]\{\}]+)(\}|\])/'~~L'.($1 eq '[' ? 'S':'C').'~~'.$2.'~~R'.($3 eq ']' ? 'S':'C').'~~'/ge;
949
    $term =~ s/(\{|\[)([^\s\[\]\{\}]+ TO [^\s\[\]\{\}]+)(\}|\])/'~~L'.($1 eq '[' ? 'S':'C').'~~'.$2.'~~R'.($3 eq ']' ? 'S':'C').'~~'/ge;
948
    # screen all brackets with backslash
950
949
    $term =~ s/([\{\}\[\]])/\\$1/g;
951
    # store the content of regex before escaping brackets:
952
    my @saved_regexes;
953
    my $rgx_i = 0;
954
    while(
955
            $term =~ s@(
956
                (?<!\\)(?:[\\]{2})*/
957
                (?:[^/]+|(?<=\\)(?:[\\]{2})*/)+
958
                (?<!\\)(?:[\\]{2})*/
959
            )$lookahead@"~~XI$rgx_i~~"@ex
960
    ) {
961
        @saved_regexes[$rgx_i++] = $1;
962
    }
963
964
    $term =~ s/([\{\}\[\]])$lookahead/\\$1/g;
965
966
    # restore the regex content after escaping brackets:
967
    for (my $i = 0; $i < @saved_regexes; $i++) {
968
        $term =~ s/~~XI$i~~/$saved_regexes[$i]/;
969
    }
970
950
    # restore temporary weird substitutions back to normal brackets
971
    # restore temporary weird substitutions back to normal brackets
951
    $term =~ s/~~L(C|S)~~([^\s\[\]\{\}]+ TO [^\s\[\]\{\}]+)~~R(C|S)~~/($1 eq 'S' ? '[':'{').$2.($3 eq 'S' ? ']':'}')/ge;
972
    $term =~ s/~~L(C|S)~~([^\s\[\]\{\}]+ TO [^\s\[\]\{\}]+)~~R(C|S)~~/($1 eq 'S' ? '[':'{').$2.($3 eq 'S' ? ']':'}')/ge;
952
973
953
    $term = $self->_query_regex_escape_process($term);
974
    warn Koha::Nug::dumper([$term],['$term']);
954
975
955
    return $term;
976
    return $term;
956
}
977
}
(-)a/t/Koha/SearchEngine/Elasticsearch/QueryBuilder.t (-4 / +13 lines)
Lines 187-193 subtest '_split_query() tests' => sub { Link Here
187
};
187
};
188
188
189
subtest '_clean_search_term() tests' => sub {
189
subtest '_clean_search_term() tests' => sub {
190
    plan tests => 13;
190
    plan tests => 15;
191
191
192
    my $qb;
192
    my $qb;
193
    ok(
193
    ok(
Lines 219-230 subtest '_clean_search_term() tests' => sub { Link Here
219
    $res = $qb->_clean_search_term('test [123 TO 345]');
219
    $res = $qb->_clean_search_term('test [123 TO 345]');
220
    is($res, 'test [123 TO 345]', 'keep inculsive range untouched');
220
    is($res, 'test [123 TO 345]', 'keep inculsive range untouched');
221
221
222
    $res = $qb->_clean_search_term('test [test TO TEST} [and] {123 TO 456]');
222
    $res = $qb->_clean_search_term('test [test TO TEST} ["[and] {123 TO 456]" "[balanced]"]');
223
    is($res, 'test [test TO TEST} \[and\] {123 TO 456]', 'keep exclusive range untouched');
223
    is($res, 'test [test TO TEST} \["[and] {123 TO 456]" "[balanced]"\]', 'keep exclusive range untouched');
224
224
225
    $res = $qb->_clean_search_term('test[]test TO TEST] [ {123 to 345}}');
225
    $res = $qb->_clean_search_term('test[]test TO TEST] [ {123 to 345}}');
226
    is($res, 'test\[\]test TO TEST\] \[ \{123 to 345\}\}', 'screen all square and curly brackets');
226
    is($res, 'test\[\]test TO TEST\] \[ \{123 to 345\}\}', 'screen all square and curly brackets');
227
227
228
    t::lib::Mocks::mock_preference('QueryRegexEscapeOptions', 'escape');
229
230
    $res = $qb->_clean_search_term('test inside regexps /this [a-z]/ and \/not [a-z]\/ and that [a-z] [a TO z]');
231
    is($res, 'test inside regexps \/this \[a-z\]\/ and \/not \[a-z\]\/ and that \[a-z\] [a TO z]', 'test inside regexps with QueryRegexEscapeOptions parameter set to escape');
232
233
    t::lib::Mocks::mock_preference('QueryRegexEscapeOptions', 'dont_escape');
234
235
    $res = $qb->_clean_search_term('test inside regexps /this [a-z]/ /this2 [a-z]/ [but] /this3 [a-z]/ and \/not [a-z]\/ and that [a-z] [a TO z]');
236
    is($res, 'test inside regexps /this [a-z]/ /this2 [a-z]/ \[but\] /this3 [a-z]/ and \/not \[a-z\]\/ and that \[a-z\] [a TO z]', 'test inside regexps with QueryRegexEscapeOptions parameter set to dont_escape');
237
228
    $res = $qb->_clean_search_term('ti:test AND kw:test');
238
    $res = $qb->_clean_search_term('ti:test AND kw:test');
229
    is($res, 'title:test AND test', 'ti converted to title, kw converted to empty string, dangling colon removed with space preserved');
239
    is($res, 'title:test AND test', 'ti converted to title, kw converted to empty string, dangling colon removed with space preserved');
230
240
231
- 

Return to bug 28316