Lines 41-46
Koha::SearchEngine::Elasticsearch::Indexer - handles adding new records to the i
Link Here
|
41 |
|
41 |
|
42 |
=head1 FUNCTIONS |
42 |
=head1 FUNCTIONS |
43 |
|
43 |
|
|
|
44 |
=head2 Koha::SearchEngine::Elasticsearch::Indexer->({index => $index_name}); |
45 |
|
46 |
$index_name should be biblios or authorities |
47 |
call our SUPER class and then set ES parameters |
48 |
|
49 |
=cut |
50 |
|
51 |
sub new { |
52 |
my $class = shift @_; |
53 |
my $self = $class->SUPER::new(@_); |
54 |
my $params = $self->get_elasticsearch_params(); |
55 |
$self->store( |
56 |
Catmandu::Store::ElasticSearch->new( |
57 |
%$params, |
58 |
index_settings => $self->get_elasticsearch_settings(), |
59 |
index_mappings => $self->get_elasticsearch_mappings(), |
60 |
) |
61 |
); |
62 |
return $self; |
63 |
} |
64 |
|
44 |
=head2 $indexer->update_index($biblionums, $records); |
65 |
=head2 $indexer->update_index($biblionums, $records); |
45 |
|
66 |
|
46 |
C<$biblionums> is an arrayref containing the biblionumbers for the records. |
67 |
C<$biblionums> is an arrayref containing the biblionumbers for the records. |
Lines 67-84
sub update_index {
Link Here
|
67 |
} |
88 |
} |
68 |
|
89 |
|
69 |
my $from = $self->_convert_marc_to_json($records); |
90 |
my $from = $self->_convert_marc_to_json($records); |
70 |
if ( !$self->store ) { |
|
|
71 |
my $params = $self->get_elasticsearch_params(); |
72 |
$self->store( |
73 |
Catmandu::Store::ElasticSearch->new( |
74 |
%$params, |
75 |
index_settings => $self->get_elasticsearch_settings(), |
76 |
index_mappings => $self->get_elasticsearch_mappings(), |
77 |
) |
78 |
); |
79 |
} |
80 |
|
91 |
|
81 |
#print Data::Dumper::Dumper( $from->to_array ); |
92 |
print Data::Dumper::Dumper( $from->to_array ); |
82 |
$self->store->bag->add_many($from); |
93 |
$self->store->bag->add_many($from); |
83 |
$self->store->bag->commit; |
94 |
$self->store->bag->commit; |
84 |
return 1; |
95 |
return 1; |
Lines 111-126
C<$biblionums> is an arrayref of biblionumbers to delete from the index.
Link Here
|
111 |
sub delete_index { |
122 |
sub delete_index { |
112 |
my ($self, $biblionums) = @_; |
123 |
my ($self, $biblionums) = @_; |
113 |
|
124 |
|
114 |
if ( !$self->store ) { |
|
|
115 |
my $params = $self->get_elasticsearch_params(); |
116 |
$self->store( |
117 |
Catmandu::Store::ElasticSearch->new( |
118 |
%$params, |
119 |
index_settings => $self->get_elasticsearch_settings(), |
120 |
index_mappings => $self->get_elasticsearch_mappings(), |
121 |
) |
122 |
); |
123 |
} |
124 |
$self->store->bag->delete($_) foreach @$biblionums; |
125 |
$self->store->bag->delete($_) foreach @$biblionums; |
125 |
$self->store->bag->commit; |
126 |
$self->store->bag->commit; |
126 |
} |
127 |
} |
Lines 149-168
after this will recreate it again.
Link Here
|
149 |
sub drop_index { |
150 |
sub drop_index { |
150 |
my ($self) = @_; |
151 |
my ($self) = @_; |
151 |
|
152 |
|
152 |
if (!$self->store) { |
|
|
153 |
# If this index doesn't exist, this will create it. Then it'll be |
154 |
# deleted. That's not the end of the world however. |
155 |
my $params = $self->get_elasticsearch_params(); |
156 |
$self->store( |
157 |
Catmandu::Store::ElasticSearch->new( |
158 |
%$params, |
159 |
index_settings => $self->get_elasticsearch_settings(), |
160 |
index_mappings => $self->get_elasticsearch_mappings(), |
161 |
) |
162 |
); |
163 |
} |
164 |
$self->store->drop(); |
153 |
$self->store->drop(); |
165 |
$self->store(undef); |
154 |
my $params = $self->get_elasticsearch_params(); |
|
|
155 |
$self->store( |
156 |
Catmandu::Store::ElasticSearch->new( |
157 |
%$params, |
158 |
index_settings => $self->get_elasticsearch_settings(), |
159 |
index_mappings => $self->get_elasticsearch_mappings(), |
160 |
) |
161 |
); |
162 |
} |
163 |
|
164 |
|
165 |
=head2 $indexer->do_reindex(); |
166 |
|
167 |
Performs a reindex based on records passed in |
168 |
|
169 |
=cut |
170 |
|
171 |
sub do_reindex { |
172 |
my ($self, $next, $delete, $commit, $verbose, $biblionumbers) = @_; |
173 |
|
174 |
_log(1, $verbose, "Indexing ".$self->index."\n"); |
175 |
|
176 |
if ( scalar @$biblionumbers ) { |
177 |
if ( $self->index eq 'biblios' ) { |
178 |
$next = sub { |
179 |
my $r = shift @$biblionumbers; |
180 |
return () unless defined $r; |
181 |
return ( $r, Koha::BiblioUtils->get_from_biblionumber($r, item_data => 1 )); |
182 |
}; |
183 |
} |
184 |
elsif ( $self->index eq 'authorities' ) { |
185 |
$next = sub { |
186 |
my $r = shift @$biblionumbers; |
187 |
return () unless defined $r; |
188 |
my $a = Koha::MetadataRecord::Authority->get_from_authid($r); |
189 |
return ($r, $a->record); |
190 |
}; |
191 |
} |
192 |
} |
193 |
else { |
194 |
my $records = $self->index eq 'biblios' ? |
195 |
Koha::BiblioUtils->get_all_biblios_iterator() : $self->index eq 'authorities' ? |
196 |
Koha::MetadataRecord::Authority->get_all_authorities_iterator() : die "Reindex called on undefined index"; |
197 |
$next = sub { $records->next(); }; |
198 |
} |
199 |
|
200 |
warn |
201 |
$self->drop_index() if ( $delete ); |
202 |
|
203 |
my $count = 0; |
204 |
my $commit_count = $commit; |
205 |
my ( @id_buffer, @commit_buffer ); |
206 |
while ( my $record = $next->() ) { |
207 |
my $id = $record->id; |
208 |
my $record = $record->record; |
209 |
_log( 1, $verbose, "$id\n" ); |
210 |
$count++; |
211 |
|
212 |
push @id_buffer, $id; |
213 |
push @commit_buffer, $record; |
214 |
if ( !( --$commit_count ) ) { |
215 |
_log( 2, $verbose, "Committing...\n" ); |
216 |
$self->update_index( \@id_buffer, \@commit_buffer ); |
217 |
$commit_count = $commit; |
218 |
@id_buffer = (); |
219 |
@commit_buffer = (); |
220 |
} |
221 |
} |
222 |
# There are probably uncommitted records (beacuse we didn't hit exact commit count) |
223 |
$self->update_index( \@id_buffer, \@commit_buffer ); |
224 |
_log( 1, $verbose, "$count records indexed.\n" ); |
225 |
} |
226 |
|
227 |
|
228 |
sub _log { |
229 |
my ($level, $verbose, $msg) = @_; |
230 |
print $msg if ($verbose >= $level); |
166 |
} |
231 |
} |
167 |
|
232 |
|
168 |
sub _sanitise_records { |
233 |
sub _sanitise_records { |