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

(-)a/C4/Search.pm (-41 / +63 lines)
Lines 340-347 sub getRecords { Link Here
340
    my $results_hashref = ();
340
    my $results_hashref = ();
341
341
342
    # Initialize variables for the faceted results objects
342
    # Initialize variables for the faceted results objects
343
    my $facets_counter = ();
343
    my $facets_counter = {};
344
    my $facets_info    = ();
344
    my $facets_info    = {};
345
    my $facets         = getFacets();
345
    my $facets         = getFacets();
346
    my $facets_maxrecs = C4::Context->preference('maxRecordsForFacets')||20;
346
    my $facets_maxrecs = C4::Context->preference('maxRecordsForFacets')||20;
347
347
Lines 499-549 sub getRecords { Link Here
499
                }
499
                }
500
                $results_hashref->{ $servers[ $i - 1 ] } = $results_hash;
500
                $results_hashref->{ $servers[ $i - 1 ] } = $results_hash;
501
501
502
# Fill the facets while we're looping, but only for the biblioserver and not for a scan
502
                # Fill the facets while we're looping, but only for the
503
                # biblioserver and not for a scan
503
                if ( !$scan && $servers[ $i - 1 ] =~ /biblioserver/ ) {
504
                if ( !$scan && $servers[ $i - 1 ] =~ /biblioserver/ ) {
504
505
505
                    my $jmax =
506
                    my $jmax = $size > $facets_maxrecs
506
                      $size > $facets_maxrecs ? $facets_maxrecs : $size;
507
                                ? $facets_maxrecs
507
                    for my $facet (@$facets) {
508
                                : $size;
508
                        for ( my $j = 0 ; $j < $jmax ; $j++ ) {
509
509
510
                            my $marc_record = new_record_from_zebra (
510
                    for ( my $j = 0 ; $j < $jmax ; $j++ ) {
511
                                    'biblioserver',
512
                                    $results[ $i - 1 ]->record($j)->raw()
513
                            );
514
515
                            if ( ! defined $marc_record ) {
516
                                warn "ERROR DECODING RECORD - $@: " .
517
                                    $results[ $i - 1 ]->record($j)->raw();
518
                                next;
519
                            }
520
521
                            my @used_datas = ();
522
511
523
                            foreach my $tag ( @{ $facet->{tags} } ) {
512
                        my $marc_record = new_record_from_zebra (
513
                                'biblioserver',
514
                                $results[ $i - 1 ]->record($j)->raw()
515
                        );
524
516
525
                                # avoid first line
517
                        if ( ! defined $marc_record ) {
526
                                my $tag_num = substr( $tag, 0, 3 );
518
                            warn "ERROR DECODING RECORD - $@: " .
527
                                my $subfield_letters = substr( $tag, 3 );
519
                                $results[ $i - 1 ]->record($j)->raw();
528
                                # Removed when as_string fixed
520
                            next;
529
                                my @subfields = $subfield_letters =~ /./sg;
521
                        }
530
531
                                my @fields = $marc_record->field($tag_num);
532
                                foreach my $field (@fields) {
533
                                    my $data = $field->as_string( $subfield_letters, $facet->{sep} );
534
522
535
                                    unless ( grep { /^\Q$data\E$/ } @used_datas ) {
523
                        _get_facets_data_from_record( $marc_record, $facets, $facets_counter, $facets_info );
536
                                        push @used_datas, $data;
524
                    }
537
                                        $facets_counter->{ $facet->{idx} }->{$data}++;
538
                                    }
539
                                } # fields
540
                            }    # field codes
541
                        }    # records
542
                        $facets_info->{ $facet->{idx} }->{label_value} =
543
                          $facet->{label};
544
                        $facets_info->{ $facet->{idx} }->{expanded} =
545
                          $facet->{expanded};
546
                    }    # facets
547
                }
525
                }
548
526
549
                # warn "connection ", $i-1, ": $size hits";
527
                # warn "connection ", $i-1, ": $size hits";
Lines 673-678 sub getRecords { Link Here
673
    return ( undef, $results_hashref, \@facets_loop );
651
    return ( undef, $results_hashref, \@facets_loop );
674
}
652
}
675
653
654
=head2 _get_facets_data_from_record
655
656
    C4::Search::_get_facets_data_from_record( $marc_record, $facets, $facets_counter );
657
658
Internal function that extracts facets information from a MARC::Record object
659
and populates $facets_counter for using in getRecords.
660
661
$facets is expected to be filled with C4::Koha::getFacets output (i.e. the configured
662
facets for Zebra).
663
664
=cut
665
666
sub _get_facets_data_from_record {
667
668
    my ( $marc_record, $facets, $facets_counter, $facets_info ) = @_;
669
670
    for my $facet (@$facets) {
671
672
        my @used_datas = ();
673
674
        foreach my $tag ( @{ $facet->{ tags } } ) {
675
676
            # avoid first line
677
            my $tag_num          = substr( $tag, 0, 3 );
678
            my $subfield_letters = substr( $tag, 3 );
679
            # Removed when as_string fixed
680
            my @subfields = $subfield_letters =~ /./sg;
681
682
            my @fields = $marc_record->field( $tag_num );
683
            foreach my $field (@fields) {
684
                my $data = $field->as_string( $subfield_letters, $facet->{ sep } );
685
686
                unless ( grep { /^\Q$data\E$/ } @used_datas ) {
687
                    push @used_datas, $data;
688
                    $facets_counter->{ $facet->{ idx } }->{ $data }++;
689
                }
690
            }
691
        }
692
        # update $facets_info so we know what facet categories need to be rendered
693
        $facets_info->{ $facet->{ idx } }->{ label_value } = $facet->{ label };
694
        $facets_info->{ $facet->{ idx } }->{ expanded }    = $facet->{ expanded };
695
    }
696
}
697
676
sub pazGetRecords {
698
sub pazGetRecords {
677
    my (
699
    my (
678
        $koha_query,       $simple_query, $sort_by_ref,    $servers_ref,
700
        $koha_query,       $simple_query, $sort_by_ref,    $servers_ref,
(-)a/t/db_dependent/Search.t (-3 / +28 lines)
Lines 857-862 if ( $indexing_mode eq 'dom' ) { Link Here
857
        getRecords('ccl=( AND )', '', ['title_az'], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, 'ccl', undef)
857
        getRecords('ccl=( AND )', '', ['title_az'], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, 'ccl', undef)
858
    } qr/WARNING: query problem with/, 'got warning instead of crash when attempting to run invalid query (bug 9578)';
858
    } qr/WARNING: query problem with/, 'got warning instead of crash when attempting to run invalid query (bug 9578)';
859
    
859
    
860
    # Test facet calculation
861
    my $facets_counter = {};
862
    my $facets_info    = {};
863
    my $facets         = C4::Koha::getFacets();
864
    # Create a record with a 100$z field
865
    my $marc_record    = MARC::Record->new;
866
    $marc_record->add_fields(
867
        [ '001', '1234' ],
868
        [ '100', ' ', ' ', a => 'Cohen Arazi, Tomas' ],
869
        [ '100', 'z', ' ', a => 'Tomasito' ],
870
        [ '245', ' ', ' ', a => 'First try' ]
871
    );
872
    C4::Search::_get_facets_data_from_record($marc_record, $facets, $facets_counter,$facets_info);
873
    is_deeply( { au => { 'Cohen Arazi, Tomas' => 1 } },  $facets_counter,
874
        "_get_facets_data_from_record doesn't count 100\$z (Bug 12788)");
875
    $marc_record    = MARC::Record->new;
876
    $marc_record->add_fields(
877
        [ '001', '1234' ],
878
        [ '100', ' ', ' ', a => 'Cohen Arazi, Tomas' ],
879
        [ '100', 'z', ' ', a => 'Tomasito' ],
880
        [ '245', ' ', ' ', a => 'Second try' ]
881
    );
882
    C4::Search::_get_facets_data_from_record($marc_record, $facets, $facets_counter, $facets_info);
883
    is_deeply( { au => { 'Cohen Arazi, Tomas' => 2 } },  $facets_counter,
884
        "_get_facets_data_from_record correctly counts author facet twice");
885
860
    cleanup();
886
    cleanup();
861
}
887
}
862
888
Lines 932-943 sub run_unimarc_search_tests { Link Here
932
}
958
}
933
959
934
subtest 'MARC21 + GRS-1' => sub {
960
subtest 'MARC21 + GRS-1' => sub {
935
    plan tests => 106;
961
    plan tests => 108;
936
    run_marc21_search_tests('grs1');
962
    run_marc21_search_tests('grs1');
937
};
963
};
938
964
939
subtest 'MARC21 + DOM' => sub {
965
subtest 'MARC21 + DOM' => sub {
940
    plan tests => 106;
966
    plan tests => 108;
941
    run_marc21_search_tests('dom');
967
    run_marc21_search_tests('dom');
942
};
968
};
943
969
944
- 

Return to bug 12788