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

(-)a/Koha/SearchEngine/Elasticsearch/Search.pm (+20 lines)
Lines 127-132 sub count { Link Here
127
    return $result->{hits}->{total};
127
    return $result->{hits}->{total};
128
}
128
}
129
129
130
=head2 escape_query
131
132
    my $query = $searcher->escape_query($query);
133
134
This escapes characters reserved by the search engine. This allows searching for fields
135
that contain special characters.
136
137
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
138
139
=cut
140
141
sub escape_query {
142
    my ( $self, $query ) = @_;
143
144
    $query =~ s/(\+|\-|\=|\&\&|\|\||\!|\(|\)|\{|\}|\[|\]|\^|\"|\~|\*|\?|\:|\\|\/)/\\$1/g;
145
    $query =~ s/(<|>)/ /g;
146
147
    return $query;
148
}
149
130
=head2 search_compat
150
=head2 search_compat
131
151
132
    my ( $error, $results, $facets ) = $search->search_compat(
152
    my ( $error, $results, $facets ) = $search->search_compat(
(-)a/Koha/SearchEngine/Zebra/Search.pm (+13 lines)
Lines 53-58 sub search { Link Here
53
    }
53
    }
54
}
54
}
55
55
56
=head2 escape_query
57
58
This passes the query back unchanged as we have not found issues with
59
Zebra reserved characters
60
61
=cut
62
63
sub escape_query {
64
    my ($self,$query) = @_;
65
66
    return $query;
67
}
68
56
=head2 search_compat
69
=head2 search_compat
57
70
58
This passes straight through to C4::Search::getRecords.
71
This passes straight through to C4::Search::getRecords.
(-)a/catalogue/detail.pl (-2 / +6 lines)
Lines 127-134 if ( $xslfile ) { Link Here
127
    my $searcher = Koha::SearchEngine::Search->new(
127
    my $searcher = Koha::SearchEngine::Search->new(
128
        { index => $Koha::SearchEngine::BIBLIOS_INDEX }
128
        { index => $Koha::SearchEngine::BIBLIOS_INDEX }
129
    );
129
    );
130
    my $cleaned_title = $biblio->title;
130
131
    $cleaned_title =~ tr|/||;
131
    my $raw_title = $biblio->title;
132
    my $cleaned_title = $searcher->escape_query( $raw_title );
133
134
    $cleaned_title =~ tr|/||; # Trim trailing slash from title
135
132
    my $query =
136
    my $query =
133
      ( C4::Context->preference('UseControlNumber') and $record->field('001') )
137
      ( C4::Context->preference('UseControlNumber') and $record->field('001') )
134
      ? 'rcn:'. $record->field('001')->data . ' AND (bib-level:a OR bib-level:b)'
138
      ? 'rcn:'. $record->field('001')->data . ' AND (bib-level:a OR bib-level:b)'
(-)a/t/db_dependent/Koha/SearchEngine/Search.t (-2 / +18 lines)
Lines 4-10 Link Here
4
4
5
use Modern::Perl;
5
use Modern::Perl;
6
6
7
use Test::More tests => 1;
7
use Test::More tests => 2;
8
8
9
use MARC::Field;
9
use MARC::Field;
10
use MARC::Record;
10
use MARC::Record;
Lines 44-49 subtest 'Test extract_biblionumber' => sub { Link Here
44
    is( $bibno, 4567, 'Extracted biblio number for Zebra' );
44
    is( $bibno, 4567, 'Extracted biblio number for Zebra' );
45
};
45
};
46
46
47
subtest 'Test escape_query' => sub {
48
    plan tests => 2;
49
50
    my @engines = ('Zebra','Elasticsearch');
51
    my $expected = {
52
        Zebra => q{+ - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /},
53
        Elasticsearch => q{\\+ \\- \\= \\&& \\||     \\! \\( \\) \\{ \\} \\[ \\] \\^ \\" \\~ \\* \\? \\: \\\ \\/}
54
    };
55
56
    my $query = q{+ - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /};
57
    for my $engine ( @engines ){
58
        t::lib::Mocks::mock_preference( 'SearchEngine', $engine );
59
        my $searcher = Koha::SearchEngine::Search->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
60
        is( $searcher->escape_query( $query ), $expected->{$engine},"Query is escaped as expected");
61
    }
62
};
63
47
# -- Helper routine
64
# -- Helper routine
48
sub test_record {
65
sub test_record {
49
    my $marc = MARC::Record->new;
66
    my $marc = MARC::Record->new;
50
- 

Return to bug 28326