1 package Koha::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 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 use base qw(Koha::ElasticSearch);
25 # For now just marc, but we can do anything here really
26 use Catmandu::Importer::MARC;
27 use Catmandu::Store::ElasticSearch;
29 Koha::ElasticSearch::Indexer->mk_accessors(qw( store ));
33 Koha::ElasticSearch::Indexer - handles adding new records to the index
37 my $indexer = Koha::ElasticSearch::Indexer->new({ index => 'biblios' });
38 $indexer->delete_index();
39 $indexer->update_index(\@biblionumbers, \@records);
45 =head2 $indexer->update_index($biblionums, $records);
47 C<$biblionums> is an arrayref containing the biblionumbers for the records.
49 C<$records> is an arrayref containing the L<MARC::Record>s themselves.
51 The values in the arrays must match up, and the 999$c value in the MARC record
52 will be rewritten using the values in C<$biblionums> to ensure they are correct.
53 If C<$biblionums> is C<undef>, this won't happen, but you should be sure that
54 999$c is correct on your own then.
56 Note that this will modify the original record if C<$biblionums> is supplied.
57 If that's a problem, clone them first.
62 my ($self, $biblionums, $records) = @_;
65 $self->_sanitise_records($biblionums, $records);
68 my $from = $self->_convert_marc_to_json($records);
69 if ( !$self->store ) {
70 my $params = $self->get_elasticsearch_params();
72 Catmandu::Store::ElasticSearch->new(
74 index_settings => $self->get_elasticsearch_settings(),
75 index_mappings => $self->get_elasticsearch_mappings(),
80 $self->store->bag->add_many($from);
81 $self->store->bag->commit;
85 =head2 $indexer->update_index_background($biblionums, $records)
87 This has exactly the same API as C<update_index_background> however it'll
88 return immediately. It'll start a background process that does the adding.
90 If it fails to add to Elasticsearch then it'll add to a queue that will cause
91 it to be updated by a regular index cron job in the future.
93 # TODO implement in the future - I don't know the best way of doing this yet.
94 # If fork: make sure process group is changed so apache doesn't wait for us.
98 sub update_index_background {
100 $self->update_index(@_);
103 =head2 $indexer->delete_index();
105 Deletes the index from the elasticsearch server. Calling C<update_index>
106 after this will recreate it again.
114 # If this index doesn't exist, this will create it. Then it'll be
115 # deleted. That's not the end of the world however.
116 my $params = $self->get_elasticsearch_params();
118 Catmandu::Store::ElasticSearch->new(
120 index_settings => $self->get_elasticsearch_settings(),
121 index_mappings => $self->get_elasticsearch_mappings(),
126 $self->store->drop();
130 sub _sanitise_records {
131 my ($self, $biblionums, $records) = @_;
133 confess "Unequal number of values in \$biblionums and \$records." if (@$biblionums != @$records);
135 my $c = @$biblionums;
136 for (my $i=0; $i<$c; $i++) {
137 my $bibnum = $biblionums->[$i];
138 my $rec = $records->[$i];
139 # I've seen things you people wouldn't believe. Attack ships on fire
140 # off the shoulder of Orion. I watched C-beams glitter in the dark near
141 # the Tannhauser gate. MARC records where 999$c doesn't match the
142 # biblionumber column. All those moments will be lost in time... like
144 $rec->delete_fields($rec->field('999'));
145 $rec->append_fields(MARC::Field->new('999','','','c' => $bibnum, 'd' => $bibnum));
149 sub _convert_marc_to_json {
153 Catmandu::Importer::MARC->new( records => $records, id => '999c' );
154 my $fixer = Catmandu::Fix->new( fixes => $self->get_fixer_rules() );
155 $importer = $fixer->fix($importer);
167 =item Chris Cormack C<< <chrisc@catalyst.net.nz> >>
169 =item Robin Sheat C<< <robin@catalyst.net.nz> >>