1 package Koha::SearchEngine::Solr::Index;
3 with 'Koha::SearchEngine::IndexRole';
5 use Data::SearchEngine::Solr;
6 use Data::Dump qw(dump);
7 use List::MoreUtils qw(uniq);
9 use Koha::SearchEngine::Solr;
10 use C4::AuthoritiesMarc;
12 use Koha::RecordProcessor;
16 isa => 'Koha::SearchEngine::Solr',
17 default => sub { Koha::SearchEngine::Solr->new },
23 return $self->searchengine->_solr->optimize;
27 my ($self, $recordtype, $recordids) = @_;
29 my $indexes = $self->searchengine->config->indexes;
32 my $recordids_str = ref($recordids) eq 'ARRAY'
33 ? join " ", @$recordids
35 warn "IndexRecord called with $recordtype $recordids_str";
37 for my $id ( @$recordids ) {
40 $record = GetAuthority( $id ) if $recordtype eq "authority";
41 $record = GetMarcBiblio( $id ) if $recordtype eq "biblio";
43 if ($record_type eq 'biblio' && C4::Context->preference('IncludeSeeFromInSearches')) {
44 my $normalizer = Koha::RecordProcessor->new( { filters => 'EmbedSeeFromHeadings' } );
45 $record = $normalizer->process($record);
48 next unless ( $record );
52 recordtype => $recordtype,
55 warn "Indexing $recordtype $id";
57 for my $index ( @$indexes ) {
58 next if $index->{ressource_type} ne $recordtype;
61 my $mappings = $index->{mappings};
62 for my $tag_subf_code ( sort @$mappings ) {
63 my ( $f, $sf ) = split /\$/, $tag_subf_code;
64 for my $field ( $record->field( $f ) ) {
65 if ( $field->is_control_field ) {
66 push @values, $field->data;
68 my @sfvals = $sf eq '*'
69 ? map { $_->[1] } $field->subfields
70 : map { $_ } $field->subfield( $sf );
73 $_ = NormalizeDate( $_ ) if $index->{type} eq 'date';
74 push @values, $_ if $_;
79 @values = uniq (@values); #Removes duplicates
81 $index_values->{$index->{type}."_".$index->{code}} = \@values;
82 if ( $index->{sortable} ){
83 $index_values->{"srt_" . $index->{type} . "_".$index->{code}} = $values[0];
85 # Add index str for facets if it's not exist
86 if ( $index->{facetable} and @values > 0 and $index->{type} ne 'str' ) {
87 $index_values->{"str_" . $index->{code}} = $values[0];
92 warn "Error during indexation : recordid $id, index $index->{code} ( $@ )";
96 my $solrrecord = Data::SearchEngine::Item->new(
97 id => "${recordtype}_$id",
99 values => $index_values,
101 push @records, $solrrecord;
103 $self->searchengine->add( \@records );