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

(-)a/Koha/SearchEngine/Elasticsearch.pm (-2 / +16 lines)
Lines 36-42 use Search::Elasticsearch; Link Here
36
use Try::Tiny;
36
use Try::Tiny;
37
use YAML::Syck;
37
use YAML::Syck;
38
38
39
use List::Util qw( sum0 reduce );
39
use List::Util qw( sum0 reduce all );
40
use MARC::File::XML;
40
use MARC::File::XML;
41
use MIME::Base64;
41
use MIME::Base64;
42
use Encode qw(encode);
42
use Encode qw(encode);
Lines 215-220 sub get_elasticsearch_mappings { Link Here
215
                    $es_type = 'integer';
215
                    $es_type = 'integer';
216
                } elsif ($type eq 'isbn' || $type eq 'stdno') {
216
                } elsif ($type eq 'isbn' || $type eq 'stdno') {
217
                    $es_type = 'stdno';
217
                    $es_type = 'stdno';
218
                } elsif ($type eq 'year') {
219
                    $es_type = 'year';
218
                }
220
                }
219
221
220
                if ($search) {
222
                if ($search) {
Lines 398-404 sub _process_mappings { Link Here
398
        # with differing options for (possibly) mutating data
400
        # with differing options for (possibly) mutating data
399
        # so need a different copy for each
401
        # so need a different copy for each
400
        my $_data = $data;
402
        my $_data = $data;
401
        $record_document->{$target} //= [];
402
        if (defined $options->{substr}) {
403
        if (defined $options->{substr}) {
403
            my ($start, $length) = @{$options->{substr}};
404
            my ($start, $length) = @{$options->{substr}};
404
            $_data = length($data) > $start ? substr $data, $start, $length : '';
405
            $_data = length($data) > $start ? substr $data, $start, $length : '';
Lines 406-416 sub _process_mappings { Link Here
406
        if (defined $options->{value_callbacks}) {
407
        if (defined $options->{value_callbacks}) {
407
            $_data = reduce { $b->($a) } ($_data, @{$options->{value_callbacks}});
408
            $_data = reduce { $b->($a) } ($_data, @{$options->{value_callbacks}});
408
        }
409
        }
410
        if (defined $options->{filter_callbacks}) {
411
            # Skip mapping unless all filter callbacks return true
412
            next unless all { $_->($_data) } @{$options->{filter_callbacks}};
413
        }
409
        if (defined $options->{property}) {
414
        if (defined $options->{property}) {
410
            $_data = {
415
            $_data = {
411
                $options->{property} => $_data
416
                $options->{property} => $_data
412
            }
417
            }
413
        }
418
        }
419
420
        $record_document->{$target} //= [];
414
        push @{$record_document->{$target}}, $_data;
421
        push @{$record_document->{$target}}, $_data;
415
    }
422
    }
416
}
423
}
Lines 768-773 sub _field_mappings { Link Here
768
            return $value ? 'true' : 'false';
775
            return $value ? 'true' : 'false';
769
        };
776
        };
770
    }
777
    }
778
    elsif ($target_type eq 'year') {
779
        $default_options->{filter_callbacks} //= [];
780
        push @{$default_options->{filter_callbacks}}, sub {
781
            my ($value) = @_;
782
            return $value =~ /^\d+$/;
783
        };
784
    }
771
785
772
    if ($search) {
786
    if ($search) {
773
        my $mapping = [$target_name, $default_options];
787
        my $mapping = [$target_name, $default_options];
(-)a/Koha/SearchEngine/Elasticsearch/Indexer.pm (+3 lines)
Lines 123-128 sub update_index { Link Here
123
            type => 'data', # is just hard coded in Indexer.pm?
123
            type => 'data', # is just hard coded in Indexer.pm?
124
            body => \@body
124
            body => \@body
125
        );
125
        );
126
        if ($response->{errors}) {
127
            carp "One or more ElasticSearch errors occured when indexing documents";
128
        }
126
    }
129
    }
127
    # TODO: handle response
130
    # TODO: handle response
128
    return 1;
131
    return 1;
(-)a/admin/searchengine/elasticsearch/field_config.yaml (+2 lines)
Lines 24-29 search: Link Here
24
  integer:
24
  integer:
25
    type: integer
25
    type: integer
26
    null_value: 0
26
    null_value: 0
27
  year:
28
    type: short
27
  stdno:
29
  stdno:
28
    type: text
30
    type: text
29
    analyzer: analyzer_stdno
31
    analyzer: analyzer_stdno
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt (+5 lines)
Lines 192-197 a.add, a.delete { Link Here
192
                          [% ELSE %]
192
                          [% ELSE %]
193
                            <option value="date">Date</option>
193
                            <option value="date">Date</option>
194
                          [% END %]
194
                          [% END %]
195
                          [% IF search_field.type == "year" %]
196
                            <option value="year" selected="selected">Year</option>
197
                          [% ELSE %]
198
                            <option value="year">Year</option>
199
                          [% END %]
195
                          [% IF search_field.type == "number" %]
200
                          [% IF search_field.type == "number" %]
196
                            <option value="number" selected="selected">Number</option>
201
                            <option value="number" selected="selected">Number</option>
197
                          [% ELSE %]
202
                          [% ELSE %]
(-)a/t/Koha/SearchEngine/Elasticsearch.t (-2 / +21 lines)
Lines 117-123 subtest 'get_elasticsearch_mappings() tests' => sub { Link Here
117
117
118
subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' => sub {
118
subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' => sub {
119
119
120
    plan tests => 51;
120
    plan tests => 54;
121
121
122
    t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
122
    t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
123
    t::lib::Mocks::mock_preference('ElasticsearchMARCFormat', 'ISO2709');
123
    t::lib::Mocks::mock_preference('ElasticsearchMARCFormat', 'ISO2709');
Lines 273-278 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
273
            marc_type => 'marc21',
273
            marc_type => 'marc21',
274
            marc_field => '952l',
274
            marc_field => '952l',
275
        },
275
        },
276
        {
277
            name => 'date-of-publication',
278
            type => 'year',
279
            facet => 0,
280
            suggestible => 0,
281
            searchable => 1,
282
            sort => 1,
283
            marc_type => 'marc21',
284
            marc_field => '008_/7-10',
285
        },
286
276
    );
287
    );
277
288
278
    my $se = Test::MockModule->new('Koha::SearchEngine::Elasticsearch');
289
    my $se = Test::MockModule->new('Koha::SearchEngine::Elasticsearch');
Lines 304-309 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
304
    $marc_record_1->append_fields(
315
    $marc_record_1->append_fields(
305
        MARC::Field->new('001', '123'),
316
        MARC::Field->new('001', '123'),
306
        MARC::Field->new('007', 'ku'),
317
        MARC::Field->new('007', 'ku'),
318
        MARC::Field->new('008', '901111s1962 xxk|||| |00| ||eng c'),
307
        MARC::Field->new('020', '', '', a => '1-56619-909-3'),
319
        MARC::Field->new('020', '', '', a => '1-56619-909-3'),
308
        MARC::Field->new('100', '', '', a => 'Author 1'),
320
        MARC::Field->new('100', '', '', a => 'Author 1'),
309
        MARC::Field->new('110', '', '', a => 'Corp Author'),
321
        MARC::Field->new('110', '', '', a => 'Corp Author'),
Lines 318-323 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
318
    my $marc_record_2 = MARC::Record->new();
330
    my $marc_record_2 = MARC::Record->new();
319
    $marc_record_2->leader('     cam  22      a 4500');
331
    $marc_record_2->leader('     cam  22      a 4500');
320
    $marc_record_2->append_fields(
332
    $marc_record_2->append_fields(
333
        MARC::Field->new('008', '901111s19uu xxk|||| |00| ||eng c'),
321
        MARC::Field->new('100', '', '', a => 'Author 2'),
334
        MARC::Field->new('100', '', '', a => 'Author 2'),
322
        # MARC::Field->new('210', '', '', a => 'Title 2'),
335
        # MARC::Field->new('210', '', '', a => 'Title 2'),
323
        # MARC::Field->new('245', '', '', a => 'Title: second record'),
336
        # MARC::Field->new('245', '', '', a => 'Title: second record'),
Lines 432-437 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
432
        'First document local_classification field should be set correctly'
445
        'First document local_classification field should be set correctly'
433
    );
446
    );
434
447
448
    # Tests for 'year' type and 'filter_callbacks'
449
    is(scalar @{$docs->[0]->{'date-of-publication'}}, 1, 'First document date-of-publication field should contain one value');
450
    is_deeply($docs->[0]->{'date-of-publication'}, ['1962'], 'First document date-of-publication field should be set correctly');
451
435
    # Second record:
452
    # Second record:
436
453
437
    is(scalar @{$docs->[1]->{author}}, 1, 'Second document author field should contain one value');
454
    is(scalar @{$docs->[1]->{author}}, 1, 'Second document author field should contain one value');
Lines 456-461 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
456
        'Second document local_classification__sort field should be set correctly'
473
        'Second document local_classification__sort field should be set correctly'
457
    );
474
    );
458
475
476
    # Tests for 'year' type and 'filter_callbacks'
477
    ok(!(defined $docs->[1]->{'date-of-publication'}), "Second document invalid date-of-publication value should have been removed");
478
459
    # Mappings marc_type:
479
    # Mappings marc_type:
460
480
461
    ok(!(defined $docs->[0]->{unimarc_title}), "No mapping when marc_type doesn't match marc flavour");
481
    ok(!(defined $docs->[0]->{unimarc_title}), "No mapping when marc_type doesn't match marc flavour");
462
- 

Return to bug 24807