use base qw(Koha::SearchEngine::Elasticsearch);
use C4::Context;
+use C4::AuthoritiesMarc;
use Koha::ItemTypes;
use Koha::AuthorisedValues;
use Koha::SearchEngine::QueryBuilder;
+use Koha::SearchEngine::Search;
+use Koha::Exceptions::Elasticsearch;
use MARC::Record;
use Catmandu::Store::ElasticSearch;
-
+use MARC::File::XML;
use Data::Dumper; #TODO remove
use Carp qw(cluck);
+use MIME::Base64;
Koha::SearchEngine::Elasticsearch::Search->mk_accessors(qw( store ));
$servers, $results_per_page, $offset, $expanded_facet,
$branches, $query_type, $scan
) = @_;
-
my %options;
+ if ( !defined $offset or $offset < 0 ) {
+ $offset = 0;
+ }
$options{offset} = $offset;
$options{expanded_facet} = $expanded_facet;
my $results = $self->search($query, undef, $results_per_page, %options);
# Convert each result into a MARC::Record
- my (@records, $index);
- $index = $offset; # opac-search expects results to be put in the
- # right place in the array, according to $offset
+ my @records;
+ # opac-search expects results to be put in the
+ # right place in the array, according to $offset
+ my $index = $offset;
$results->each(sub {
- # The results come in an array for some reason
- my $marc_json = $_[0]->{record};
- my $marc = $self->json2marc($marc_json);
- $records[$index++] = $marc;
- });
+ $records[$index++] = $self->decode_record_from_result(@_);
+ });
# consumers of this expect a name-spaced result, we provide the default
# configuration.
my %result;
$result{biblioserver}{hits} = $results->total;
$result{biblioserver}{RECORDS} = \@records;
- return (undef, \%result, $self->_convert_facets($results->{facets}, $expanded_facet));
+ return (undef, \%result, $self->_convert_facets($results->{aggregations}, $expanded_facet));
}
=head2 search_auth_compat
$res->each(
sub {
my %result;
- my $record = $_[0];
- my $marc_json = $record->{record};
# I wonder if these should be real values defined in the mapping
# rather than hard-coded conversions.
- # Our results often come through as nested arrays, to fix this
- # requires changes in catmandu.
- my $authid = $record->{ 'Local-number' }[0][0];
+ my $record = $_[0];
+ # Handle legacy nested arrays indexed with splitting enabled.
+ my $authid = $record->{ 'Local-number' }[0];
+ $authid = @$authid[0] if (ref $authid eq 'ARRAY');
+
$result{authid} = $authid;
# TODO put all this info into the record at index time so we
# with the record. It's not documented why this is the case, so
# it's not reproduced here yet.
my $authtype = $rs->single;
- my $auth_tag_to_report = $authtype->auth_tag_to_report;
- my $marc = $self->json2marc($marc_json);
+ my $auth_tag_to_report = $authtype ? $authtype->auth_tag_to_report : "";
+ my $marc = $self->decode_record_from_result(@_);
my $mainentry = $marc->field($auth_tag_to_report);
my $reported_tag;
if ($mainentry) {
}
}
# Turn the resultset into a hash
- my %authtype_cols;
- foreach my $col ($authtype->result_source->columns) {
- $authtype_cols{$col} = $authtype->get_column($col);
- }
- $result{authtype} = $authtype->authtypetext;
+ $result{authtype} = $authtype ? $authtype->authtypetext : $authtypecode;
$result{reported_tag} = $reported_tag;
# Reimplementing BuildSummary is out of scope because it'll be hard
my $query = {
query => {
- filtered => {
- query => { match_all => {} },
+ bool => {
+# query => { match_all => {} },
filter => { term => { an => $authid } }
}
}
=head2 simple_search_compat
my ( $error, $marcresults, $total_hits ) =
- $searcher->simple_search( $query, $offset, $max_results );
+ $searcher->simple_search( $query, $offset, $max_results, %options );
This is a simpler interface to the searching, intended to be similar enough to
L<C4::Search::SimpleSearch>.
The max number of results to return. The default is 100 (because unlimited
is a pretty terrible thing to do.)
+=item C<%options>
+
+These options are unused by Elasticsearch
+
=back
Returns:
return ('No query entered', undef, undef) unless $query;
my %options;
- $options{offset} = $offset // 0;
+ $offset = 0 if not defined $offset or $offset < 0;
+ $options{offset} = $offset;
$max_results //= 100;
unless (ref $query) {
my $results = $self->search($query, undef, $max_results, %options);
my @records;
$results->each(sub {
- # The results come in an array for some reason
- my $marc_json = $_[0]->{record};
- my $marc = $self->json2marc($marc_json);
+ my $marc = $self->decode_record_from_result(@_);
push @records, $marc;
});
return (undef, \@records, $results->total);
}
-=head2 json2marc
+=head2 extract_biblionumber
+
+ my $biblionumber = $searcher->extract_biblionumber( $searchresult );
- my $marc = $self->json2marc($marc_json);
+$searchresult comes from simple_search_compat.
-Converts the form of marc (based on its JSON, but as a Perl structure) that
-Catmandu stores into a MARC::Record object.
+Returns the biblionumber from the search result record.
=cut
-sub json2marc {
- my ( $self, $marcjson ) = @_;
+sub extract_biblionumber {
+ my ( $self, $searchresultrecord ) = @_;
+ return Koha::SearchEngine::Search::extract_biblionumber( $searchresultrecord );
+}
- my $marc = MARC::Record->new();
- $marc->encoding('UTF-8');
+=head2 decode_record_from_result
+ my $marc_record = $self->decode_record_from_result(@result);
- # fields are like:
- # [ '245', '1', '2', 'a' => 'Title', 'b' => 'Subtitle' ]
- # conveniently, this is the form that MARC::Field->new() likes
- foreach my $field (@$marcjson) {
- next if @$field < 5; # Shouldn't be possible, but...
- if ( $field->[0] eq 'LDR' ) {
- $marc->leader( $field->[4] );
- }
- else {
- my $marc_field = MARC::Field->new(@$field);
- $marc->append_fields($marc_field);
- }
+Extracts marc data from Elasticsearch result and decodes to MARC::Record object
+
+=cut
+
+sub decode_record_from_result {
+ # Result is passed in as array, will get flattened
+ # and first element will be $result
+ my ( $self, $result ) = @_;
+ if ($result->{marc_format} eq 'base64ISO2709') {
+ return MARC::Record->new_from_usmarc(decode_base64($result->{marc_data}));
+ }
+ elsif ($result->{marc_format} eq 'MARCXML') {
+ return MARC::Record->new_from_xml($result->{marc_data}, 'UTF-8', uc C4::Context->preference('marcflavour'));
}
- return $marc;
+ else {
+ Koha::Exceptions::Elasticsearch->throw("Missing marc_format field in Elasticsearch result");
+ }
+}
+
+=head2 max_result_window
+
+Returns the maximum number of results that can be fetched
+
+This directly requests Elasticsearch for the setting index.max_result_window (or
+the default value for this setting in case it is not set)
+
+=cut
+
+sub max_result_window {
+ my ($self) = @_;
+
+ $self->store(
+ Catmandu::Store::ElasticSearch->new(%{ $self->get_elasticsearch_params })
+ ) unless $self->store;
+
+ my $index_name = $self->store->index_name;
+ my $settings = $self->store->es->indices->get_settings(
+ index => $index_name,
+ params => { include_defaults => 1, flat_settings => 1 },
+ );
+
+ my $max_result_window = $settings->{$index_name}->{settings}->{'index.max_result_window'};
+ $max_result_window //= $settings->{$index_name}->{defaults}->{'index.max_result_window'};
+
+ return $max_result_window;
}
=head2 _convert_facets
'su-geo' => { order => 4, label => 'Places', },
se => { order => 5, label => 'Series', },
subject => { order => 6, label => 'Topics', },
+ ccode => { order => 7, label => 'CollectionCodes',},
+ holdingbranch => { order => 8, label => 'HoldingLibrary' },
+ homebranch => { order => 9, label => 'HomeLibrary' }
);
# We also have some special cases, e.g. itypes that need to show the
# value rather than the code.
my @itypes = Koha::ItemTypes->search;
+ my @libraries = Koha::Libraries->search;
+ my $library_names = { map { $_->branchcode => $_->branchname } @libraries };
my @locations = Koha::AuthorisedValues->search( { category => 'LOC' } );
my $opac = C4::Context->interface eq 'opac' ;
my %special = (
itype => { map { $_->itemtype => $_->description } @itypes },
location => { map { $_->authorised_value => ( $opac ? ( $_->lib_opac || $_->lib ) : $_->lib ) } @locations },
+ holdingbranch => $library_names,
+ homebranch => $library_names
);
my @facets;
$exp_facet //= '';
type_id => $type . '_id',
expand => $type,
expandable => ( $type ne $exp_facet )
- && ( @{ $data->{terms} } > $limit ),
+ && ( @{ $data->{buckets} } > $limit ),
"type_label_$type_to_label{$type}{label}" => 1,
type_link_value => $type,
order => $type_to_label{$type}{order},
};
- $limit = @{ $data->{terms} } if ( $limit > @{ $data->{terms} } );
- foreach my $term ( @{ $data->{terms} }[ 0 .. $limit - 1 ] ) {
- my $t = $term->{term};
- my $c = $term->{count};
+ $limit = @{ $data->{buckets} } if ( $limit > @{ $data->{buckets} } );
+ foreach my $term ( @{ $data->{buckets} }[ 0 .. $limit - 1 ] ) {
+ my $t = $term->{key};
+ my $c = $term->{doc_count};
my $label;
if ( exists( $special{$type} ) ) {
$label = $special{$type}->{$t} // $t;