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

(-)a/Koha/SearchEngine/Elasticsearch.pm (-41 / +20 lines)
Lines 481-530 sub _process_mappings { Link Here
481
            $data_copy = length($data) > $start ? substr $data_copy, $start, $length : '';
481
            $data_copy = length($data) > $start ? substr $data_copy, $start, $length : '';
482
        }
482
        }
483
483
484
        # Add data to tokens array for callbacks processing
484
        # Add data to values array for callbacks processing
485
        my $tokens = [$data_copy];
485
        my $values = [$data_copy];
486
486
487
        # Tokenize callbacks takes as token (possibly tokenized subfield data)
487
        # Value callbacks takes subfield data (or values from previous
488
        # as argument, and returns a possibly different list of tokens.
488
        # callbacks) as argument, and returns a possibly different list of values.
489
        # Note that this list also might be empty.
489
        # Note that the returned list may also be empty.
490
        if (defined $options->{tokenize_callbacks}) {
490
        if (defined $options->{value_callbacks}) {
491
            foreach my $callback (@{$options->{tokenize_callbacks}}) {
491
            foreach my $callback (@{$options->{value_callbacks}}) {
492
                # Pass each token to current callback which returns a list
492
                # Pass each value to current callback which returns a list
493
                # (scalar is fine too) resulting either in a list or
493
                # (scalar is fine too) resulting either in a list or
494
                # a list of lists that will be flattened by perl.
494
                # a list of lists that will be flattened by perl.
495
                # The next callback will recieve the possibly expanded list of tokens.
495
                # The next callback will recieve the possibly expanded list of values.
496
                $tokens = [ map { $callback->($_) } @{$tokens} ];
496
                $values = [ map { $callback->($_) } @{$values} ];
497
            }
498
        }
499
        if (defined $options->{value_callbacks}) {
500
            $tokens = [ map { reduce { $b->($a) } ($_, @{$options->{value_callbacks}}) } @{$tokens} ];
501
        }
502
        if (defined $options->{filter_callbacks}) {
503
            my @tokens_filtered;
504
            foreach my $_data (@{$tokens}) {
505
                if ( all { $_->($_data) } @{$options->{filter_callbacks}} ) {
506
                    push @tokens_filtered, $_data;
507
                }
508
            }
497
            }
509
            # Overwrite $tokens with filtered values
510
            $tokens = \@tokens_filtered;
511
        }
498
        }
499
512
        # Skip mapping if all values has been removed
500
        # Skip mapping if all values has been removed
513
        next unless @{$tokens};
501
        next unless @{$values};
514
502
515
        if (defined $options->{property}) {
503
        if (defined $options->{property}) {
516
            $tokens = [ map { { $options->{property} => $_ } } @{$tokens} ];
504
            $values = [ map { { $options->{property} => $_ } } @{$values} ];
517
        }
505
        }
518
        if (defined $options->{nonfiling_characters_indicator}) {
506
        if (defined $options->{nonfiling_characters_indicator}) {
519
            my $nonfiling_chars = $meta->{field}->indicator($options->{nonfiling_characters_indicator});
507
            my $nonfiling_chars = $meta->{field}->indicator($options->{nonfiling_characters_indicator});
520
            $nonfiling_chars = looks_like_number($nonfiling_chars) ? int($nonfiling_chars) : 0;
508
            $nonfiling_chars = looks_like_number($nonfiling_chars) ? int($nonfiling_chars) : 0;
521
            # Nonfiling chars does not make sense for multiple tokens
509
            # Nonfiling chars does not make sense for multiple values
522
            # Only apply on first element
510
            # Only apply on first element
523
            $tokens->[0] = substr $tokens->[0], $nonfiling_chars;
511
            $values->[0] = substr $values->[0], $nonfiling_chars;
524
        }
512
        }
525
513
526
        $record_document->{$target} //= [];
514
        $record_document->{$target} //= [];
527
        push @{$record_document->{$target}}, @{$tokens};
515
        push @{$record_document->{$target}}, @{$values};
528
    }
516
    }
529
}
517
}
530
518
Lines 916-935 sub _field_mappings { Link Here
916
        };
904
        };
917
    }
905
    }
918
    elsif ($target_type eq 'year') {
906
    elsif ($target_type eq 'year') {
919
        $default_options->{tokenize_callbacks} //= [];
920
        # Only accept years containing digits and "u"
921
        push @{$default_options->{tokenize_callbacks}}, sub {
922
            my ($value) = @_;
923
            my @years = ( $value =~ /[0-9u]{4}/g );
924
            return @years;
925
        };
926
927
        $default_options->{value_callbacks} //= [];
907
        $default_options->{value_callbacks} //= [];
928
        # Replace "u" with "0" for sorting
908
        # Only accept years containing digits and "u"
929
        push @{$default_options->{value_callbacks}}, sub {
909
        push @{$default_options->{value_callbacks}}, sub {
930
            my ($value) = @_;
910
            my ($value) = @_;
931
            $value =~ s/[u]/0/g;
911
            # Replace "u" with "0" for sorting
932
            return $value;
912
            return map { s/[u]/0/gr } ( $value =~ /[0-9u]{4}/g );
933
        };
913
        };
934
    }
914
    }
935
915
936
- 

Return to bug 24807