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

(-)a/Koha/Filter/MARC/EmbedSeeFromHeadings.pm (-6 / +14 lines)
Lines 58-79 sub filter { Link Here
58
    if (ref $record eq 'ARRAY') {
58
    if (ref $record eq 'ARRAY') {
59
        my @recarray;
59
        my @recarray;
60
        foreach my $thisrec (@$record) {
60
        foreach my $thisrec (@$record) {
61
            push @recarray, _processrecord($thisrec);
61
            push @recarray, $self->_processrecord($thisrec);
62
        }
62
        }
63
        $newrecord = \@recarray;
63
        $newrecord = \@recarray;
64
    } elsif (ref $record eq 'MARC::Record') {
64
    } elsif (ref $record eq 'MARC::Record') {
65
        $newrecord = _processrecord($record);
65
        $newrecord = $self->_processrecord($record);
66
    }
66
    }
67
67
68
    return $newrecord;
68
    return $newrecord;
69
}
69
}
70
70
71
sub _processrecord {
71
sub _processrecord {
72
    my $record = shift;
72
    my ($self, $record) = @_;
73
74
    $record->append_fields($self->fields($record));
75
76
    return $record;
77
}
78
79
sub fields {
80
    my ($self, $record) = @_;
73
81
74
    my ($item_tag) = GetMarcFromKohaField( "items.itemnumber" );
82
    my ($item_tag) = GetMarcFromKohaField( "items.itemnumber" );
75
    $item_tag ||= '';
83
    $item_tag ||= '';
76
84
85
    my @newfields;
77
    foreach my $field ( $record->fields() ) {
86
    foreach my $field ( $record->fields() ) {
78
        next if $field->is_control_field();
87
        next if $field->is_control_field();
79
        next if $field->tag() eq $item_tag;
88
        next if $field->tag() eq $item_tag;
Lines 85-91 sub _processrecord { Link Here
85
        next unless $authority;
94
        next unless $authority;
86
        my $auth_marc = $authority->record;
95
        my $auth_marc = $authority->record;
87
        my @seefrom = $auth_marc->field('4..');
96
        my @seefrom = $auth_marc->field('4..');
88
        my @newfields;
89
        foreach my $authfield (@seefrom) {
97
        foreach my $authfield (@seefrom) {
90
            my $tag = substr($field->tag(), 0, 1) . substr($authfield->tag(), 1, 2);
98
            my $tag = substr($field->tag(), 0, 1) . substr($authfield->tag(), 1, 2);
91
            next if MARC::Field->is_controlfield_tag($tag);
99
            next if MARC::Field->is_controlfield_tag($tag);
Lines 100-108 sub _processrecord { Link Here
100
            $newfield->delete_subfield( code => '9' );
108
            $newfield->delete_subfield( code => '9' );
101
            push @newfields, $newfield if (scalar($newfield->subfields()) > 0);
109
            push @newfields, $newfield if (scalar($newfield->subfields()) > 0);
102
        }
110
        }
103
        $record->append_fields(@newfields);
104
    }
111
    }
105
    return $record;
112
113
    return @newfields;
106
}
114
}
107
115
108
1;
116
1;
(-)a/Koha/SearchEngine/Elasticsearch.pm (+49 lines)
Lines 24-29 use C4::Context; Link Here
24
use Koha::Database;
24
use Koha::Database;
25
use Koha::Exceptions::Config;
25
use Koha::Exceptions::Config;
26
use Koha::Exceptions::Elasticsearch;
26
use Koha::Exceptions::Elasticsearch;
27
use Koha::Filter::MARC::EmbedSeeFromHeadings;
27
use Koha::SearchFields;
28
use Koha::SearchFields;
28
use Koha::SearchMarcMaps;
29
use Koha::SearchMarcMaps;
29
use Koha::Caches;
30
use Koha::Caches;
Lines 639-644 sub marc_records_to_documents { Link Here
639
                }
640
                }
640
            }
641
            }
641
        }
642
        }
643
644
        if (C4::Context->preference('IncludeSeeFromInSearches') and $self->index eq 'biblios') {
645
            foreach my $field (Koha::Filter::MARC::EmbedSeeFromHeadings->new->fields($record)) {
646
                my $data_field_rules = $data_fields_rules->{$field->tag()};
647
                if ($data_field_rules) {
648
                    my $subfields_mappings = $data_field_rules->{subfields};
649
                    my $wildcard_mappings = $subfields_mappings->{'*'};
650
                    foreach my $subfield ($field->subfields()) {
651
                        my ($code, $data) = @{$subfield};
652
                        my @mappings;
653
                        push @mappings, @{ $subfields_mappings->{$code} } if $subfields_mappings->{$code};
654
                        push @mappings, @$wildcard_mappings if $wildcard_mappings;
655
                        # Do not include "see from" into these kind of fields
656
                        @mappings = grep { $_->[0] !~ /__(sort|facet|suggestion)$/ } @mappings;
657
                        if (@mappings) {
658
                            $self->_process_mappings(\@mappings, $data, $record_document, {
659
                                    data_source => 'subfield',
660
                                    code => $code,
661
                                    field => $field
662
                                }
663
                            );
664
                        }
665
                    }
666
667
                    my $subfields_join_mappings = $data_field_rules->{subfields_join};
668
                    if ($subfields_join_mappings) {
669
                        foreach my $subfields_group (keys %{$subfields_join_mappings}) {
670
                            my $data_field = $field->clone;
671
                            # remove empty subfields, otherwise they are printed as a space
672
                            $data_field->delete_subfield(match => qr/^$/);
673
                            my $data = $data_field->as_string( $subfields_group );
674
                            if ($data) {
675
                                my @mappings = @{ $subfields_join_mappings->{$subfields_group} };
676
                                # Do not include "see from" into these kind of fields
677
                                @mappings = grep { $_->[0] !~ /__(sort|facet|suggestion)$/ } @mappings;
678
                                $self->_process_mappings(\@mappings, $data, $record_document, {
679
                                        data_source => 'subfields_group',
680
                                        codes => $subfields_group,
681
                                        field => $field
682
                                    }
683
                                );
684
                            }
685
                        }
686
                    }
687
                }
688
            }
689
        }
690
642
        foreach my $field (keys %{$rules->{defaults}}) {
691
        foreach my $field (keys %{$rules->{defaults}}) {
643
            unless (defined $record_document->{$field}) {
692
            unless (defined $record_document->{$field}) {
644
                $record_document->{$field} = $rules->{defaults}->{$field};
693
                $record_document->{$field} = $rules->{defaults}->{$field};
(-)a/t/db_dependent/Koha/SearchEngine/Elasticsearch.t (-2 / +77 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 6;
20
use Test::More tests => 7;
21
use Test::Exception;
21
use Test::Exception;
22
22
23
use t::lib::Mocks;
23
use t::lib::Mocks;
Lines 29-34 use MARC::Record; Link Here
29
use Try::Tiny;
29
use Try::Tiny;
30
use List::Util qw( any );
30
use List::Util qw( any );
31
31
32
use C4::AuthoritiesMarc;
33
32
use Koha::SearchEngine::Elasticsearch;
34
use Koha::SearchEngine::Elasticsearch;
33
use Koha::SearchEngine::Elasticsearch::Search;
35
use Koha::SearchEngine::Elasticsearch::Search;
34
36
Lines 850-853 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () authori Link Here
850
852
851
};
853
};
852
854
855
subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents with IncludeSeeFromInSearches' => sub {
856
857
    plan tests => 4;
858
859
    t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
860
    t::lib::Mocks::mock_preference('IncludeSeeFromInSearches', '1');
861
862
    my $builder = t::lib::TestBuilder->new;
863
    my $auth_type = $builder->build_object({
864
        class => 'Koha::Authority::Types',
865
        value => {
866
            auth_tag_to_report => '150'
867
        }
868
    });
869
    my $authority_record = MARC::Record->new();
870
    $authority_record->append_fields(
871
        MARC::Field->new(150, '', '', a => 'Foo'),
872
        MARC::Field->new(450, '', '', a => 'Bar'),
873
    );
874
    my $authid = AddAuthority($authority_record, undef, $auth_type->authtypecode);
875
876
    my @mappings = (
877
        {
878
            name => 'subject',
879
            type => 'string',
880
            facet => 1,
881
            suggestible => 1,
882
            sort => undef,
883
            searchable => 1,
884
            marc_type => 'marc21',
885
            marc_field => '650a',
886
        }
887
    );
888
889
    my $se = Test::MockModule->new('Koha::SearchEngine::Elasticsearch');
890
    $se->mock('_foreach_mapping', sub {
891
        my ($self, $sub) = @_;
892
893
        foreach my $map (@mappings) {
894
            $sub->(
895
                $map->{name},
896
                $map->{type},
897
                $map->{facet},
898
                $map->{suggestible},
899
                $map->{sort},
900
                $map->{searchable},
901
                $map->{marc_type},
902
                $map->{marc_field}
903
            );
904
        }
905
    });
906
907
    my $see = Koha::SearchEngine::Elasticsearch::Search->new({ index => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX });
908
909
    my $marc_record_1 = MARC::Record->new();
910
    $marc_record_1->leader('     cam  22      a 4500');
911
    $marc_record_1->append_fields(
912
        MARC::Field->new('001', '123'),
913
        MARC::Field->new('245', '', '', a => 'Title'),
914
        MARC::Field->new('650', '', '', a => 'Foo', 9 => $authid),
915
        MARC::Field->new('999', '', '', c => '1234567'),
916
    );
917
918
    # sort_fields will call this and use the actual db values unless we call it first
919
    $see->get_elasticsearch_mappings();
920
921
    my $docs = $see->marc_records_to_documents([$marc_record_1]);
922
923
    is_deeply($docs->[0]->{subject}, ['Foo', 'Bar'], 'subject should include "See from"');
924
    is_deeply($docs->[0]->{subject__facet}, ['Foo'], 'subject__facet should not include "See from"');
925
    is_deeply($docs->[0]->{subject__suggestion}, [{ input => 'Foo' }], 'subject__suggestion should not include "See from"');
926
    is_deeply($docs->[0]->{subject__sort}, ['Foo'], 'subject__sort should not include "See from"');
927
};
928
853
$schema->storage->txn_rollback;
929
$schema->storage->txn_rollback;
854
- 

Return to bug 25030