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

(-)a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm (-2 / +2 lines)
Lines 696-702 sub _convert_sort_fields { Link Here
696
        author      => 'author',
696
        author      => 'author',
697
        call_number => 'cn-sort',
697
        call_number => 'cn-sort',
698
        popularity  => 'issues',
698
        popularity  => 'issues',
699
        relevance   => undef,       # default
699
        relevance   => '',       # default
700
        title       => 'title',
700
        title       => 'title',
701
        pubdate     => 'date-of-publication',
701
        pubdate     => 'date-of-publication',
702
    );
702
    );
Lines 707-713 sub _convert_sort_fields { Link Here
707
    grep { $_->{field} } map {
707
    grep { $_->{field} } map {
708
        my ( $f, $d ) = /(.+)_(.+)/;
708
        my ( $f, $d ) = /(.+)_(.+)/;
709
        {
709
        {
710
            field     => $sort_field_convert{$f},
710
            field     => $sort_field_convert{$f} // $f,
711
            direction => $sort_order_convert{$d}
711
            direction => $sort_order_convert{$d}
712
        }
712
        }
713
    } @sort_by;
713
    } @sort_by;
(-)a/Koha/Z3950Responder/GenericSession.pm (-2 / +28 lines)
Lines 1-6 Link Here
1
package Koha::Z3950Responder::GenericSession;
1
package Koha::Z3950Responder::GenericSession;
2
2
3
# Copyright The National Library of Finland 2018
3
# Copyright The National Library of Finland 2018-2019
4
#
4
#
5
# This file is part of Koha.
5
# This file is part of Koha.
6
#
6
#
Lines 61-67 sub start_search { Link Here
61
    my $query = $args->{RPN}->{'query'}->to_koha($self->{'attribute_mappings'}->{$database});
61
    my $query = $args->{RPN}->{'query'}->to_koha($self->{'attribute_mappings'}->{$database});
62
    $self->log_debug("    parsed search: $query");
62
    $self->log_debug("    parsed search: $query");
63
    my @operands = $query;
63
    my @operands = $query;
64
    (undef, $built_query) = $builder->build_query_compat( undef, \@operands, undef, undef, undef, 0);
64
    my $sort = $self->_convert_sort_rules($args->{SRW_SORTKEYS} // '');
65
66
    (undef, $built_query) = $builder->build_query_compat( undef, \@operands, undef, undef, $sort, 0);
65
67
66
    my ($error, $marcresults, $hits ) = $searcher->simple_search_compat($built_query, 0, $num_to_prefetch);
68
    my ($error, $marcresults, $hits ) = $searcher->simple_search_compat($built_query, 0, $num_to_prefetch);
67
    if (defined $error) {
69
    if (defined $error) {
Lines 108-111 sub fetch_record { Link Here
108
    return $resultset->{cached_results}[$offset - $resultset->{cached_offset}];
110
    return $resultset->{cached_results}[$offset - $resultset->{cached_offset}];
109
}
111
}
110
112
113
=head2 Internal methods
114
115
=head3 _convert_sort_rules
116
117
    my $sort = $self->_convert_sort_rules($srw_sortkeys);
118
119
Convert SRW_SORTKEYS from SimpleServer to Koha's format
120
121
=cut
122
123
sub _convert_sort_rules {
124
    my ( $self, $srw_sortkeys ) = @_;
125
126
    my @result;
127
    my @sortkeys = split(' ', $srw_sortkeys);
128
    foreach my $sortkey (@sortkeys) {
129
        my @parts = split(',', $sortkey);
130
        my $field = $parts[0];
131
        my $direction = $parts[2] // 1 == 1 ? 'asc' : 'desc';
132
        push @result, $field . '_' . $direction;
133
    }
134
    return \@result;
135
}
136
111
1;
137
1;
(-)a/t/db_dependent/Koha/Z3950Responder/GenericSession.t (-6 / +31 lines)
Lines 23-29 our $child; Link Here
23
23
24
subtest 'test_search' => sub {
24
subtest 'test_search' => sub {
25
25
26
    plan tests => 20;
26
    plan tests => 27;
27
27
28
    t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch');
28
    t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch');
29
29
Lines 66-83 subtest 'test_search' => sub { Link Here
66
66
67
    my $builder = Test::MockModule->new('Koha::SearchEngine::Elasticsearch::QueryBuilder');
67
    my $builder = Test::MockModule->new('Koha::SearchEngine::Elasticsearch::QueryBuilder');
68
    $builder->mock('build_query_compat', sub {
68
    $builder->mock('build_query_compat', sub {
69
        my ( $self, $operators, $operands ) = @_;
69
        my ( $self, $operators, $operands, undef, undef, $sort_by ) = @_;
70
70
71
        return (undef, $operands->[0]);
71
        my $query = $operands->[0];
72
        if (scalar(@{$sort_by})) {
73
            $query .= ' sort ' . join(',', @{$sort_by});
74
        }
75
        return (undef, $query);
72
    });
76
    });
73
77
74
    my $search = Test::MockModule->new('Koha::SearchEngine::Elasticsearch::Search');
78
    my $search = Test::MockModule->new('Koha::SearchEngine::Elasticsearch::Search');
75
    $search->mock('simple_search_compat', sub {
79
    $search->mock('simple_search_compat', sub {
76
        my ( $self, $query ) = @_;
80
        my ( $self, $query ) = @_;
77
81
78
        return ('unexpected query', undef, 0) unless $query eq '((author:(author)) AND ((title:(title\(s\))) OR (title:(speciäl))))' || $query eq "(simple search)";
82
        return ('unexpected query', undef, 0) unless $query eq '((author:(author)) AND ((title:(title\(s\))) OR (title:(speciäl))))'
83
            || $query eq '((author:(author)) AND ((title:(title\(s\))) OR (title:(speciäl)))) sort title_desc'
84
            || $query eq "(simple search)";
79
85
80
        my @records = ($marc_record_1, $marc_record_2);
86
        my @records;
87
        if ($query =~ / sort title_desc/) {
88
            @records = ($marc_record_2, $marc_record_1);
89
        } else {
90
            @records = ($marc_record_1, $marc_record_2);
91
        }
81
        return (undef, \@records, 2);
92
        return (undef, \@records, 2);
82
    });
93
    });
83
94
Lines 151-156 subtest 'test_search' => sub { Link Here
151
    ok($returned2, 'Record 2 returned as MARCXML');
162
    ok($returned2, 'Record 2 returned as MARCXML');
152
    is($returned2->as_xml, $marc_record_2->as_xml, 'Record 2 returned properly');
163
    is($returned2->as_xml, $marc_record_2->as_xml, 'Record 2 returned properly');
153
164
165
    $agent->get_ok("$base/biblios?operation=searchRetrieve&recordSchema=marcxml&version=1.1&maximumRecords=10&query=(dc.author%3dauthor AND (dc.title%3d\"title(s)\" OR dc.title%3dspeciäl)) sortby title%2Fdescending", 'Retrieve search results');
166
    $dom = XML::LibXML->load_xml(string => $agent->content());
167
    @nodes = $dom->getElementsByTagNameNS($ns, 'searchRetrieveResponse');
168
    is(scalar(@nodes), 1, 'sorted searchRetrieveResponse returned');
169
    @records = $nodes[0]->getElementsByTagNameNS($marc_ns, 'record');
170
    is(scalar(@records), 2, 'Two results returned');
171
172
    $returned1 = MARC::Record->new_from_xml($records[0]->toString(), 'UTF-8');
173
    ok($returned1, 'First record returned as MARCXML');
174
    is($returned1->as_xml, $marc_record_2->as_xml, 'Record 2 returned first');
175
176
    $returned2= MARC::Record->new_from_xml($records[1]->toString(), 'UTF-8');
177
    ok($returned2, 'Second record returned as MARCXML');
178
    is($returned2->as_xml, $marc_record_1->as_xml, 'Record 1 returned second');
179
154
    cleanup();
180
    cleanup();
155
};
181
};
156
182
157
- 

Return to bug 23887