View | Details | Raw Unified | Return to bug 23630
Collapse All | Expand All

(-)a/Koha/SearchEngine/Elasticsearch.pm (-3 / +2 lines)
Lines 385-391 sub _process_mappings { Link Here
385
385
386
=head2 marc_records_to_documents($marc_records)
386
=head2 marc_records_to_documents($marc_records)
387
387
388
    my @record_documents = $self->marc_records_to_documents($marc_records);
388
    my $record_documents = $self->marc_records_to_documents($marc_records);
389
389
390
Using mappings stored in database convert C<$marc_records> to Elasticsearch documents.
390
Using mappings stored in database convert C<$marc_records> to Elasticsearch documents.
391
391
Lines 554-561 sub marc_records_to_documents { Link Here
554
                $record_document->{'marc_format'} = 'base64ISO2709';
554
                $record_document->{'marc_format'} = 'base64ISO2709';
555
            }
555
            }
556
        }
556
        }
557
        my $id = $record->subfield('999', 'c');
557
        push @record_documents, $record_document;
558
        push @record_documents, [$id, $record_document];
559
    }
558
    }
560
    return \@record_documents;
559
    return \@record_documents;
561
}
560
}
(-)a/Koha/SearchEngine/Elasticsearch/Indexer.pm (-36 / +3 lines)
Lines 84-97 use constant { Link Here
84
Converts C<MARC::Records> C<$records> to Elasticsearch documents and performs
84
Converts C<MARC::Records> C<$records> to Elasticsearch documents and performs
85
an update request for these records on the Elasticsearch index.
85
an update request for these records on the Elasticsearch index.
86
86
87
The values in the arrays must match up, and the 999$c value in the MARC record
88
will be rewritten using the values in C<$biblionums> to ensure they are correct.
89
If C<$biblionums> is C<undef>, this won't happen, so in that case you should make
90
sure that 999$c is correct.
91
92
Note that this will modify the original record if C<$biblionums> is supplied.
93
If that's a problem, clone them first.
94
95
=over 4
87
=over 4
96
88
97
=item C<$biblionums>
89
=item C<$biblionums>
Lines 110-126 Arrayref of C<MARC::Record>s. Link Here
110
sub update_index {
102
sub update_index {
111
    my ($self, $biblionums, $records) = @_;
103
    my ($self, $biblionums, $records) = @_;
112
104
113
    if ($biblionums) {
114
        $self->_sanitise_records($biblionums, $records);
115
    }
116
117
    my $conf = $self->get_elasticsearch_params();
105
    my $conf = $self->get_elasticsearch_params();
118
    my $elasticsearch = $self->get_elasticsearch();
106
    my $elasticsearch = $self->get_elasticsearch();
119
    my $documents = $self->marc_records_to_documents($records);
107
    my $documents = $self->marc_records_to_documents($records);
120
    my @body;
108
    my @body;
121
109
122
    foreach my $document_info (@{$documents}) {
110
    for (my $i=0; $i < scalar @$biblionums; $i++) {
123
        my ($id, $document) = @{$document_info};
111
        my $id = $biblionums->[$i];
112
        my $document = $documents->[$i];
124
        push @body, {
113
        push @body, {
125
            index => {
114
            index => {
126
                _id => $id
115
                _id => $id
Lines 382-409 sub index_exists { Link Here
382
    );
371
    );
383
}
372
}
384
373
385
sub _sanitise_records {
386
    my ($self, $biblionums, $records) = @_;
387
388
    confess "Unequal number of values in \$biblionums and \$records." if (@$biblionums != @$records);
389
390
    my $c = @$biblionums;
391
    for (my $i=0; $i<$c; $i++) {
392
        my $bibnum = $biblionums->[$i];
393
        my $rec = $records->[$i];
394
        # I've seen things you people wouldn't believe. Attack ships on fire
395
        # off the shoulder of Orion. I watched C-beams glitter in the dark near
396
        # the Tannhauser gate. MARC records where 999$c doesn't match the
397
        # biblionumber column. All those moments will be lost in time... like
398
        # tears in rain...
399
        if ( $rec ) {
400
            $rec->delete_fields($rec->field('999'));
401
            # Make sure biblionumber is a string. Elasticsearch would consider int and string different IDs.
402
            $rec->append_fields(MARC::Field->new('999','','','c' => "" . $bibnum, 'd' => "" . $bibnum));
403
        }
404
    }
405
}
406
407
1;
374
1;
408
375
409
__END__
376
__END__
(-)a/t/db_dependent/Koha/SearchEngine/Elasticsearch/Indexer.t (-2 / +1 lines)
Lines 59-65 subtest 'create_index() tests' => sub { Link Here
59
        MARC::Field->new('245', '', '', 'a' => 'Title')
59
        MARC::Field->new('245', '', '', 'a' => 'Title')
60
    );
60
    );
61
    my $records = [$marc_record];
61
    my $records = [$marc_record];
62
    ok($indexer->update_index(undef, $records), 'Update Index');
62
    ok($indexer->update_index([1], $records), 'Update Index');
63
63
64
    is(
64
    is(
65
        $indexer->drop_index(),
65
        $indexer->drop_index(),
66
- 

Return to bug 23630