1 package Koha::SearchEngine::Elasticsearch::Indexer;
3 # Copyright 2013 Catalyst IT
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 use List::Util qw(any);
24 use base qw(Koha::SearchEngine::Elasticsearch);
28 use Koha::Exceptions::Elasticsearch;
29 use Koha::SearchEngine::Zebra::Indexer;
30 use C4::AuthoritiesMarc qw//;
36 Koha::SearchEngine::Elasticsearch::Indexer - handles adding new records to the index
40 my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new(
41 { index => Koha::SearchEngine::BIBLIOS_INDEX } );
42 $indexer->drop_index();
43 $indexer->update_index(\@biblionumbers, \@records);
50 =item C<Koha::SearchEngine::Elasticsearch::Indexer::INDEX_STATUS_OK>
52 Represents an index state where index is created and in a working state.
54 =item C<Koha::SearchEngine::Elasticsearch::Indexer::INDEX_STATUS_REINDEX_REQUIRED>
56 Not currently used, but could be useful later, for example if can detect when new field or mapping added.
58 =item C<Koha::SearchEngine::Elasticsearch::Indexer::INDEX_STATUS_RECREATE_REQUIRED>
60 Representings an index state where index needs to be recreated and is not in a working state.
68 INDEX_STATUS_REINDEX_REQUIRED => 1,
69 INDEX_STATUS_RECREATE_REQUIRED => 2,
74 =head2 update_index($biblionums, $records)
77 $self->update_index($biblionums, $records);
79 die("Something went wrong trying to update index:" . $_[0]);
82 Converts C<MARC::Records> C<$records> to Elasticsearch documents and performs
83 an update request for these records on the Elasticsearch index.
89 Arrayref of biblio numbers for the C<$records>, the order must be the same as
90 and match up with C<$records>.
94 Arrayref of C<MARC::Record>s.
101 my ($self, $biblionums, $records) = @_;
103 my $documents = $self->marc_records_to_documents($records);
105 for (my $i = 0; $i < scalar @$biblionums; $i++) {
106 my $id = $biblionums->[$i];
107 my $document = $documents->[$i];
113 push @body, $document;
118 my $elasticsearch = $self->get_elasticsearch();
119 $response = $elasticsearch->bulk(
120 index => $self->index_name,
121 type => 'data', # is just hard coded in Indexer.pm?
124 if ($response->{errors}) {
125 carp "One or more ElasticSearch errors occurred when indexing documents";
128 Koha::Exceptions::Elasticsearch::BadResponse->throw(
130 details => $_->{text},
137 =head2 set_index_status_ok
139 Convenience method for setting index status to C<INDEX_STATUS_OK>.
143 sub set_index_status_ok {
145 $self->index_status(INDEX_STATUS_OK);
148 =head2 is_index_status_ok
150 Convenience method for checking if index status is C<INDEX_STATUS_OK>.
154 sub is_index_status_ok {
156 return $self->index_status == INDEX_STATUS_OK;
159 =head2 set_index_status_reindex_required
161 Convenience method for setting index status to C<INDEX_REINDEX_REQUIRED>.
165 sub set_index_status_reindex_required {
167 $self->index_status(INDEX_STATUS_REINDEX_REQUIRED);
170 =head2 is_index_status_reindex_required
172 Convenience method for checking if index status is C<INDEX_STATUS_REINDEX_REQUIRED>.
176 sub is_index_status_reindex_required {
178 return $self->index_status == INDEX_STATUS_REINDEX_REQUIRED;
181 =head2 set_index_status_recreate_required
183 Convenience method for setting index status to C<INDEX_STATUS_RECREATE_REQUIRED>.
187 sub set_index_status_recreate_required {
189 $self->index_status(INDEX_STATUS_RECREATE_REQUIRED);
192 =head2 is_index_status_recreate_required
194 Convenience method for checking if index status is C<INDEX_STATUS_RECREATE_REQUIRED>.
198 sub is_index_status_recreate_required {
200 return $self->index_status == INDEX_STATUS_RECREATE_REQUIRED;
203 =head2 index_status($status)
205 Will either set the current index status to C<$status> and return C<$status>,
206 or return the current index status if called with no arguments.
212 Optional argument. If passed will set current index status to C<$status> if C<$status> is
213 a valid status. See L</CONSTANTS>.
220 my ($self, $status) = @_;
221 my $key = 'ElasticsearchIndexStatus_' . $self->index;
223 if (defined $status) {
224 unless (any { $status == $_ } (
226 INDEX_STATUS_REINDEX_REQUIRED,
227 INDEX_STATUS_RECREATE_REQUIRED,
230 Koha::Exceptions::Exception->throw("Invalid index status: $status");
232 C4::Context->set_preference($key, $status);
236 return C4::Context->preference($key);
240 =head2 update_mappings
242 Generate Elasticsearch mappings from mappings stored in database and
243 perform a request to update Elasticsearch index mappings. Will throw an
244 error and set index status to C<INDEX_STATUS_RECREATE_REQUIRED> if update
249 sub update_mappings {
251 my $elasticsearch = $self->get_elasticsearch();
252 my $mappings = $self->get_elasticsearch_mappings();
254 foreach my $type (keys %{$mappings}) {
256 my $response = $elasticsearch->indices->put_mapping(
257 index => $self->index_name,
260 $type => $mappings->{$type}
264 $self->set_index_status_recreate_required();
265 my $reason = $_[0]->{vars}->{body}->{error}->{reason};
266 my $index_name = $self->index_name;
267 Koha::Exceptions::Exception->throw(
268 error => "Unable to update mappings for index \"$index_name\". Reason was: \"$reason\". Index needs to be recreated and reindexed",
272 $self->set_index_status_ok();
275 =head2 update_index_background($biblionums, $records)
277 This has exactly the same API as C<update_index> however it'll
278 return immediately. It'll start a background process that does the adding.
280 If it fails to add to Elasticsearch then it'll add to a queue that will cause
281 it to be updated by a regular index cron job in the future.
285 # TODO implement in the future - I don't know the best way of doing this yet.
286 # If fork: make sure process group is changed so apache doesn't wait for us.
288 sub update_index_background {
290 $self->update_index(@_);
295 This function takes an array of record numbers and fetches the records to send to update_index
298 If $records parameter is provided the records will be used as-is, this is only utilized for authorities
301 The other variables are used for parity with Zebra indexing calls. Currently the calls are passed through
307 my ( $self, $record_numbers, $op, $server, $records ) = @_;
308 $record_numbers = [$record_numbers] if ref $record_numbers ne 'ARRAY' && defined $record_numbers;
309 $records = [$records] if ref $records ne 'ARRAY' && defined $records;
310 if ( $op eq 'specialUpdate' ) {
311 my $index_record_numbers;
313 $index_record_numbers = $record_numbers;
315 foreach my $record_number ( @$record_numbers ){
316 my $record = _get_record( $record_number, $server );
318 push @$records, $record;
319 push @$index_record_numbers, $record_number;
323 $self->update_index_background( $index_record_numbers, $records ) if $index_record_numbers && $records;
325 elsif ( $op eq 'recordDelete' ) {
326 $self->delete_index_background( $record_numbers );
328 #FIXME Current behaviour is to index Zebra when using ES, at some point we should stop
329 Koha::SearchEngine::Zebra::Indexer::index_records( $self, $record_numbers, $op, $server, undef );
333 my ( $id, $server ) = @_;
334 return $server eq 'biblioserver'
335 ? C4::Biblio::GetMarcBiblio({ biblionumber => $id, embed_items => 1 })
336 : C4::AuthoritiesMarc::GetAuthority($id);
339 =head2 delete_index($biblionums)
341 C<$biblionums> is an arrayref of biblionumbers to delete from the index.
346 my ($self, $biblionums) = @_;
348 my $elasticsearch = $self->get_elasticsearch();
349 my @body = map { { delete => { _id => "$_" } } } @{$biblionums};
350 my $result = $elasticsearch->bulk(
351 index => $self->index_name,
355 if ($result->{errors}) {
356 croak "An Elasticsearch error occurred during bulk delete";
360 =head2 delete_index_background($biblionums)
362 Identical to L</delete_index($biblionums)>
366 # TODO: Should be made async
367 sub delete_index_background {
369 $self->delete_index(@_);
374 Drops the index from the Elasticsearch server.
380 if ($self->index_exists) {
381 my $elasticsearch = $self->get_elasticsearch();
382 $elasticsearch->indices->delete(index => $self->index_name);
383 $self->set_index_status_recreate_required();
389 Creates the index (including mappings) on the Elasticsearch server.
395 my $settings = $self->get_elasticsearch_settings();
396 my $elasticsearch = $self->get_elasticsearch();
397 $elasticsearch->indices->create(
398 index => $self->index_name,
400 settings => $settings
403 $self->update_mappings();
408 Checks if index has been created on the Elasticsearch server. Returns C<1> or the
409 empty string to indicate whether index exists or not.
415 my $elasticsearch = $self->get_elasticsearch();
416 return $elasticsearch->indices->exists(
417 index => $self->index_name,
429 =item Chris Cormack C<< <chrisc@catalyst.net.nz> >>
431 =item Robin Sheat C<< <robin@catalyst.net.nz> >>