Lines 34-39
use Search::Elasticsearch;
Link Here
|
34 |
use Try::Tiny; |
34 |
use Try::Tiny; |
35 |
use YAML::Syck; |
35 |
use YAML::Syck; |
36 |
|
36 |
|
|
|
37 |
use List::Util qw( sum0 reduce ); |
38 |
use Search::Elasticsearch; |
39 |
use MARC::File::XML; |
40 |
|
37 |
__PACKAGE__->mk_ro_accessors(qw( index )); |
41 |
__PACKAGE__->mk_ro_accessors(qw( index )); |
38 |
__PACKAGE__->mk_accessors(qw( sort_fields )); |
42 |
__PACKAGE__->mk_accessors(qw( sort_fields )); |
39 |
|
43 |
|
Lines 67-72
sub new {
Link Here
|
67 |
return $self; |
71 |
return $self; |
68 |
} |
72 |
} |
69 |
|
73 |
|
|
|
74 |
sub get_elasticsearch { |
75 |
my $self = shift @_; |
76 |
unless (defined $self->{elasticsearch}) { |
77 |
my $conf = $self->get_elasticsearch_params(); |
78 |
$self->{elasticsearch} = Search::Elasticsearch->new( |
79 |
client => "5_0::Direct", |
80 |
nodes => $conf->{nodes}, |
81 |
cxn_pool => 'Sniff' |
82 |
); |
83 |
} |
84 |
return $self->{elasticsearch}; |
85 |
} |
86 |
|
70 |
=head2 get_elasticsearch_params |
87 |
=head2 get_elasticsearch_params |
71 |
|
88 |
|
72 |
my $params = $self->get_elasticsearch_params(); |
89 |
my $params = $self->get_elasticsearch_params(); |
Lines 281-328
sub sort_fields {
Link Here
|
281 |
return $self->_sort_fields_accessor(); |
298 |
return $self->_sort_fields_accessor(); |
282 |
} |
299 |
} |
283 |
|
300 |
|
284 |
# Provides the rules for data conversion. |
301 |
sub marc_records_to_documents { |
285 |
sub get_fixer_rules { |
302 |
my ($self, $records) = @_; |
|
|
303 |
my $rules = $self->get_marc_mapping_rules(); |
304 |
my $control_fields_rules = $rules->{control_fields}; |
305 |
my $data_fields_rules = $rules->{data_fields}; |
306 |
my $marcflavour = lc C4::Context->preference('marcflavour'); |
307 |
|
308 |
my @record_documents; |
309 |
|
310 |
sub _process_mappings { |
311 |
my ($mappings, $data, $record_document) = @_; |
312 |
foreach my $mapping (@{$mappings}) { |
313 |
my ($target, $options) = @{$mapping}; |
314 |
# Copy (scalar) data since can have multiple targets |
315 |
# with differing options for (possibly) mutating data |
316 |
# so need a different copy for each |
317 |
my $_data = $data; |
318 |
$record_document->{$target} //= []; |
319 |
if (defined $options->{substr}) { |
320 |
my ($start, $length) = @{$options->{substr}}; |
321 |
$_data = length($data) > $start ? substr $data, $start, $length : ''; |
322 |
} |
323 |
if (defined $options->{value_callbacks}) { |
324 |
$_data = reduce { $b->($a) } ($_data, @{$options->{value_callbacks}}); |
325 |
} |
326 |
if (defined $options->{property}) { |
327 |
$_data = { |
328 |
$options->{property} => $_data |
329 |
} |
330 |
} |
331 |
push @{$record_document->{$target}}, $_data; |
332 |
} |
333 |
} |
334 |
foreach my $record (@{$records}) { |
335 |
my $record_document = {}; |
336 |
my $mappings = $rules->{leader}; |
337 |
if ($mappings) { |
338 |
_process_mappings($mappings, $record->leader(), $record_document); |
339 |
} |
340 |
foreach my $field ($record->fields()) { |
341 |
if($field->is_control_field()) { |
342 |
my $mappings = $control_fields_rules->{$field->tag()}; |
343 |
if ($mappings) { |
344 |
_process_mappings($mappings, $field->data(), $record_document); |
345 |
} |
346 |
} |
347 |
else { |
348 |
my $subfields_mappings = $data_fields_rules->{$field->tag()}; |
349 |
if ($subfields_mappings) { |
350 |
my $wildcard_mappings = $subfields_mappings->{'*'}; |
351 |
foreach my $subfield ($field->subfields()) { |
352 |
my ($code, $data) = @{$subfield}; |
353 |
my $mappings = $subfields_mappings->{$code} // []; |
354 |
if ($wildcard_mappings) { |
355 |
$mappings = [@{$mappings}, @{$wildcard_mappings}]; |
356 |
} |
357 |
if (@{$mappings}) { |
358 |
_process_mappings($mappings, $data, $record_document); |
359 |
} |
360 |
} |
361 |
} |
362 |
} |
363 |
} |
364 |
foreach my $field (keys %{$rules->{defaults}}) { |
365 |
unless (defined $record_document->{$field}) { |
366 |
$record_document->{$field} = $rules->{defaults}->{$field}; |
367 |
} |
368 |
} |
369 |
foreach my $field (@{$rules->{sum}}) { |
370 |
if (defined $record_document->{$field}) { |
371 |
use Data::Dumper; |
372 |
# TODO: validate numeric? filter? |
373 |
# TODO: Or should only accept fields without nested values? |
374 |
# TODO: Quick and dirty, improve if needed |
375 |
$record_document->{$field} = sum0(grep { !ref($_) && m/\d+(\.\d+)?/} @{$record_document->{$field}}); |
376 |
} |
377 |
} |
378 |
# TODO: Perhaps should check if $records_document non empty, but really should never be the case |
379 |
$record->encoding('UTF-8'); |
380 |
$record_document->{'marc_xml'} = $record->as_xml_record($marcflavour); |
381 |
my $id = $record->subfield('999', 'c'); |
382 |
push @record_documents, [$id, $record_document]; |
383 |
} |
384 |
return \@record_documents; |
385 |
} |
386 |
|
387 |
# Provides the rules for marc to Elasticsearch JSON document conversion. |
388 |
sub get_marc_mapping_rules { |
286 |
my ($self) = @_; |
389 |
my ($self) = @_; |
287 |
|
390 |
|
288 |
my $marcflavour = lc C4::Context->preference('marcflavour'); |
391 |
my $marcflavour = lc C4::Context->preference('marcflavour'); |
289 |
my @rules; |
392 |
my @rules; |
290 |
|
393 |
|
291 |
$self->_foreach_mapping( |
394 |
sub _field_mappings { |
292 |
sub { |
395 |
my ($facet, $suggestible, $sort, $target_name, $target_type, $range) = @_; |
293 |
my ( $name, $type, $facet, $suggestible, $sort, $marc_type, $marc_field ) = @_; |
396 |
my %mapping_defaults = (); |
294 |
return if $marc_type ne $marcflavour; |
397 |
my @mappings; |
295 |
my $options =''; |
398 |
|
|
|
399 |
my $substr_args = undef; |
400 |
if ($range) { |
401 |
# TODO: use value_callback instead? |
402 |
my ($start, $end) = map(int, split /-/, $range, 2); |
403 |
$substr_args = [$start]; |
404 |
push @{$substr_args}, (defined $end ? $end - $start + 1 : 1); |
405 |
} |
406 |
my $default_options = {}; |
407 |
if ($substr_args) { |
408 |
$default_options->{substr} = $substr_args; |
409 |
} |
410 |
|
411 |
# TODO: Should probably have per type value callback/hook |
412 |
# but hard code for now |
413 |
if ($target_type eq 'boolean') { |
414 |
$default_options->{value_callbacks} //= []; |
415 |
push @{$default_options->{value_callbacks}}, sub { |
416 |
my ($value) = @_; |
417 |
# Trim whitespace at both ends |
418 |
$value =~ s/^\s+|\s+$//g; |
419 |
return $value ? 'true' : 'false'; |
420 |
}; |
421 |
} |
296 |
|
422 |
|
297 |
push @rules, "marc_map('$marc_field','${name}.\$append', $options)"; |
423 |
my $mapping = [$target_name, $default_options]; |
298 |
if ($facet) { |
424 |
push @mappings, $mapping; |
299 |
push @rules, "marc_map('$marc_field','${name}__facet.\$append', $options)"; |
425 |
|
|
|
426 |
my @suffixes = (); |
427 |
push @suffixes, 'facet' if $facet; |
428 |
push @suffixes, 'suggestion' if $suggestible; |
429 |
push @suffixes, 'sort' if !defined $sort || $sort; |
430 |
|
431 |
foreach my $suffix (@suffixes) { |
432 |
my $mapping = ["${target_name}__$suffix"]; |
433 |
# Hack, fix later in less hideous manner |
434 |
if ($suffix eq 'suggestion') { |
435 |
push @{$mapping}, {%{$default_options}, property => 'input'}; |
300 |
} |
436 |
} |
301 |
if ($suggestible) { |
437 |
else { |
302 |
push @rules, |
438 |
push @{$mapping}, $default_options; |
303 |
#"marc_map('$marc_field','${name}__suggestion.input.\$append', '')"; #must not have nested data structures in .input |
|
|
304 |
"marc_map('$marc_field','${name}__suggestion.input.\$append')"; |
305 |
} |
439 |
} |
306 |
if ( $type eq 'boolean' ) { |
440 |
push @mappings, $mapping; |
|
|
441 |
} |
442 |
return @mappings; |
443 |
}; |
444 |
my $field_spec_regexp = qr/^([0-9]{3})([0-9a-z]+)?(?:_\/(\d+(?:-\d+)?))?$/; |
445 |
my $leader_regexp = qr/^leader(?:_\/(\d+(?:-\d+)?))?$/; |
446 |
my $rules = { |
447 |
'leader' => [], |
448 |
'control_fields' => {}, |
449 |
'data_fields' => {}, |
450 |
'sum' => [], |
451 |
'defaults' => {} |
452 |
}; |
453 |
|
454 |
$self->_foreach_mapping(sub { |
455 |
my ( $name, $type, $facet, $suggestible, $sort, $marc_type, $marc_field ) = @_; |
456 |
return if $marc_type ne $marcflavour; |
457 |
|
458 |
if ($type eq 'sum') { |
459 |
push @{$rules->{sum}}, $name; |
460 |
} |
461 |
elsif($type eq 'boolean') { |
462 |
# boolean gets special handling, if value doesn't exist for a field, |
463 |
# it is set to false |
464 |
$rules->{defaults}->{$name} = 'false'; |
465 |
} |
307 |
|
466 |
|
308 |
# boolean gets special handling, basically if it doesn't exist, |
467 |
if ($marc_field =~ $field_spec_regexp) { |
309 |
# it's added and set to false. Otherwise we can't query it. |
468 |
my $field_tag = $1; |
310 |
push @rules, |
469 |
my $subfields = defined $2 ? $2 : '*'; |
311 |
"unless exists('$name') add_field('$name', 0) end"; |
470 |
my $range = defined $3 ? $3 : undef; |
312 |
} |
471 |
if ($field_tag < 10) { |
313 |
if ($type eq 'sum' ) { |
472 |
$rules->{control_fields}->{$field_tag} //= []; |
314 |
push @rules, "sum('$name')"; |
473 |
my @mappings = _field_mappings($facet, $suggestible, $sort, $name, $type, $range); |
|
|
474 |
push @{$rules->{control_fields}->{$field_tag}}, @mappings; |
315 |
} |
475 |
} |
316 |
if ($self->sort_fields()->{$name}) { |
476 |
else { |
317 |
if ($sort || !defined $sort) { |
477 |
$rules->{data_fields}->{$field_tag} //= {}; |
318 |
push @rules, "marc_map('$marc_field','${name}__sort.\$append', $options)"; |
478 |
foreach my $subfield (split //, $subfields) { |
|
|
479 |
$rules->{data_fields}->{$field_tag}->{$subfield} //= []; |
480 |
my @mappings = _field_mappings($facet, $suggestible, $sort, $name, $type, $range); |
481 |
push @{$rules->{data_fields}->{$field_tag}->{$subfield}}, @mappings; |
319 |
} |
482 |
} |
320 |
} |
483 |
} |
321 |
} |
484 |
} |
322 |
); |
485 |
elsif ($marc_field =~ $leader_regexp) { |
323 |
|
486 |
my $range = defined $1 ? $1 : undef; |
324 |
push @rules, "move_field(_id,es_id)"; #Also you must set the Catmandu::Store::ElasticSearch->new(key_prefix: 'es_'); |
487 |
my @mappings = _field_mappings($facet, $suggestible, $sort, $name, $type, $range); |
325 |
return \@rules; |
488 |
push @{$rules->{leader}}, @mappings; |
|
|
489 |
} |
490 |
else { |
491 |
die("Invalid marc field: $marc_field"); |
492 |
} |
493 |
}); |
494 |
return $rules; |
326 |
} |
495 |
} |
327 |
|
496 |
|
328 |
=head2 _foreach_mapping |
497 |
=head2 _foreach_mapping |