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

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

Return to bug 24807