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

(-)a/Koha/Filter/MARC/EmbedSeeFromHeadings.pm (-6 / +14 lines)
Lines 59-80 sub filter { Link Here
59
    if (ref $record eq 'ARRAY') {
59
    if (ref $record eq 'ARRAY') {
60
        my @recarray;
60
        my @recarray;
61
        foreach my $thisrec (@$record) {
61
        foreach my $thisrec (@$record) {
62
            push @recarray, _processrecord($thisrec);
62
            push @recarray, $self->_processrecord($thisrec);
63
        }
63
        }
64
        $newrecord = \@recarray;
64
        $newrecord = \@recarray;
65
    } elsif (ref $record eq 'MARC::Record') {
65
    } elsif (ref $record eq 'MARC::Record') {
66
        $newrecord = _processrecord($record);
66
        $newrecord = $self->_processrecord($record);
67
    }
67
    }
68
68
69
    return $newrecord;
69
    return $newrecord;
70
}
70
}
71
71
72
sub _processrecord {
72
sub _processrecord {
73
    my $record = shift;
73
    my ($self, $record) = @_;
74
75
    $record->append_fields($self->fields($record));
76
77
    return $record;
78
}
79
80
sub fields {
81
    my ($self, $record) = @_;
74
82
75
    my ($item_tag) = GetMarcFromKohaField( "items.itemnumber" );
83
    my ($item_tag) = GetMarcFromKohaField( "items.itemnumber" );
76
    $item_tag ||= '';
84
    $item_tag ||= '';
77
85
86
    my @newfields;
78
    foreach my $field ( $record->fields() ) {
87
    foreach my $field ( $record->fields() ) {
79
        next if $field->is_control_field();
88
        next if $field->is_control_field();
80
        next if $field->tag() eq $item_tag;
89
        next if $field->tag() eq $item_tag;
Lines 86-92 sub _processrecord { Link Here
86
        next unless $authority;
95
        next unless $authority;
87
        my $auth_marc = $authority->record;
96
        my $auth_marc = $authority->record;
88
        my @seefrom = $auth_marc->field('4..');
97
        my @seefrom = $auth_marc->field('4..');
89
        my @newfields;
90
        foreach my $authfield (@seefrom) {
98
        foreach my $authfield (@seefrom) {
91
            my $tag = substr($field->tag(), 0, 1) . substr($authfield->tag(), 1, 2);
99
            my $tag = substr($field->tag(), 0, 1) . substr($authfield->tag(), 1, 2);
92
            next if MARC::Field->is_controlfield_tag($tag);
100
            next if MARC::Field->is_controlfield_tag($tag);
Lines 101-107 sub _processrecord { Link Here
101
            $newfield->delete_subfield( code => '9' );
109
            $newfield->delete_subfield( code => '9' );
102
            push @newfields, $newfield if (scalar($newfield->subfields()) > 0);
110
            push @newfields, $newfield if (scalar($newfield->subfields()) > 0);
103
        }
111
        }
104
        $record->append_fields(@newfields);
105
    }
112
    }
106
    return $record;
113
114
    return @newfields;
107
}
115
}
(-)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 640-645 sub marc_records_to_documents { Link Here
640
                }
641
                }
641
            }
642
            }
642
        }
643
        }
644
645
        if (C4::Context->preference('IncludeSeeFromInSearches') and $self->index eq 'biblios') {
646
            foreach my $field (Koha::Filter::MARC::EmbedSeeFromHeadings->new->fields($record)) {
647
                my $data_field_rules = $data_fields_rules->{$field->tag()};
648
                if ($data_field_rules) {
649
                    my $subfields_mappings = $data_field_rules->{subfields};
650
                    my $wildcard_mappings = $subfields_mappings->{'*'};
651
                    foreach my $subfield ($field->subfields()) {
652
                        my ($code, $data) = @{$subfield};
653
                        my @mappings;
654
                        push @mappings, @{ $subfields_mappings->{$code} } if $subfields_mappings->{$code};
655
                        push @mappings, @$wildcard_mappings if $wildcard_mappings;
656
                        # Do not include "see from" into these kind of fields
657
                        @mappings = grep { $_->[0] !~ /__(sort|facet|suggestion)$/ } @mappings;
658
                        if (@mappings) {
659
                            $self->_process_mappings(\@mappings, $data, $record_document, {
660
                                    data_source => 'subfield',
661
                                    code => $code,
662
                                    field => $field
663
                                }
664
                            );
665
                        }
666
                    }
667
668
                    my $subfields_join_mappings = $data_field_rules->{subfields_join};
669
                    if ($subfields_join_mappings) {
670
                        foreach my $subfields_group (keys %{$subfields_join_mappings}) {
671
                            my $data_field = $field->clone;
672
                            # remove empty subfields, otherwise they are printed as a space
673
                            $data_field->delete_subfield(match => qr/^$/);
674
                            my $data = $data_field->as_string( $subfields_group );
675
                            if ($data) {
676
                                my @mappings = @{ $subfields_join_mappings->{$subfields_group} };
677
                                # Do not include "see from" into these kind of fields
678
                                @mappings = grep { $_->[0] !~ /__(sort|facet|suggestion)$/ } @mappings;
679
                                $self->_process_mappings(\@mappings, $data, $record_document, {
680
                                        data_source => 'subfields_group',
681
                                        codes => $subfields_group,
682
                                        field => $field
683
                                    }
684
                                );
685
                            }
686
                        }
687
                    }
688
                }
689
            }
690
        }
691
643
        foreach my $field (keys %{$rules->{defaults}}) {
692
        foreach my $field (keys %{$rules->{defaults}}) {
644
            unless (defined $record_document->{$field}) {
693
            unless (defined $record_document->{$field}) {
645
                $record_document->{$field} = $rules->{defaults}->{$field};
694
                $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