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 637-642 sub marc_records_to_documents { Link Here
637
                }
638
                }
638
            }
639
            }
639
        }
640
        }
641
642
        if (C4::Context->preference('IncludeSeeFromInSearches') and $self->index eq 'biblios') {
643
            foreach my $field (Koha::Filter::MARC::EmbedSeeFromHeadings->new->fields($record)) {
644
                my $data_field_rules = $data_fields_rules->{$field->tag()};
645
                if ($data_field_rules) {
646
                    my $subfields_mappings = $data_field_rules->{subfields};
647
                    my $wildcard_mappings = $subfields_mappings->{'*'};
648
                    foreach my $subfield ($field->subfields()) {
649
                        my ($code, $data) = @{$subfield};
650
                        my @mappings;
651
                        push @mappings, @{ $subfields_mappings->{$code} } if $subfields_mappings->{$code};
652
                        push @mappings, @$wildcard_mappings if $wildcard_mappings;
653
                        # Do not include "see from" into these kind of fields
654
                        @mappings = grep { $_->[0] !~ /__(sort|facet|suggestion)$/ } @mappings;
655
                        if (@mappings) {
656
                            $self->_process_mappings(\@mappings, $data, $record_document, {
657
                                    data_source => 'subfield',
658
                                    code => $code,
659
                                    field => $field
660
                                }
661
                            );
662
                        }
663
                    }
664
665
                    my $subfields_join_mappings = $data_field_rules->{subfields_join};
666
                    if ($subfields_join_mappings) {
667
                        foreach my $subfields_group (keys %{$subfields_join_mappings}) {
668
                            my $data_field = $field->clone;
669
                            # remove empty subfields, otherwise they are printed as a space
670
                            $data_field->delete_subfield(match => qr/^$/);
671
                            my $data = $data_field->as_string( $subfields_group );
672
                            if ($data) {
673
                                my @mappings = @{ $subfields_join_mappings->{$subfields_group} };
674
                                # Do not include "see from" into these kind of fields
675
                                @mappings = grep { $_->[0] !~ /__(sort|facet|suggestion)$/ } @mappings;
676
                                $self->_process_mappings(\@mappings, $data, $record_document, {
677
                                        data_source => 'subfields_group',
678
                                        codes => $subfields_group,
679
                                        field => $field
680
                                    }
681
                                );
682
                            }
683
                        }
684
                    }
685
                }
686
            }
687
        }
688
640
        foreach my $field (keys %{$rules->{defaults}}) {
689
        foreach my $field (keys %{$rules->{defaults}}) {
641
            unless (defined $record_document->{$field}) {
690
            unless (defined $record_document->{$field}) {
642
                $record_document->{$field} = $rules->{defaults}->{$field};
691
                $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 829-832 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () authori Link Here
829
    );
831
    );
830
};
832
};
831
833
834
subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents with IncludeSeeFromInSearches' => sub {
835
836
    plan tests => 4;
837
838
    t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
839
    t::lib::Mocks::mock_preference('IncludeSeeFromInSearches', '1');
840
841
    my $builder = t::lib::TestBuilder->new;
842
    my $auth_type = $builder->build_object({
843
        class => 'Koha::Authority::Types',
844
        value => {
845
            auth_tag_to_report => '150'
846
        }
847
    });
848
    my $authority_record = MARC::Record->new();
849
    $authority_record->append_fields(
850
        MARC::Field->new(150, '', '', a => 'Foo'),
851
        MARC::Field->new(450, '', '', a => 'Bar'),
852
    );
853
    my $authid = AddAuthority($authority_record, undef, $auth_type->authtypecode);
854
855
    my @mappings = (
856
        {
857
            name => 'subject',
858
            type => 'string',
859
            facet => 1,
860
            suggestible => 1,
861
            sort => undef,
862
            searchable => 1,
863
            marc_type => 'marc21',
864
            marc_field => '650a',
865
        }
866
    );
867
868
    my $se = Test::MockModule->new('Koha::SearchEngine::Elasticsearch');
869
    $se->mock('_foreach_mapping', sub {
870
        my ($self, $sub) = @_;
871
872
        foreach my $map (@mappings) {
873
            $sub->(
874
                $map->{name},
875
                $map->{type},
876
                $map->{facet},
877
                $map->{suggestible},
878
                $map->{sort},
879
                $map->{searchable},
880
                $map->{marc_type},
881
                $map->{marc_field}
882
            );
883
        }
884
    });
885
886
    my $see = Koha::SearchEngine::Elasticsearch::Search->new({ index => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX });
887
888
    my $marc_record_1 = MARC::Record->new();
889
    $marc_record_1->leader('     cam  22      a 4500');
890
    $marc_record_1->append_fields(
891
        MARC::Field->new('001', '123'),
892
        MARC::Field->new('245', '', '', a => 'Title'),
893
        MARC::Field->new('650', '', '', a => 'Foo', 9 => $authid),
894
        MARC::Field->new('999', '', '', c => '1234567'),
895
    );
896
897
    # sort_fields will call this and use the actual db values unless we call it first
898
    $see->get_elasticsearch_mappings();
899
900
    my $docs = $see->marc_records_to_documents([$marc_record_1]);
901
902
    is_deeply($docs->[0]->{subject}, ['Foo', 'Bar'], 'subject should include "See from"');
903
    is_deeply($docs->[0]->{subject__facet}, ['Foo'], 'subject__facet should not include "See from"');
904
    is_deeply($docs->[0]->{subject__suggestion}, [{ input => 'Foo' }], 'subject__suggestion should not include "See from"');
905
    is_deeply($docs->[0]->{subject__sort}, ['Foo'], 'subject__sort should not include "See from"');
906
};
907
832
$schema->storage->txn_rollback;
908
$schema->storage->txn_rollback;
833
- 

Return to bug 25030