|
Lines 39-44
use MARC::File::XML;
Link Here
|
| 39 |
use MIME::Base64; |
39 |
use MIME::Base64; |
| 40 |
use Encode qw(encode); |
40 |
use Encode qw(encode); |
| 41 |
use Business::ISBN; |
41 |
use Business::ISBN; |
|
|
42 |
use Koha::Database; |
| 42 |
|
43 |
|
| 43 |
__PACKAGE__->mk_ro_accessors(qw( index )); |
44 |
__PACKAGE__->mk_ro_accessors(qw( index )); |
| 44 |
__PACKAGE__->mk_accessors(qw( sort_fields )); |
45 |
__PACKAGE__->mk_accessors(qw( sort_fields )); |
|
Lines 274-299
sub _get_elasticsearch_mapping {
Link Here
|
| 274 |
return; |
275 |
return; |
| 275 |
} |
276 |
} |
| 276 |
|
277 |
|
| 277 |
sub reset_elasticsearch_mappings { |
278 |
sub sync_elasticsearch_mappings { |
| 278 |
my ( $reset_fields ) = @_; |
279 |
my ($self, $options) = @_; |
|
|
280 |
$options //= {}; |
| 279 |
my $mappings_yaml = C4::Context->config('elasticsearch_index_mappings'); |
281 |
my $mappings_yaml = C4::Context->config('elasticsearch_index_mappings'); |
| 280 |
$mappings_yaml ||= C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/mappings.yaml'; |
282 |
$mappings_yaml ||= C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/mappings.yaml'; |
| 281 |
my $indexes = LoadFile( $mappings_yaml ); |
283 |
my $indexes = LoadFile( $mappings_yaml ); |
| 282 |
|
284 |
|
| 283 |
while ( my ( $index_name, $fields ) = each %$indexes ) { |
285 |
while ( my ( $index_name, $fields ) = each %$indexes ) { |
| 284 |
while ( my ( $field_name, $data ) = each %$fields ) { |
286 |
while ( my ( $field_name, $data ) = each %$fields ) { |
| 285 |
my $field_type = $data->{type}; |
287 |
my $search_field = Koha::SearchFields->find({ 'name' => $field_name }); |
| 286 |
my $field_label = $data->{label}; |
288 |
if ($search_field) { |
| 287 |
my $mappings = $data->{mappings}; |
289 |
next if $options->{insert_only}; |
| 288 |
my $search_field = Koha::SearchFields->find_or_create({ name => $field_name, label => $field_label, type => $field_type }, { key => 'name' }); |
290 |
} |
| 289 |
for my $mapping ( @$mappings ) { |
291 |
else { |
| 290 |
my $marc_field = Koha::SearchMarcMaps->find_or_create({ index_name => $index_name, marc_type => $mapping->{marc_type}, marc_field => $mapping->{marc_field} }); |
292 |
my $rs = Koha::SearchFields->_resultset()->create({ name => $field_name, label => $data->{label}, type => $data->{type}}); |
| 291 |
$search_field->add_to_search_marc_maps($marc_field, { facet => $mapping->{facet} || 0, suggestible => $mapping->{suggestible} || 0, sort => $mapping->{sort} } ); |
293 |
$search_field = Koha::SearchFields->object_class->_new_from_dbic($rs); |
|
|
294 |
} |
| 295 |
if ($options->{revert_mappings}) { |
| 296 |
# Delete all current marc_targets for field |
| 297 |
my $rs = $search_field->_result()->search_marc_maps(); |
| 298 |
while (my $marc_map = $rs->next) { |
| 299 |
$search_field->_result()->remove_from_search_marc_maps($marc_map); |
| 300 |
# Check if other search fields uses mapping, if not delete |
| 301 |
$marc_map->delete unless (defined $marc_map->search_fields()->first); |
| 302 |
} |
| 303 |
} |
| 304 |
for my $mapping ( @{$data->{mappings}} ) { |
| 305 |
my $marc_field = Koha::SearchMarcMaps->find_or_create({ |
| 306 |
index_name => $index_name, |
| 307 |
marc_type => $mapping->{marc_type}, |
| 308 |
marc_field => $mapping->{marc_field} |
| 309 |
}); |
| 310 |
# If merging mappings relation may already exist, remove to avoid duplicate entry |
| 311 |
if(!($options->{revert_mappings} || $options->{insert_only})) { |
| 312 |
$search_field->_result()->remove_from_search_marc_maps($marc_field->_result()); |
| 313 |
} |
| 314 |
$search_field->add_to_search_marc_maps($marc_field, { |
| 315 |
facet => $mapping->{facet} || 0, |
| 316 |
suggestible => $mapping->{suggestible} || 0, |
| 317 |
sort => $mapping->{sort} |
| 318 |
}); |
| 292 |
} |
319 |
} |
| 293 |
} |
320 |
} |
| 294 |
} |
321 |
} |
| 295 |
} |
322 |
} |
| 296 |
|
323 |
|
|
|
324 |
sub reset_elasticsearch_mappings { |
| 325 |
my $self = shift; |
| 326 |
Koha::SearchFields->search->delete; |
| 327 |
Koha::SearchMarcMaps->search->delete; |
| 328 |
$self->sync_elasticsearch_mappings(); |
| 329 |
} |
| 330 |
|
| 297 |
# This overrides the accessor provided by Class::Accessor so that if |
331 |
# This overrides the accessor provided by Class::Accessor so that if |
| 298 |
# sort_fields isn't set, then it'll generate it. |
332 |
# sort_fields isn't set, then it'll generate it. |
| 299 |
sub sort_fields { |
333 |
sub sort_fields { |