1 package Koha::SearchEngine::Solr::Index;
3 # This file is part of Koha.
5 # Copyright 2012 BibLibre
6 # Copyright 2012 C & P Bibliography Services
7 # Copyright 2012 PTFS-Europe Ltd.
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.
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.
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>.
23 with 'Koha::SearchEngine::IndexRole';
25 use Data::SearchEngine::Solr;
26 use Data::Dump qw(dump);
27 use List::MoreUtils qw(uniq);
29 use Koha::SearchEngine::Solr;
30 use C4::AuthoritiesMarc;
32 use Koha::RecordProcessor;
36 isa => 'Koha::SearchEngine::Solr',
37 default => sub { Koha::SearchEngine::Solr->new },
43 return $self->searchengine->_solr->optimize;
47 my ($self, $recordtype, $recordids) = @_;
49 my $indexes = $self->searchengine->config->indexes;
52 my $recordids_str = ref($recordids) eq 'ARRAY'
53 ? join " ", @$recordids
55 warn "IndexRecord called with $recordtype $recordids_str";
57 for my $id ( @$recordids ) {
60 $record = GetAuthority( $id ) if $recordtype eq "authority";
61 $record = GetMarcBiblio( $id ) if $recordtype eq "biblio";
63 if ($recordtype eq 'biblio' && C4::Context->preference('IncludeSeeFromInSearches')) {
64 my $normalizer = Koha::RecordProcessor->new( { filters => 'EmbedSeeFromHeadings' } );
65 $record = $normalizer->process($record);
68 next unless ( $record );
72 recordtype => $recordtype,
75 warn "Indexing $recordtype $id";
77 for my $index ( @$indexes ) {
78 next if $index->{ressource_type} ne $recordtype;
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;
88 my @sfvals = $sf eq '*'
89 ? map { $_->[1] } $field->subfields
90 : map { $_ } $field->subfield( $sf );
93 $_ = NormalizeDate( $_ ) if $index->{type} eq 'date';
94 push @values, $_ if $_;
99 @values = uniq (@values); #Removes duplicates
101 $index_values->{$index->{type}."_".$index->{code}} = \@values;
102 if ( $index->{sortable} ){
103 $index_values->{"srt_" . $index->{type} . "_".$index->{code}} = $values[0];
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];
112 warn "Error during indexation : recordid $id, index $index->{code} ( $@ )";
116 my $solrrecord = Data::SearchEngine::Item->new(
117 id => "${recordtype}_$id",
119 values => $index_values,
121 push @records, $solrrecord;
123 $self->searchengine->add( \@records );