View | Details | Raw Unified | Return to bug 24807
Collapse All | Expand All

(-)a/Koha/SearchEngine/Elasticsearch.pm (-25 / +48 lines)
Lines 475-511 sub _process_mappings { Link Here
475
        # Copy (scalar) data since can have multiple targets
475
        # Copy (scalar) data since can have multiple targets
476
        # with differing options for (possibly) mutating data
476
        # with differing options for (possibly) mutating data
477
        # so need a different copy for each
477
        # so need a different copy for each
478
        my $_data = $data;
478
        my $data_copy = $data;
479
        if (defined $options->{substr}) {
479
        if (defined $options->{substr}) {
480
            my ($start, $length) = @{$options->{substr}};
480
            my ($start, $length) = @{$options->{substr}};
481
            $_data = length($data) > $start ? substr $data, $start, $length : '';
481
            $data_copy = length($data) > $start ? substr $data_copy, $start, $length : '';
482
        }
483
484
        # Add data to tokens array for callbacks processing
485
        my $tokens = [$data_copy];
486
487
        # Tokenize callbacks takes as token (possibly tokenized subfield data)
488
        # as argument, and returns a possibly different list of tokens.
489
        # Note that this list also might be empty.
490
        if (defined $options->{tokenize_callbacks}) {
491
            foreach my $callback (@{$options->{tokenize_callbacks}}) {
492
                # Pass each token to current callback which returns a list
493
                # (scalar is fine too) resulting either in a list or
494
                # a list of lists that will be flattened by perl.
495
                # The next callback will recieve the possibly expanded list of tokens.
496
                $tokens = [ map { $callback->($_) } @{$tokens} ];
497
            }
482
        }
498
        }
483
        if (defined $options->{value_callbacks}) {
499
        if (defined $options->{value_callbacks}) {
484
            $_data = reduce { $b->($a) } ($_data, @{$options->{value_callbacks}});
500
            $tokens = [ map { reduce { $b->($a) } ($_, @{$options->{value_callbacks}}) } @{$tokens} ];
485
        }
501
        }
486
        if (defined $options->{filter_callbacks}) {
502
        if (defined $options->{filter_callbacks}) {
487
            # Skip mapping unless all filter callbacks return true
503
            my @tokens_filtered;
488
            next unless all { $_data = $_->($_data) } @{$options->{filter_callbacks}};
504
            foreach my $_data (@{$tokens}) {
505
                if ( all { $_->($_data) } @{$options->{filter_callbacks}} ) {
506
                    push @tokens_filtered, $_data;
507
                }
508
            }
509
            # Overwrite $tokens with filtered values
510
            $tokens = \@tokens_filtered;
489
        }
511
        }
512
        # Skip mapping if all values has been removed
513
        next unless @{$tokens};
514
490
        if (defined $options->{property}) {
515
        if (defined $options->{property}) {
491
            $_data = {
516
            $tokens = [ map { { $options->{property} => $_ } } @{$tokens} ];
492
                $options->{property} => $_data
493
            }
494
        }
517
        }
495
        if (defined $options->{nonfiling_characters_indicator}) {
518
        if (defined $options->{nonfiling_characters_indicator}) {
496
            my $nonfiling_chars = $meta->{field}->indicator($options->{nonfiling_characters_indicator});
519
            my $nonfiling_chars = $meta->{field}->indicator($options->{nonfiling_characters_indicator});
497
            $nonfiling_chars = looks_like_number($nonfiling_chars) ? int($nonfiling_chars) : 0;
520
            $nonfiling_chars = looks_like_number($nonfiling_chars) ? int($nonfiling_chars) : 0;
498
            if ($nonfiling_chars) {
521
            # Nonfiling chars does not make sense for multiple tokens
499
                $_data = substr $_data, $nonfiling_chars;
522
            # Only apply on first element
500
            }
523
            $tokens->[0] = substr $tokens->[0], $nonfiling_chars;
501
        }
524
        }
502
525
503
        $record_document->{$target} //= [];
526
        $record_document->{$target} //= [];
504
        if( ref $_data eq 'ARRAY' ){
527
        push @{$record_document->{$target}}, @{$tokens};
505
            push @{$record_document->{$target}}, @{$_data};
506
        } else {
507
            push @{$record_document->{$target}}, $_data;
508
        }
509
    }
528
    }
510
}
529
}
511
530
Lines 897-912 sub _field_mappings { Link Here
897
        };
916
        };
898
    }
917
    }
899
    elsif ($target_type eq 'year') {
918
    elsif ($target_type eq 'year') {
900
        $default_options->{filter_callbacks} //= [];
919
        $default_options->{tokenize_callbacks} //= [];
901
        push @{$default_options->{filter_callbacks}}, sub {
920
        # Only accept years containing digits and "u"
921
        push @{$default_options->{tokenize_callbacks}}, sub {
902
            my ($value) = @_;
922
            my ($value) = @_;
903
            my @years = ();
923
            my @years = ( $value =~ /[0-9u]{4}/g );
904
            my @field_years = ( $value =~ /[0-9u]{4}/g );
924
            return @years;
905
            foreach my $year (@field_years){
925
        };
906
                $year =~ s/[u]/0/g;
926
907
                push @years, $year;
927
        $default_options->{value_callbacks} //= [];
908
            }
928
        # Replace "u" with "0" for sorting
909
            return \@years;
929
        push @{$default_options->{value_callbacks}}, sub {
930
            my ($value) = @_;
931
            $value =~ s/[u]/0/g;
932
            return $value;
910
        };
933
        };
911
    }
934
    }
912
935
(-)a/t/Koha/SearchEngine/Elasticsearch.t (-3 / +2 lines)
Lines 357-362 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
357
        MARC::Field->new('100', '', '', a => 'Author 2'),
357
        MARC::Field->new('100', '', '', a => 'Author 2'),
358
        # MARC::Field->new('210', '', '', a => 'Title 2'),
358
        # MARC::Field->new('210', '', '', a => 'Title 2'),
359
        # MARC::Field->new('245', '', '', a => 'Title: second record'),
359
        # MARC::Field->new('245', '', '', a => 'Title: second record'),
360
        MARC::Field->new('260', '', '', a => 'New York :', b => 'Ace ,', c => '1963-2003'),
360
        MARC::Field->new('999', '', '', c => '1234568'),
361
        MARC::Field->new('999', '', '', c => '1234568'),
361
        MARC::Field->new('952', '', '', 0 => 1, g => 'string where should be numeric', o => $long_callno),
362
        MARC::Field->new('952', '', '', 0 => 1, g => 'string where should be numeric', o => $long_callno),
362
    );
363
    );
Lines 517-523 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
517
    # Tests for 'year' type
518
    # Tests for 'year' type
518
    is_deeply(
519
    is_deeply(
519
      $docs->[1]->{'copydate'},
520
      $docs->[1]->{'copydate'},
520
      ['1963','2003'],
521
      ['1963', '2003'],
521
      'Second document copydate field should be set correctly'
522
      'Second document copydate field should be set correctly'
522
    );
523
    );
523
    is_deeply(
524
    is_deeply(
Lines 659-665 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents_array () t Link Here
659
        MARC::Field->new('100', '', '', a => 'Author 2'),
660
        MARC::Field->new('100', '', '', a => 'Author 2'),
660
        # MARC::Field->new('210', '', '', a => 'Title 2'),
661
        # MARC::Field->new('210', '', '', a => 'Title 2'),
661
        # MARC::Field->new('245', '', '', a => 'Title: second record'),
662
        # MARC::Field->new('245', '', '', a => 'Title: second record'),
662
        MARC::Field->new('260', '', '', a => 'New York :', b => 'Ace ,', c => '1963-2003'),
663
        MARC::Field->new('999', '', '', c => '1234568'),
663
        MARC::Field->new('999', '', '', c => '1234568'),
664
        MARC::Field->new('952', '', '', 0 => 1, g => 'string where should be numeric'),
664
        MARC::Field->new('952', '', '', 0 => 1, g => 'string where should be numeric'),
665
    );
665
    );
666
- 

Return to bug 24807