]> git.koha-community.org Git - koha.git/blob - Koha/ElasticSearch/Indexer.pm
Bug 12478: ES is now updated when records are updated/deleted
[koha.git] / Koha / ElasticSearch / Indexer.pm
1 package Koha::ElasticSearch::Indexer;
2
3 # Copyright 2013 Catalyst IT
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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.
19
20 use Carp;
21 use Modern::Perl;
22 use base qw(Koha::ElasticSearch);
23 use Data::Dumper;
24
25 # For now just marc, but we can do anything here really
26 use Catmandu::Importer::MARC;
27 use Catmandu::Store::ElasticSearch;
28
29 Koha::ElasticSearch::Indexer->mk_accessors(qw( store ));
30
31 =head1 NAME
32
33 Koha::ElasticSearch::Indexer - handles adding new records to the index
34
35 =head1 SYNOPSIS
36
37     my $indexer = Koha::ElasticSearch::Indexer->new(
38         { index => Koha::SearchEngine::BIBLIOS_INDEX } );
39     $indexer->drop_index();
40     $indexer->update_index(\@biblionumbers, \@records);
41
42 =head1 FUNCTIONS
43
44 =cut
45
46 =head2 $indexer->update_index($biblionums, $records);
47
48 C<$biblionums> is an arrayref containing the biblionumbers for the records.
49
50 C<$records> is an arrayref containing the L<MARC::Record>s themselves.
51
52 The values in the arrays must match up, and the 999$c value in the MARC record
53 will be rewritten using the values in C<$biblionums> to ensure they are correct.
54 If C<$biblionums> is C<undef>, this won't happen, but you should be sure that
55 999$c is correct on your own then.
56
57 Note that this will modify the original record if C<$biblionums> is supplied.
58 If that's a problem, clone them first.
59
60 =cut
61
62 sub update_index {
63     my ($self, $biblionums, $records) = @_;
64
65     # TODO should have a separate path for dealing with a large number
66     # of records at once where we use the bulk update functions in ES.
67     if ($biblionums) {
68         $self->_sanitise_records($biblionums, $records);
69     }
70
71     my $from    = $self->_convert_marc_to_json($records);
72     if ( !$self->store ) {
73         my $params  = $self->get_elasticsearch_params();
74         $self->store(
75             Catmandu::Store::ElasticSearch->new(
76                 %$params,
77                 index_settings => $self->get_elasticsearch_settings(),
78                 index_mappings => $self->get_elasticsearch_mappings(),
79                 #trace_calls => 1,
80             )
81         );
82     }
83     $self->store->bag->add_many($from);
84     $self->store->bag->commit;
85     return 1;
86 }
87
88 =head2 $indexer->update_index_background($biblionums, $records)
89
90 This has exactly the same API as C<update_index_background> however it'll
91 return immediately. It'll start a background process that does the adding.
92
93 If it fails to add to Elasticsearch then it'll add to a queue that will cause
94 it to be updated by a regular index cron job in the future.
95
96 # TODO implement in the future - I don't know the best way of doing this yet.
97 # If fork: make sure process group is changed so apache doesn't wait for us.
98
99 =cut
100
101 sub update_index_background {
102     my $self = shift;
103     $self->update_index(@_);
104 }
105
106 =head2 $indexer->delete_index($biblionums)
107
108 C<$biblionums> is an arrayref of biblionumbers to delete from the index.
109
110 =cut
111
112 sub delete_index {
113     my ($self, $biblionums) = @_;
114
115     if ( !$self->store ) {
116         my $params  = $self->get_elasticsearch_params();
117         $self->store(
118             Catmandu::Store::ElasticSearch->new(
119                 %$params,
120                 index_settings => $self->get_elasticsearch_settings(),
121                 index_mappings => $self->get_elasticsearch_mappings(),
122                 trace_calls => 1,
123             )
124         );
125     }
126     $self->store->bag->delete($_) foreach @$biblionums;
127     $self->store->bag->commit;
128 }
129
130 =head2 $indexer->delete_index_background($biblionums)
131
132 Identical to L<delete_index>, this will return immediately and start a
133 background process to do the actual deleting.
134
135 =cut
136
137 # TODO implement in the future
138
139 sub delete_index_background {
140     my $self = shift;
141     $self->delete_index(@_);
142 }
143 =head2 $indexer->drop_index();
144
145 Drops the index from the elasticsearch server. Calling C<update_index>
146 after this will recreate it again.
147
148 =cut
149
150 sub drop_index {
151     my ($self) = @_;
152
153     if (!$self->store) {
154         # If this index doesn't exist, this will create it. Then it'll be
155         # deleted. That's not the end of the world however.
156         my $params  = $self->get_elasticsearch_params();
157         $self->store(
158             Catmandu::Store::ElasticSearch->new(
159                 %$params,
160                 index_settings => $self->get_elasticsearch_settings(),
161                 index_mappings => $self->get_elasticsearch_mappings(),
162                 #trace_calls => 1,
163             )
164         );
165     }
166     $self->store->drop();
167     $self->store(undef);
168 }
169
170 sub _sanitise_records {
171     my ($self, $biblionums, $records) = @_;
172
173     confess "Unequal number of values in \$biblionums and \$records." if (@$biblionums != @$records);
174
175     my $c = @$biblionums;
176     for (my $i=0; $i<$c; $i++) {
177         my $bibnum = $biblionums->[$i];
178         my $rec = $records->[$i];
179         # I've seen things you people wouldn't believe. Attack ships on fire
180         # off the shoulder of Orion. I watched C-beams glitter in the dark near
181         # the Tannhauser gate. MARC records where 999$c doesn't match the
182         # biblionumber column. All those moments will be lost in time... like
183         # tears in rain...
184         $rec->delete_fields($rec->field('999'));
185         $rec->append_fields(MARC::Field->new('999','','','c' => $bibnum, 'd' => $bibnum));
186     }
187 }
188
189 sub _convert_marc_to_json {
190     my $self    = shift;
191     my $records = shift;
192     my $importer =
193       Catmandu::Importer::MARC->new( records => $records, id => '999c' );
194     my $fixer = Catmandu::Fix->new( fixes => $self->get_fixer_rules() );
195     $importer = $fixer->fix($importer);
196     return $importer;
197 }
198
199 1;
200
201 __END__
202
203 =head1 AUTHOR
204
205 =over 4
206
207 =item Chris Cormack C<< <chrisc@catalyst.net.nz> >>
208
209 =item Robin Sheat C<< <robin@catalyst.net.nz> >>
210
211 =back
212
213 =cut