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

(-)a/Koha/SearchEngine/Elasticsearch.pm (-2 / +16 lines)
Lines 37-43 use Search::Elasticsearch; Link Here
37
use Try::Tiny;
37
use Try::Tiny;
38
use YAML::Syck;
38
use YAML::Syck;
39
39
40
use List::Util qw( sum0 reduce );
40
use List::Util qw( sum0 reduce all );
41
use MARC::File::XML;
41
use MARC::File::XML;
42
use MIME::Base64;
42
use MIME::Base64;
43
use Encode qw(encode);
43
use Encode qw(encode);
Lines 216-221 sub get_elasticsearch_mappings { Link Here
216
                    $es_type = 'integer';
216
                    $es_type = 'integer';
217
                } elsif ($type eq 'isbn' || $type eq 'stdno') {
217
                } elsif ($type eq 'isbn' || $type eq 'stdno') {
218
                    $es_type = 'stdno';
218
                    $es_type = 'stdno';
219
                } elsif ($type eq 'year') {
220
                    $es_type = 'year';
219
                }
221
                }
220
222
221
                if ($search) {
223
                if ($search) {
Lines 471-477 sub _process_mappings { Link Here
471
        # with differing options for (possibly) mutating data
473
        # with differing options for (possibly) mutating data
472
        # so need a different copy for each
474
        # so need a different copy for each
473
        my $_data = $data;
475
        my $_data = $data;
474
        $record_document->{$target} //= [];
475
        if (defined $options->{substr}) {
476
        if (defined $options->{substr}) {
476
            my ($start, $length) = @{$options->{substr}};
477
            my ($start, $length) = @{$options->{substr}};
477
            $_data = length($data) > $start ? substr $data, $start, $length : '';
478
            $_data = length($data) > $start ? substr $data, $start, $length : '';
Lines 479-484 sub _process_mappings { Link Here
479
        if (defined $options->{value_callbacks}) {
480
        if (defined $options->{value_callbacks}) {
480
            $_data = reduce { $b->($a) } ($_data, @{$options->{value_callbacks}});
481
            $_data = reduce { $b->($a) } ($_data, @{$options->{value_callbacks}});
481
        }
482
        }
483
        if (defined $options->{filter_callbacks}) {
484
            # Skip mapping unless all filter callbacks return true
485
            next unless all { $_->($_data) } @{$options->{filter_callbacks}};
486
        }
482
        if (defined $options->{property}) {
487
        if (defined $options->{property}) {
483
            $_data = {
488
            $_data = {
484
                $options->{property} => $_data
489
                $options->{property} => $_data
Lines 491-496 sub _process_mappings { Link Here
491
                $_data = substr $_data, $nonfiling_chars;
496
                $_data = substr $_data, $nonfiling_chars;
492
            }
497
            }
493
        }
498
        }
499
500
        $record_document->{$target} //= [];
494
        push @{$record_document->{$target}}, $_data;
501
        push @{$record_document->{$target}}, $_data;
495
    }
502
    }
496
}
503
}
Lines 882-887 sub _field_mappings { Link Here
882
            return $value ? 'true' : 'false';
889
            return $value ? 'true' : 'false';
883
        };
890
        };
884
    }
891
    }
892
    elsif ($target_type eq 'year') {
893
        $default_options->{filter_callbacks} //= [];
894
        push @{$default_options->{filter_callbacks}}, sub {
895
            my ($value) = @_;
896
            return $value =~ /^\d+$/;
897
        };
898
    }
885
899
886
    if ($search) {
900
    if ($search) {
887
        my $mapping = [$target_name, $default_options];
901
        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 118-124 subtest 'get_elasticsearch_mappings() tests' => sub { Link Here
118
118
119
subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' => sub {
119
subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' => sub {
120
120
121
    plan tests => 53;
121
    plan tests => 56;
122
122
123
    t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
123
    t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
124
    t::lib::Mocks::mock_preference('ElasticsearchMARCFormat', 'ISO2709');
124
    t::lib::Mocks::mock_preference('ElasticsearchMARCFormat', 'ISO2709');
Lines 284-289 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
284
            marc_type => 'marc21',
284
            marc_type => 'marc21',
285
            marc_field => '952l',
285
            marc_field => '952l',
286
        },
286
        },
287
        {
288
            name => 'date-of-publication',
289
            type => 'year',
290
            facet => 0,
291
            suggestible => 0,
292
            searchable => 1,
293
            sort => 1,
294
            marc_type => 'marc21',
295
            marc_field => '008_/7-10',
296
        },
297
287
    );
298
    );
288
299
289
    my $se = Test::MockModule->new('Koha::SearchEngine::Elasticsearch');
300
    my $se = Test::MockModule->new('Koha::SearchEngine::Elasticsearch');
Lines 315-320 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
315
    $marc_record_1->append_fields(
326
    $marc_record_1->append_fields(
316
        MARC::Field->new('001', '123'),
327
        MARC::Field->new('001', '123'),
317
        MARC::Field->new('007', 'ku'),
328
        MARC::Field->new('007', 'ku'),
329
        MARC::Field->new('008', '901111s1962 xxk|||| |00| ||eng c'),
318
        MARC::Field->new('020', '', '', a => '1-56619-909-3'),
330
        MARC::Field->new('020', '', '', a => '1-56619-909-3'),
319
        MARC::Field->new('100', '', '', a => 'Author 1'),
331
        MARC::Field->new('100', '', '', a => 'Author 1'),
320
        MARC::Field->new('110', '', '', a => 'Corp Author'),
332
        MARC::Field->new('110', '', '', a => 'Corp Author'),
Lines 330-335 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
330
    my $marc_record_2 = MARC::Record->new();
342
    my $marc_record_2 = MARC::Record->new();
331
    $marc_record_2->leader('     cam  22      a 4500');
343
    $marc_record_2->leader('     cam  22      a 4500');
332
    $marc_record_2->append_fields(
344
    $marc_record_2->append_fields(
345
        MARC::Field->new('008', '901111s19uu xxk|||| |00| ||eng c'),
333
        MARC::Field->new('100', '', '', a => 'Author 2'),
346
        MARC::Field->new('100', '', '', a => 'Author 2'),
334
        # MARC::Field->new('210', '', '', a => 'Title 2'),
347
        # MARC::Field->new('210', '', '', a => 'Title 2'),
335
        # MARC::Field->new('245', '', '', a => 'Title: second record'),
348
        # MARC::Field->new('245', '', '', a => 'Title: second record'),
Lines 456-461 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
456
        'First document uniform_title__sort field should contain the title with the first four initial characters removed'
469
        'First document uniform_title__sort field should contain the title with the first four initial characters removed'
457
    );
470
    );
458
471
472
    # Tests for 'year' type and 'filter_callbacks'
473
    is(scalar @{$docs->[0]->{'date-of-publication'}}, 1, 'First document date-of-publication field should contain one value');
474
    is_deeply($docs->[0]->{'date-of-publication'}, ['1962'], 'First document date-of-publication field should be set correctly');
475
459
    # Second record:
476
    # Second record:
460
477
461
    is(scalar @{$docs->[1]->{author}}, 1, 'Second document author field should contain one value');
478
    is(scalar @{$docs->[1]->{author}}, 1, 'Second document author field should contain one value');
Lines 480-485 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
480
        'Second document local_classification__sort field should be set correctly'
497
        'Second document local_classification__sort field should be set correctly'
481
    );
498
    );
482
499
500
    # Tests for 'year' type and 'filter_callbacks'
501
    ok(!(defined $docs->[1]->{'date-of-publication'}), "Second document invalid date-of-publication value should have been removed");
502
483
    # Mappings marc_type:
503
    # Mappings marc_type:
484
504
485
    ok(!(defined $docs->[0]->{unimarc_title}), "No mapping when marc_type doesn't match marc flavour");
505
    ok(!(defined $docs->[0]->{unimarc_title}), "No mapping when marc_type doesn't match marc flavour");
486
- 

Return to bug 24807