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);
27 # For now just marc, but we can do anything here really
28 use Catmandu::Importer::MARC;
29 use Catmandu::Store::ElasticSearch;
34 Koha::SearchEngine::Elasticsearch::Indexer->mk_accessors(qw( store ));
38 Koha::SearchEngine::Elasticsearch::Indexer - handles adding new records to the index
42 my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new(
43 { index => Koha::SearchEngine::BIBLIOS_INDEX } );
44 $indexer->drop_index();
45 $indexer->update_index(\@biblionumbers, \@records);
52 =item C<Koha::SearchEngine::Elasticsearch::Indexer::INDEX_STATUS_OK>
54 Represents an index state where index is created and in a working state.
56 =item C<Koha::SearchEngine::Elasticsearch::Indexer::INDEX_STATUS_REINDEX_REQUIRED>
58 Not currently used, but could be useful later, for example if can detect when new field or mapping added.
60 =item C<Koha::SearchEngine::Elasticsearch::Indexer::INDEX_STATUS_RECREATE_REQUIRED>
62 Representings an index state where index needs to be recreated and is not in a working state.
70 INDEX_STATUS_REINDEX_REQUIRED => 1,
71 INDEX_STATUS_RECREATE_REQUIRED => 2,
76 =head2 update_index($biblionums, $records)
79 $self->update_index($biblionums, $records);
81 die("Something went wrong trying to update index:" . $_[0]);
84 Converts C<MARC::Records> C<$records> to Elasticsearch documents and performs
85 an update request for these records on the Elasticsearch index.
91 Arrayref of biblio numbers for the C<$records>, the order must be the same as
92 and match up with C<$records>.
96 Arrayref of C<MARC::Record>s.
103 my ($self, $biblionums, $records) = @_;
104 my $conf = $self->get_elasticsearch_params();
105 my $elasticsearch = $self->get_elasticsearch();
106 my $documents = $self->marc_records_to_documents($records);
109 for (my $i=0; $i < scalar @$biblionums; $i++) {
110 my $id = $biblionums->[$i];
111 my $document = $documents->[$i];
117 push @body, $document;
121 $response = $elasticsearch->bulk(
122 index => $conf->{index_name},
123 type => 'data', # is just hard coded in Indexer.pm?
130 =head2 set_index_status_ok
132 Convenience method for setting index status to C<INDEX_STATUS_OK>.
136 sub set_index_status_ok {
138 $self->index_status(INDEX_STATUS_OK);
141 =head2 is_index_status_ok
143 Convenience method for checking if index status is C<INDEX_STATUS_OK>.
147 sub is_index_status_ok {
149 return $self->index_status == INDEX_STATUS_OK;
152 =head2 set_index_status_reindex_required
154 Convenience method for setting index status to C<INDEX_REINDEX_REQUIRED>.
158 sub set_index_status_reindex_required {
160 $self->index_status(INDEX_STATUS_REINDEX_REQUIRED);
163 =head2 is_index_status_reindex_required
165 Convenience method for checking if index status is C<INDEX_STATUS_REINDEX_REQUIRED>.
169 sub is_index_status_reindex_required {
171 return $self->index_status == INDEX_STATUS_REINDEX_REQUIRED;
174 =head2 set_index_status_recreate_required
176 Convenience method for setting index status to C<INDEX_STATUS_RECREATE_REQUIRED>.
180 sub set_index_status_recreate_required {
182 $self->index_status(INDEX_STATUS_RECREATE_REQUIRED);
185 =head2 is_index_status_recreate_required
187 Convenience method for checking if index status is C<INDEX_STATUS_RECREATE_REQUIRED>.
191 sub is_index_status_recreate_required {
193 return $self->index_status == INDEX_STATUS_RECREATE_REQUIRED;
196 =head2 index_status($status)
198 Will either set the current index status to C<$status> and return C<$status>,
199 or return the current index status if called with no arguments.
205 Optional argument. If passed will set current index status to C<$status> if C<$status> is
206 a valid status. See L</CONSTANTS>.
213 my ($self, $status) = @_;
214 my $key = 'ElasticsearchIndexStatus_' . $self->index;
216 if (defined $status) {
217 unless (any { $status == $_ } (
219 INDEX_STATUS_REINDEX_REQUIRED,
220 INDEX_STATUS_RECREATE_REQUIRED,
223 Koha::Exceptions::Exception->throw("Invalid index status: $status");
225 C4::Context->set_preference($key, $status);
229 return C4::Context->preference($key);
233 =head2 update_mappings
235 Generate Elasticsearch mappings from mappings stored in database and
236 perform a request to update Elasticsearch index mappings. Will throw an
237 error and set index status to C<INDEX_STATUS_RECREATE_REQUIRED> if update
242 sub update_mappings {
244 my $conf = $self->get_elasticsearch_params();
245 my $elasticsearch = $self->get_elasticsearch();
246 my $mappings = $self->get_elasticsearch_mappings();
248 foreach my $type (keys %{$mappings}) {
250 my $response = $elasticsearch->indices->put_mapping(
251 index => $conf->{index_name},
254 $type => $mappings->{$type}
258 $self->set_index_status_recreate_required();
259 my $reason = $_[0]->{vars}->{body}->{error}->{reason};
260 Koha::Exceptions::Exception->throw(
261 error => "Unable to update mappings for index \"$conf->{index_name}\". Reason was: \"$reason\". Index needs to be recreated and reindexed",
265 $self->set_index_status_ok();
268 =head2 update_index_background($biblionums, $records)
270 This has exactly the same API as C<update_index> however it'll
271 return immediately. It'll start a background process that does the adding.
273 If it fails to add to Elasticsearch then it'll add to a queue that will cause
274 it to be updated by a regular index cron job in the future.
278 # TODO implement in the future - I don't know the best way of doing this yet.
279 # If fork: make sure process group is changed so apache doesn't wait for us.
281 sub update_index_background {
283 $self->update_index(@_);
286 =head2 delete_index($biblionums)
288 C<$biblionums> is an arrayref of biblionumbers to delete from the index.
293 my ($self, $biblionums) = @_;
295 if ( !$self->store ) {
296 my $params = $self->get_elasticsearch_params();
298 Catmandu::Store::ElasticSearch->new(
300 index_settings => $self->get_elasticsearch_settings(),
301 index_mappings => $self->get_elasticsearch_mappings(),
305 $self->store->bag->delete("$_") foreach @$biblionums;
306 $self->store->bag->commit;
309 =head2 delete_index_background($biblionums)
311 Identical to L</delete_index($biblionums)>
315 # TODO: Should be made async
316 sub delete_index_background {
318 $self->delete_index(@_);
323 Drops the index from the Elasticsearch server.
329 if ($self->index_exists) {
330 my $conf = $self->get_elasticsearch_params();
331 my $elasticsearch = $self->get_elasticsearch();
332 $elasticsearch->indices->delete(index => $conf->{index_name});
333 $self->set_index_status_recreate_required();
339 Creates the index (including mappings) on the Elasticsearch server.
345 my $conf = $self->get_elasticsearch_params();
346 my $settings = $self->get_elasticsearch_settings();
347 my $elasticsearch = $self->get_elasticsearch();
348 $elasticsearch->indices->create(
349 index => $conf->{index_name},
351 settings => $settings
354 $self->update_mappings();
359 Checks if index has been created on the Elasticsearch server. Returns C<1> or the
360 empty string to indicate whether index exists or not.
366 my $conf = $self->get_elasticsearch_params();
367 my $elasticsearch = $self->get_elasticsearch();
368 return $elasticsearch->indices->exists(
369 index => $conf->{index_name},
381 =item Chris Cormack C<< <chrisc@catalyst.net.nz> >>
383 =item Robin Sheat C<< <robin@catalyst.net.nz> >>