FRBR: added OPAC search result grouping option
authorGalen Charlton <galen.charlton@liblime.com>
Mon, 11 Feb 2008 22:21:48 +0000 (16:21 -0600)
committerJoshua Ferraro <jmf@liblime.com>
Mon, 11 Feb 2008 22:35:17 +0000 (16:35 -0600)
Because this feature is experimental, the syspref will
not be added to the list just yet.

Signed-off-by: Joshua Ferraro <jmf@liblime.com>
C4/Search.pm
C4/Search/PazPar2.pm
opac/opac-search.pl

index e586ebf..a7d8ecf 100644 (file)
@@ -623,7 +623,6 @@ sub pazGetRecords {
 
     my $paz = C4::Search::PazPar2->new('http://localhost:10006/search.pz2');
     $paz->init();
-    #die $simple_query;
     $paz->search($simple_query);
     sleep 1;
 
@@ -631,31 +630,36 @@ sub pazGetRecords {
     my $results_hashref = {};
     my $stats = XMLin($paz->stat);
     $results_hashref->{'biblioserver'}->{'hits'} = $stats->{'hits'};
-    my $results = XMLin($paz->show($offset, $results_per_page), forcearray => 1);
-    #die Dumper($results);
+    my $results = XMLin($paz->show($offset, $results_per_page, 'work-title:1'), forcearray => 1);
+    
     HIT: foreach my $hit (@{ $results->{'hit'} }) {
-        warn "hit";
         my $recid = $hit->{recid}->[0];
+
         my $work_title = $hit->{'md-work-title'}->[0];
-        #if ($recid =~ /[\200-\377]/) {
-        if ($recid =~ /sodot/) {
-            #die "bad $recid\n";
-            #probably do not want non-ASCII in record ID
-            last HIT;
+        my $work_author;
+        if (exists $hit->{'md-work-author'}) {
+            $work_author = $hit->{'md-work-author'}->[0];
         }
+        my $group_label = (defined $work_author) ? "$work_title / $work_author" : $work_title;
+
+        my $result_group = {};
+        $result_group->{'group_label'} = $group_label;
+        $result_group->{'group_merge_key'} = $recid;
+
         my $count = 1;
         if (exists $hit->{count}) {
             $count = $hit->{count}->[0];
         }
-        #die $count;
+        $result_group->{'group_count'} = $count;
+
         for (my $i = 0; $i < $count; $i++) {
-            warn "look for $recid offset = $i";
+            # FIXME -- may need to worry about diacritics here
             my $rec = $paz->record($recid, $i);
-            warn "got record $i";
-            push @{ $results_hashref->{'biblioserver'}->{$work_title}->{'RECORDS'} }, $paz->record($recid, $i);
+            push @{ $result_group->{'RECORDS'} }, $paz->record($recid, $i);
         }
+
+        push @{ $results_hashref->{'biblioserver'}->{'GROUPS'} }, $result_group;
     }
-    warn "past hits";
     
     # pass through facets
     my $termlist_xml = $paz->termlist('author,subject');
index f543c96..52191f0 100644 (file)
@@ -102,6 +102,7 @@ sub show {
     my $self = shift;
     my $start = shift;
     my $count = shift;
+    my $sort = shift;
 
     my $uri = URI->new($self->{'endpoint'});
     $uri->query_param(command => 'show');
@@ -109,6 +110,7 @@ sub show {
     $uri->query_param(num => $count);
     $uri->query_param(block => 1);
     $uri->query_param(session => $self->{'session'});
+    $uri->query_param(sort => $sort);
     my $response = $self->{'ua'}->get($uri);
     if ($response->is_success) {
         return $response->content;
index 30dc8a1..a3d326f 100755 (executable)
@@ -26,7 +26,12 @@ my ($template,$borrowernumber,$cookie);
 my $template_name;
 my $template_type;
 my @params = $cgi->param("limit");
-if ((@params>=1) || ($cgi->param("q")) || ($cgi->param('multibranchlimit')) || ($cgi->param('limit-yr')) ) {
+
+my $build_grouped_results = C4::Context->preference('OPACGroupResults');
+if ($build_grouped_results) {
+    $template_name = 'opac-results-grouped.tmpl';
+} 
+elsif ((@params>=1) || ($cgi->param("q")) || ($cgi->param('multibranchlimit')) || ($cgi->param('limit-yr')) ) {
     $template_name = 'opac-results.tmpl';
 }
 else {
@@ -304,10 +309,14 @@ if (C4::Context->preference('NoZebra')) {
     eval {
         ($error, $results_hashref, $facets) = NZgetRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$query_type,$scan);
     };
-} else {
+} elsif ($build_grouped_results) {
     eval {
         ($error, $results_hashref, $facets) = C4::Search::pazGetRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$query_type,$scan);
     };
+} else {
+    eval {
+        ($error, $results_hashref, $facets) = getRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$query_type,$scan);
+    };
 }
 if ($@ || $error) {
     $template->param(query_error => $error.$@);
@@ -324,9 +333,14 @@ for (my $i=0;$i<=@servers;$i++) {
         $hits = $results_hashref->{$server}->{"hits"};
         my $page = $cgi->param('page') || 0;
         my @newresults;
-        for my $work_title (keys %{ $results_hashref->{$server} })  {
-            next if $work_title eq "hits";
-           push @newresults, searchResults( $query_desc,$hits,$results_per_page,$offset,@{$results_hashref->{$server}->{$work_title}->{"RECORDS"}});
+        if ($build_grouped_results) {
+            foreach my $group (@{ $results_hashref->{$server}->{"GROUPS"} }) {
+                my @group_results = searchResults( $query_desc, $group->{'group_count'},$results_per_page,$offset, 
+                                                   @{ $group->{"RECORDS"} });
+                push @newresults, { group_label => $group->{'group_label'}, GROUP_RESULTS => \@group_results };
+            }
+        } else {
+            @newresults = searchResults( $query_desc,$hits,$results_per_page,$offset,@{$results_hashref->{$server}->{"RECORDS"}});
         }
         $total = $total + $results_hashref->{$server}->{"hits"};
         if ($hits) {