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

(-)a/Koha/SearchEngine/Elasticsearch.pm (-41 / +20 lines)
Lines 477-526 sub _process_mappings { Link Here
477
            $data_copy = length($data) > $start ? substr $data_copy, $start, $length : '';
477
            $data_copy = length($data) > $start ? substr $data_copy, $start, $length : '';
478
        }
478
        }
479
479
480
        # Add data to tokens array for callbacks processing
480
        # Add data to values array for callbacks processing
481
        my $tokens = [$data_copy];
481
        my $values = [$data_copy];
482
482
483
        # Tokenize callbacks takes as token (possibly tokenized subfield data)
483
        # Value callbacks takes subfield data (or values from previous
484
        # as argument, and returns a possibly different list of tokens.
484
        # callbacks) as argument, and returns a possibly different list of values.
485
        # Note that this list also might be empty.
485
        # Note that the returned list may also be empty.
486
        if (defined $options->{tokenize_callbacks}) {
486
        if (defined $options->{value_callbacks}) {
487
            foreach my $callback (@{$options->{tokenize_callbacks}}) {
487
            foreach my $callback (@{$options->{value_callbacks}}) {
488
                # Pass each token to current callback which returns a list
488
                # Pass each value to current callback which returns a list
489
                # (scalar is fine too) resulting either in a list or
489
                # (scalar is fine too) resulting either in a list or
490
                # a list of lists that will be flattened by perl.
490
                # a list of lists that will be flattened by perl.
491
                # The next callback will recieve the possibly expanded list of tokens.
491
                # The next callback will recieve the possibly expanded list of values.
492
                $tokens = [ map { $callback->($_) } @{$tokens} ];
492
                $values = [ map { $callback->($_) } @{$values} ];
493
            }
494
        }
495
        if (defined $options->{value_callbacks}) {
496
            $tokens = [ map { reduce { $b->($a) } ($_, @{$options->{value_callbacks}}) } @{$tokens} ];
497
        }
498
        if (defined $options->{filter_callbacks}) {
499
            my @tokens_filtered;
500
            foreach my $_data (@{$tokens}) {
501
                if ( all { $_->($_data) } @{$options->{filter_callbacks}} ) {
502
                    push @tokens_filtered, $_data;
503
                }
504
            }
493
            }
505
            # Overwrite $tokens with filtered values
506
            $tokens = \@tokens_filtered;
507
        }
494
        }
495
508
        # Skip mapping if all values has been removed
496
        # Skip mapping if all values has been removed
509
        next unless @{$tokens};
497
        next unless @{$values};
510
498
511
        if (defined $options->{property}) {
499
        if (defined $options->{property}) {
512
            $tokens = [ map { { $options->{property} => $_ } } @{$tokens} ];
500
            $values = [ map { { $options->{property} => $_ } } @{$values} ];
513
        }
501
        }
514
        if (defined $options->{nonfiling_characters_indicator}) {
502
        if (defined $options->{nonfiling_characters_indicator}) {
515
            my $nonfiling_chars = $meta->{field}->indicator($options->{nonfiling_characters_indicator});
503
            my $nonfiling_chars = $meta->{field}->indicator($options->{nonfiling_characters_indicator});
516
            $nonfiling_chars = looks_like_number($nonfiling_chars) ? int($nonfiling_chars) : 0;
504
            $nonfiling_chars = looks_like_number($nonfiling_chars) ? int($nonfiling_chars) : 0;
517
            # Nonfiling chars does not make sense for multiple tokens
505
            # Nonfiling chars does not make sense for multiple values
518
            # Only apply on first element
506
            # Only apply on first element
519
            $tokens->[0] = substr $tokens->[0], $nonfiling_chars;
507
            $values->[0] = substr $values->[0], $nonfiling_chars;
520
        }
508
        }
521
509
522
        $record_document->{$target} //= [];
510
        $record_document->{$target} //= [];
523
        push @{$record_document->{$target}}, @{$tokens};
511
        push @{$record_document->{$target}}, @{$values};
524
    }
512
    }
525
}
513
}
526
514
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