+=head2 _aggregation_scan
+
+ my $result = $self->_aggregration_scan($query, 10, 0);
+
+Perform an aggregation request for scan purposes.
+
+=cut
+
+sub _aggregation_scan {
+ my ($self, $query, $results_per_page, $offset) = @_;
+
+ if (!scalar(keys %{$query->{aggregations}})) {
+ my %result = {
+ biblioserver => {
+ hits => 0,
+ RECORDS => undef
+ }
+ };
+ return (undef, \%result, undef);
+ }
+ my ($field) = keys %{$query->{aggregations}};
+ $query->{aggregations}{$field}{terms}{size} = 1000;
+ my $results = $self->search($query, 1, 0);
+
+ # Convert each result into a MARC::Record
+ my (@records, $index);
+ # opac-search expects results to be put in the
+ # right place in the array, according to $offset
+ $index = $offset - 1;
+
+ my $count = scalar(@{$results->{aggregations}{$field}{buckets}});
+ for (my $index = $offset; $index - $offset < $results_per_page && $index < $count; $index++) {
+ my $bucket = $results->{aggregations}{$field}{buckets}->[$index];
+ # Scan values are expressed as:
+ # - MARC21: 100a (count) and 245a (term)
+ # - UNIMARC: 200f (count) and 200a (term)
+ my $marc = MARC::Record->new;
+ $marc->encoding('UTF-8');
+ if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
+ $marc->append_fields(
+ MARC::Field->new((200, ' ', ' ', 'f' => $bucket->{doc_count}))
+ );
+ $marc->append_fields(
+ MARC::Field->new((200, ' ', ' ', 'a' => $bucket->{key}))
+ );
+ } else {
+ $marc->append_fields(
+ MARC::Field->new((100, ' ', ' ', 'a' => $bucket->{doc_count}))
+ );
+ $marc->append_fields(
+ MARC::Field->new((245, ' ', ' ', 'a' => $bucket->{key}))
+ );
+ }
+ $records[$index] = $marc->as_usmarc();
+ };
+ # consumers of this expect a namespaced result, we provide the default
+ # configuration.
+ my %result;
+ $result{biblioserver}{hits} = $count;
+ $result{biblioserver}{RECORDS} = \@records;
+ return (undef, \%result, undef);
+}