1 package Koha::SearchEngine::Elasticsearch::Search;
3 # Copyright 2014 Catalyst IT
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 Koha::SearchEngine::ElasticSearch::Search - search functions for Elasticsearch
26 my $searcher = Koha::SearchEngine::ElasticSearch::Search->new();
27 my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new();
28 my $query = $builder->build_query('perl');
29 my $results = $searcher->search($query);
30 print "There were " . $results->total . " results.\n";
39 use base qw(Koha::ElasticSearch);
42 use Catmandu::Store::ElasticSearch;
44 use Data::Dumper; #TODO remove
47 Koha::SearchEngine::Elasticsearch::Search->mk_accessors(qw( store ));
51 my $results = $searcher->search($query, $page, $count, %options);
53 Run a search using the query. It'll return C<$count> results, starting at page
54 C<$page> (C<$page> counts from 1, anything less that, or C<undef> becomes 1.)
55 C<$count> is also the number of entries on a page.
57 C<%options> is a hash containing extra options:
63 If provided, this overrides the C<$page> value, and specifies the record as
64 an offset (i.e. the number of the record to start with), rather than a page.
73 my ($self, $query, $page, $count, %options) = @_;
75 my $params = $self->get_elasticsearch_params();
77 # 20 is the default number of results per page
78 $paging{limit} = $count || 20;
79 # ES/Catmandu doesn't want pages, it wants a record to start from.
80 if (exists $options{offset}) {
81 $paging{start} = $options{offset};
83 $page = (!defined($page) || ($page <= 0)) ? 1 : $page - 1;
84 $paging{start} = $page * $paging{limit};
87 Catmandu::Store::ElasticSearch->new(
92 my $results = $self->store->bag->search( %$query, %paging );
98 my ( $error, $results, $facets ) = $search->search_compat(
99 $query, $simple_query, \@sort_by, \@servers,
100 $results_per_page, $offset, $expanded_facet, $branches,
104 A search interface somewhat compatible with L<C4::Search->getRecords>. Anything
105 that is returned in the query created by build_query_compat will probably
112 $self, $query, $simple_query, $sort_by,
113 $servers, $results_per_page, $offset, $expanded_facet,
114 $branches, $query_type, $scan
118 $options{offset} = $offset;
119 my $results = $self->search($query, undef, $results_per_page, %options);
121 # Convert each result into a MARC::Record
122 my (@records, $index);
123 $index = $offset; # opac-search expects results to be put in the
124 # right place in the array, according to $offset
126 # The results come in an array for some reason
127 my $marc_json = @_[0]->{record};
128 my $marc = $self->json2marc($marc_json);
129 $records[$index++] = $marc;
131 # consumers of this expect a name-spaced result, we provide the default
134 $result{biblioserver}{hits} = $results->total;
135 $result{biblioserver}{RECORDS} = \@records;
136 return (undef, \%result, $self->_convert_facets($results->{facets}));
141 my ( $results, $total ) =
142 $searcher->search_marc( $query, $page, $count, %options );
144 This has a similar calling convention to L<search>, however it assumes that all
145 the results are going to contain MARC, and just provides an arrayref of them,
146 along with a count of the total number of results.
151 # TODO this probably should be temporary, until something more
152 # comprehensive is implemented using Koha::RecordProcessor and such.
155 my $res = $self->search(@_);
158 my $marc_json = @_[0]->{record};
159 my $marc = $self->json2marc($marc_json);
160 push @records, $marc;
162 return (\@records, $res->total);
167 my $marc = $self->json2marc($marc_json);
169 Converts the form of marc (based on its JSON, but as a Perl structure) that
170 Catmandu stores into a MARC::Record object.
175 my ( $self, $marcjson ) = @_;
177 my $marc = MARC::Record->new();
178 $marc->encoding('UTF-8');
181 # [ '245', '1', '2', 'a' => 'Title', 'b' => 'Subtitle' ]
182 # conveniently, this is the form that MARC::Field->new() likes
183 foreach $field (@$marcjson) {
184 next if @$field < 5; # Shouldn't be possible, but...
185 if ( $field->[0] eq 'LDR' ) {
186 $marc->leader( $field->[4] );
189 my $marc_field = MARC::Field->new(@$field);
190 $marc->append_fields($marc_field);
196 =head2 _convert_facets
198 my $koha_facets = _convert_facets($es_facets);
200 Converts elasticsearch facets types to the form that Koha expects.
201 It expects the ES facet name to match the Koha type, for example C<itype>,
202 C<au>, C<su-to>, etc.
206 sub _convert_facets {
207 my ( $self, $es ) = @_;
209 return undef if !$es;
211 # These should correspond to the ES field names, as opposed to the CCL
212 # things that zebra uses.
213 my %type_to_label = (
215 location => 'Location',
216 itype => 'ItemTypes',
219 'su-geo' => 'Places',
222 # We also have some special cases, e.g. itypes that need to show the
223 # value rather than the code.
224 my $itypes = Koha::ItemTypes->new();
225 my %special = ( itype => sub { $itypes->get_description_for_code(@_) }, );
227 while ( ( $type, $data ) = each %$es ) {
228 next if !exists( $type_to_label{$type} );
230 type_id => $type . '_id',
232 expandable => 1, # TODO figure how that's supposed to work
233 "type_label_$type_to_label{$type}" => 1,
234 type_link_value => $type,
236 foreach my $term ( @{ $data->{terms} } ) {
237 my $t = $term->{term};
238 my $c = $term->{count};
239 if ( exists( $special{$type} ) ) {
240 $label = $special{$type}->($t);
245 push @{ $facet->{facets} }, {
247 facet_link_value => $t,
248 facet_title_value => $t . " ($c)",
249 facet_label_value => $label, # TODO either truncate this,
250 # or make the template do it like it should anyway
251 type_link_value => $type,
254 push @res, $facet if exists $facet->{facets};