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

(-)a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm (+15 lines)
Lines 720-730 to ensure those parts are correct. Link Here
720
sub _clean_search_term {
720
sub _clean_search_term {
721
    my ( $self, $term ) = @_;
721
    my ( $self, $term ) = @_;
722
722
723
    # Lookahead for checking if we are inside quotes
724
    my $lookahead = '(?=(?:[^\"]*+\"[^\"]*+\")*+[^\"]*+$)';
725
723
    # Some hardcoded searches (like with authorities) produce things like
726
    # Some hardcoded searches (like with authorities) produce things like
724
    # 'an=123', when it ought to be 'an:123' for our purposes.
727
    # 'an=123', when it ought to be 'an:123' for our purposes.
725
    $term =~ s/=/:/g;
728
    $term =~ s/=/:/g;
729
726
    $term = $self->_convert_index_strings_freeform($term);
730
    $term = $self->_convert_index_strings_freeform($term);
727
    $term =~ s/[{}]/"/g;
731
    $term =~ s/[{}]/"/g;
732
733
    # Remove unbalanced quotes
734
    my $unquoted = $term;
735
    my $count = ($unquoted =~ tr/"/ /);
736
    if ($count % 2 == 1) {
737
        $term = $unquoted;
738
    }
739
740
    # Remove unquoted colons that have whitespace on either side of them
741
    $term =~ s/(\:[:\s]+|[:\s]+:)$lookahead//g;
742
728
    return $term;
743
    return $term;
729
}
744
}
730
745
(-)a/t/Koha/SearchEngine/Elasticsearch/QueryBuilder.t (-3 / +39 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 2;
20
use Test::More tests => 3;
21
21
22
use Koha::SearchEngine::Elasticsearch::QueryBuilder;
22
use Koha::SearchEngine::Elasticsearch::QueryBuilder;
23
23
Lines 83-86 subtest '_split_query() tests' => sub { Link Here
83
    is_deeply(\@res, \@exp, 'quoted search terms surrounded by spaces correctly');
83
    is_deeply(\@res, \@exp, 'quoted search terms surrounded by spaces correctly');
84
};
84
};
85
85
86
1;
86
subtest '_clean_search_term() tests' => sub {
87
    plan tests => 10;
88
89
    my $qb;
90
    ok(
91
        $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX }),
92
        'Creating a new QueryBuilder object'
93
    );
94
95
    my $res = $qb->_clean_search_term('an=123');
96
    is($res, 'an:123', 'equals sign replaced with colon');
97
98
    $res = $qb->_clean_search_term('"balanced quotes"');
99
    is($res, '"balanced quotes"', 'balanced quotes returned correctly');
100
101
    $res = $qb->_clean_search_term('unbalanced quotes"');
102
    is($res, 'unbalanced quotes ', 'unbalanced quotes removed');
103
104
    $res = $qb->_clean_search_term('"unbalanced "quotes"');
105
    is($res, ' unbalanced  quotes ', 'unbalanced quotes removed');
106
107
    $res = $qb->_clean_search_term('test : query');
108
    is($res, 'test query', 'dangling colon removed');
109
110
    $res = $qb->_clean_search_term('test :: query');
111
    is($res, 'test query', 'dangling double colon removed');
112
113
    $res = $qb->_clean_search_term('test "another : query"');
114
    is($res, 'test "another : query"', 'quoted dangling colon not removed');
115
116
    $res = $qb->_clean_search_term('test {another part}');
117
    is($res, 'test "another part"', 'curly brackets replaced correctly');
118
119
    $res = $qb->_clean_search_term('test {another part');
120
    is($res, 'test  another part', 'unbalanced curly brackets replaced correctly');
121
};
122
123
1;
87
- 

Return to bug 22228