@@ -, +, @@ indexing --- Koha/SearchEngine/Elasticsearch.pm | 38 +++++++++++++++++++++++++++++-------- t/Koha/SearchEngine/Elasticsearch.t | 4 ++-- 2 files changed, 32 insertions(+), 10 deletions(-) --- a/Koha/SearchEngine/Elasticsearch.pm +++ a/Koha/SearchEngine/Elasticsearch.pm @@ -312,7 +312,7 @@ sub marc_records_to_documents { my @record_documents; sub _process_mappings { - my ($mappings, $data, $record_document) = @_; + my ($mappings, $data, $record_document, $altscript) = @_; foreach my $mapping (@{$mappings}) { my ($target, $options) = @{$mapping}; # Copy (scalar) data since can have multiple targets @@ -332,24 +332,46 @@ sub marc_records_to_documents { $options->{property} => $_data } } - push @{$record_document->{$target}}, $_data; + + # For sort fields, index only a single field with concatenated values + # and ignore alt script fields + if ($target =~ /__sort$/) { + if (!$altscript) { + if (!@{$record_document->{$target}}) { + push @{$record_document->{$target}}, $_data; + } else { + @{$record_document->{$target}}[0] .= " $_data"; + } + } + } else { + push @{$record_document->{$target}}, $_data; + } } } foreach my $record (@{$records}) { my $record_document = {}; my $mappings = $rules->{leader}; if ($mappings) { - _process_mappings($mappings, $record->leader(), $record_document); + _process_mappings($mappings, $record->leader(), $record_document, 0); } foreach my $field ($record->fields()) { - if($field->is_control_field()) { + if ($field->is_control_field()) { my $mappings = $control_fields_rules->{$field->tag()}; if ($mappings) { - _process_mappings($mappings, $field->data(), $record_document); + _process_mappings($mappings, $field->data(), $record_document, 0); } - } - else { - my $subfields_mappings = $data_fields_rules->{$field->tag()}; + } else { + my $tag = $field->tag(); + # Handle alternate scripts in MARC 21 + my $altscript = 0; + if ($marcflavour eq 'marc21' && $tag eq '880') { + my $sub6 = $field->subfield('6'); + if ($sub6 =~ /^(...)-\d+/) { + $tag = $1; + $altscript = 1; + } + } + my $subfields_mappings = $data_fields_rules->{$tag}; if ($subfields_mappings) { my $wildcard_mappings = $subfields_mappings->{'*'}; foreach my $subfield ($field->subfields()) { --- a/t/Koha/SearchEngine/Elasticsearch.t +++ a/t/Koha/SearchEngine/Elasticsearch.t @@ -267,8 +267,8 @@ subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' is(scalar @{$docs->[0][1]->{author}}, 2, 'First document author field should contain two values'); is_deeply($docs->[0][1]->{author}, ['Author 1', 'Corp Author'], 'First document author field should be set correctly'); - is(scalar @{$docs->[0][1]->{author__sort}}, 2, 'First document author__sort field should have two values'); - is_deeply($docs->[0][1]->{author__sort}, ['Author 1', 'Corp Author'], 'First document author__sort field should be set correctly'); + is(scalar @{$docs->[0][1]->{author__sort}}, 1, 'First document author__sort field should have a single value'); + is_deeply($docs->[0][1]->{author__sort}, ['Author 1 Corp Author'], 'First document author__sort field should be set correctly'); is(scalar @{$docs->[0][1]->{title__sort}}, 1, 'First document title__sort field should have one value'); is_deeply($docs->[0][1]->{title__sort}, ['Title: first record'], 'First document title__sort field should be set correctly'); --