Bug 11015: add copyright headers to some files
[koha.git] / Koha / SearchEngine / Solr / Index.pm
1 package Koha::SearchEngine::Solr::Index;
2
3 # This file is part of Koha.
4 #
5 # Copyright 2012 BibLibre
6 # Copyright 2012 C & P Bibliography Services
7 # Copyright 2012 PTFS-Europe Ltd.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 use Moose::Role;
23 with 'Koha::SearchEngine::IndexRole';
24
25 use Data::SearchEngine::Solr;
26 use Data::Dump qw(dump);
27 use List::MoreUtils qw(uniq);
28
29 use Koha::SearchEngine::Solr;
30 use C4::AuthoritiesMarc;
31 use C4::Biblio;
32 use Koha::RecordProcessor;
33
34 has searchengine => (
35     is => 'rw',
36     isa => 'Koha::SearchEngine::Solr',
37     default => sub { Koha::SearchEngine::Solr->new },
38     lazy => 1
39 );
40
41 sub optimize {
42     my ( $self ) = @_;
43     return $self->searchengine->_solr->optimize;
44 }
45
46 sub index_record {
47     my ($self, $recordtype, $recordids) = @_;
48
49     my $indexes = $self->searchengine->config->indexes;
50     my @records;
51
52     my $recordids_str = ref($recordids) eq 'ARRAY'
53                     ? join " ", @$recordids
54                     : $recordids;
55     warn "IndexRecord called with $recordtype $recordids_str";
56
57     for my $id ( @$recordids ) {
58         my $record;
59
60         $record = GetAuthority( $id )  if $recordtype eq "authority";
61         $record = GetMarcBiblio( $id ) if $recordtype eq "biblio";
62
63         if ($recordtype eq 'biblio' && C4::Context->preference('IncludeSeeFromInSearches')) {
64             my $normalizer = Koha::RecordProcessor->new( { filters => 'EmbedSeeFromHeadings' } );
65             $record = $normalizer->process($record);
66         }
67
68         next unless ( $record );
69
70         my $index_values = {
71             recordid => $id,
72             recordtype => $recordtype,
73         };
74
75         warn "Indexing $recordtype $id";
76
77         for my $index ( @$indexes ) {
78             next if $index->{ressource_type} ne $recordtype;
79             my @values;
80             eval {
81                 my $mappings = $index->{mappings};
82                 for my $tag_subf_code ( sort @$mappings ) {
83                     my ( $f, $sf ) = split /\$/, $tag_subf_code;
84                     for my $field ( $record->field( $f ) ) {
85                         if ( $field->is_control_field ) {
86                             push @values, $field->data;
87                         } else {
88                             my @sfvals = $sf eq '*'
89                                        ? map { $_->[1] } $field->subfields
90                                        : map { $_      } $field->subfield( $sf );
91
92                             for ( @sfvals ) {
93                                 $_ = NormalizeDate( $_ ) if $index->{type} eq 'date';
94                                 push @values, $_ if $_;
95                             }
96                         }
97                     }
98                 }
99                 @values = uniq (@values); #Removes duplicates
100
101                 $index_values->{$index->{type}."_".$index->{code}} = \@values;
102                 if ( $index->{sortable} ){
103                     $index_values->{"srt_" . $index->{type} . "_".$index->{code}} = $values[0];
104                 }
105                 # Add index str for facets if it's not exist
106                 if ( $index->{facetable} and @values > 0 and $index->{type} ne 'str' ) {
107                     $index_values->{"str_" . $index->{code}} = $values[0];
108                 }
109             };
110             if ( $@ ) {
111                 chomp $@;
112                 warn  "Error during indexation : recordid $id, index $index->{code} ( $@ )";
113             }
114         }
115
116         my $solrrecord = Data::SearchEngine::Item->new(
117             id    => "${recordtype}_$id",
118             score => 1,
119             values => $index_values,
120         );
121         push @records, $solrrecord;
122     }
123     $self->searchengine->add( \@records );
124 }
125
126 1;