|
Lines 83-105
sub search {
Link Here
|
| 83 |
my ($self, $query, $page, $count, %options) = @_; |
83 |
my ($self, $query, $page, $count, %options) = @_; |
| 84 |
|
84 |
|
| 85 |
my $params = $self->get_elasticsearch_params(); |
85 |
my $params = $self->get_elasticsearch_params(); |
| 86 |
my %paging; |
|
|
| 87 |
# 20 is the default number of results per page |
86 |
# 20 is the default number of results per page |
| 88 |
$paging{limit} = $count || 20; |
87 |
$query->{size} = $count || 20; |
| 89 |
# ES/Catmandu doesn't want pages, it wants a record to start from. |
88 |
# ES doesn't want pages, it wants a record to start from. |
| 90 |
if (exists $options{offset}) { |
89 |
if (exists $options{offset}) { |
| 91 |
$paging{start} = $options{offset}; |
90 |
$query->{from} = $options{offset}; |
| 92 |
} else { |
91 |
} else { |
| 93 |
$page = (!defined($page) || ($page <= 0)) ? 0 : $page - 1; |
92 |
$page = (!defined($page) || ($page <= 0)) ? 0 : $page - 1; |
| 94 |
$paging{start} = $page * $paging{limit}; |
93 |
$query->{from} = $page * $query->{size}; |
| 95 |
} |
94 |
} |
| 96 |
$self->store( |
95 |
my $elasticsearch = $self->get_elasticsearch(); |
| 97 |
Catmandu::Store::ElasticSearch->new( |
|
|
| 98 |
%$params, |
| 99 |
) |
| 100 |
) unless $self->store; |
| 101 |
my $results = eval { |
96 |
my $results = eval { |
| 102 |
$self->store->bag->search( %$query, %paging ); |
97 |
$elasticsearch->search( |
|
|
98 |
index => $params->{index_name}, |
| 99 |
body => $query |
| 100 |
); |
| 103 |
}; |
101 |
}; |
| 104 |
if ($@) { |
102 |
if ($@) { |
| 105 |
die $self->process_error($@); |
103 |
die $self->process_error($@); |
|
Lines 162-174
sub search_compat {
Link Here
|
| 162 |
# opac-search expects results to be put in the |
160 |
# opac-search expects results to be put in the |
| 163 |
# right place in the array, according to $offset |
161 |
# right place in the array, according to $offset |
| 164 |
my $index = $offset; |
162 |
my $index = $offset; |
| 165 |
$results->each(sub { |
163 |
my $hits = $results->{'hits'}; |
| 166 |
$records[$index++] = $self->decode_record_from_result(@_); |
164 |
foreach my $es_record (@{$hits->{'hits'}}) { |
| 167 |
}); |
165 |
$records[$index++] = $self->decode_record_from_result($es_record->{'_source'}); |
|
|
166 |
} |
| 167 |
|
| 168 |
# consumers of this expect a name-spaced result, we provide the default |
168 |
# consumers of this expect a name-spaced result, we provide the default |
| 169 |
# configuration. |
169 |
# configuration. |
| 170 |
my %result; |
170 |
my %result; |
| 171 |
$result{biblioserver}{hits} = $results->total; |
171 |
$result{biblioserver}{hits} = $hits->{'total'}; |
| 172 |
$result{biblioserver}{RECORDS} = \@records; |
172 |
$result{biblioserver}{RECORDS} = \@records; |
| 173 |
return (undef, \%result, $self->_convert_facets($results->{aggregations}, $expanded_facet)); |
173 |
return (undef, \%result, $self->_convert_facets($results->{aggregations}, $expanded_facet)); |
| 174 |
} |
174 |
} |
|
Lines 176-182
sub search_compat {
Link Here
|
| 176 |
=head2 search_auth_compat |
176 |
=head2 search_auth_compat |
| 177 |
|
177 |
|
| 178 |
my ( $results, $total ) = |
178 |
my ( $results, $total ) = |
| 179 |
$searcher->search_auth_compat( $query, $page, $count, %options ); |
179 |
$searcher->search_auth_compat( $query, $offset, $count, $skipmetadata, %options ); |
| 180 |
|
180 |
|
| 181 |
This has a similar calling convention to L<search>, however it returns its |
181 |
This has a similar calling convention to L<search>, however it returns its |
| 182 |
results in a form the same as L<C4::AuthoritiesMarc::SearchAuthorities>. |
182 |
results in a form the same as L<C4::AuthoritiesMarc::SearchAuthorities>. |
|
Lines 184-215
results in a form the same as L<C4::AuthoritiesMarc::SearchAuthorities>.
Link Here
|
| 184 |
=cut |
184 |
=cut |
| 185 |
|
185 |
|
| 186 |
sub search_auth_compat { |
186 |
sub search_auth_compat { |
| 187 |
my $self = shift; |
187 |
my ($self, $query, $offset, $count, $skipmetadata, %options) = @_; |
| 188 |
|
188 |
|
| 189 |
# TODO handle paging |
189 |
if ( !defined $offset or $offset <= 0 ) { |
|
|
190 |
$offset = 1; |
| 191 |
} |
| 192 |
# Uh, authority search uses 1-based offset.. |
| 193 |
$options{offset} = $offset - 1; |
| 190 |
my $database = Koha::Database->new(); |
194 |
my $database = Koha::Database->new(); |
| 191 |
my $schema = $database->schema(); |
195 |
my $schema = $database->schema(); |
| 192 |
my $res = $self->search(@_); |
196 |
my $res = $self->search($query, undef, $count, %options); |
|
|
197 |
|
| 193 |
my $bib_searcher = Koha::SearchEngine::Elasticsearch::Search->new({index => 'biblios'}); |
198 |
my $bib_searcher = Koha::SearchEngine::Elasticsearch::Search->new({index => 'biblios'}); |
| 194 |
my @records; |
199 |
my @records; |
| 195 |
$res->each( |
200 |
my $hits = $res->{'hits'}; |
| 196 |
sub { |
201 |
foreach my $es_record ($hits->{'hits'}) { |
| 197 |
my %result; |
202 |
my $record = $es_record->[0]->{'_source'}; |
|
|
203 |
my %result; |
| 198 |
|
204 |
|
| 199 |
# I wonder if these should be real values defined in the mapping |
205 |
# I wonder if these should be real values defined in the mapping |
| 200 |
# rather than hard-coded conversions. |
206 |
# rather than hard-coded conversions. |
| 201 |
my $record = $_[0]; |
207 |
#my $record = $_[0]; |
| 202 |
# Handle legacy nested arrays indexed with splitting enabled. |
208 |
# Handle legacy nested arrays indexed with splitting enabled. |
| 203 |
my $authid = $record->{ 'Local-number' }[0]; |
209 |
my $authid = $record->{ 'Local-number' }[0]; |
| 204 |
$authid = @$authid[0] if (ref $authid eq 'ARRAY'); |
210 |
$authid = @$authid[0] if (ref $authid eq 'ARRAY'); |
| 205 |
|
211 |
|
| 206 |
$result{authid} = $authid; |
212 |
$result{authid} = $authid; |
| 207 |
|
213 |
|
|
|
214 |
if (!defined $skipmetadata || !$skipmetadata) { |
| 208 |
# TODO put all this info into the record at index time so we |
215 |
# TODO put all this info into the record at index time so we |
| 209 |
# don't have to go and sort it all out now. |
216 |
# don't have to go and sort it all out now. |
| 210 |
my $authtypecode = $record->{authtype}; |
217 |
my $authtypecode = $record->{authtype}; |
| 211 |
my $rs = $schema->resultset('AuthType') |
218 |
my $rs = $schema->resultset('AuthType') |
| 212 |
->search( { authtypecode => $authtypecode } ); |
219 |
->search( { authtypecode => $authtypecode } ); |
| 213 |
|
220 |
|
| 214 |
# FIXME there's an assumption here that we will get a result. |
221 |
# FIXME there's an assumption here that we will get a result. |
| 215 |
# the original code also makes an assumption that some provided |
222 |
# the original code also makes an assumption that some provided |
|
Lines 218-224
sub search_auth_compat {
Link Here
|
| 218 |
# it's not reproduced here yet. |
225 |
# it's not reproduced here yet. |
| 219 |
my $authtype = $rs->single; |
226 |
my $authtype = $rs->single; |
| 220 |
my $auth_tag_to_report = $authtype ? $authtype->auth_tag_to_report : ""; |
227 |
my $auth_tag_to_report = $authtype ? $authtype->auth_tag_to_report : ""; |
| 221 |
my $marc = $self->decode_record_from_result(@_); |
228 |
my $marc = $self->decode_record_from_result($record); |
| 222 |
my $mainentry = $marc->field($auth_tag_to_report); |
229 |
my $mainentry = $marc->field($auth_tag_to_report); |
| 223 |
my $reported_tag; |
230 |
my $reported_tag; |
| 224 |
if ($mainentry) { |
231 |
if ($mainentry) { |
|
Lines 232-244
sub search_auth_compat {
Link Here
|
| 232 |
|
239 |
|
| 233 |
# Reimplementing BuildSummary is out of scope because it'll be hard |
240 |
# Reimplementing BuildSummary is out of scope because it'll be hard |
| 234 |
$result{summary} = |
241 |
$result{summary} = |
| 235 |
C4::AuthoritiesMarc::BuildSummary( $marc, $result{authid}, |
242 |
C4::AuthoritiesMarc::BuildSummary( $marc, $result{authid}, |
| 236 |
$authtypecode ); |
243 |
$authtypecode ); |
| 237 |
$result{used} = $self->count_auth_use($bib_searcher, $authid); |
244 |
$result{used} = $self->count_auth_use($bib_searcher, $authid); |
| 238 |
push @records, \%result; |
|
|
| 239 |
} |
245 |
} |
| 240 |
); |
246 |
push @records, \%result; |
| 241 |
return ( \@records, $res->total ); |
247 |
} |
|
|
248 |
return ( \@records, $hits->{'total'} ); |
| 242 |
} |
249 |
} |
| 243 |
|
250 |
|
| 244 |
=head2 count_auth_use |
251 |
=head2 count_auth_use |