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

(-)a/Koha/SearchEngine/Elasticsearch.pm (-2 / +16 lines)
Lines 39-45 use Search::Elasticsearch; Link Here
39
use Try::Tiny;
39
use Try::Tiny;
40
use YAML::Syck;
40
use YAML::Syck;
41
41
42
use List::Util qw( sum0 reduce );
42
use List::Util qw( sum0 reduce all );
43
use MARC::File::XML;
43
use MARC::File::XML;
44
use MIME::Base64;
44
use MIME::Base64;
45
use Encode qw(encode);
45
use Encode qw(encode);
Lines 209-214 sub get_elasticsearch_mappings { Link Here
209
                    $es_type = 'integer';
209
                    $es_type = 'integer';
210
                } elsif ($type eq 'isbn' || $type eq 'stdno') {
210
                } elsif ($type eq 'isbn' || $type eq 'stdno') {
211
                    $es_type = 'stdno';
211
                    $es_type = 'stdno';
212
                } elsif ($type eq 'year') {
213
                    $es_type = 'year';
212
                }
214
                }
213
215
214
                if ($search) {
216
                if ($search) {
Lines 470-476 sub _process_mappings { Link Here
470
        # with differing options for (possibly) mutating data
472
        # with differing options for (possibly) mutating data
471
        # so need a different copy for each
473
        # so need a different copy for each
472
        my $_data = $data;
474
        my $_data = $data;
473
        $record_document->{$target} //= [];
474
        if (defined $options->{substr}) {
475
        if (defined $options->{substr}) {
475
            my ($start, $length) = @{$options->{substr}};
476
            my ($start, $length) = @{$options->{substr}};
476
            $_data = length($data) > $start ? substr $data, $start, $length : '';
477
            $_data = length($data) > $start ? substr $data, $start, $length : '';
Lines 478-483 sub _process_mappings { Link Here
478
        if (defined $options->{value_callbacks}) {
479
        if (defined $options->{value_callbacks}) {
479
            $_data = reduce { $b->($a) } ($_data, @{$options->{value_callbacks}});
480
            $_data = reduce { $b->($a) } ($_data, @{$options->{value_callbacks}});
480
        }
481
        }
482
        if (defined $options->{filter_callbacks}) {
483
            # Skip mapping unless all filter callbacks return true
484
            next unless all { $_->($_data) } @{$options->{filter_callbacks}};
485
        }
481
        if (defined $options->{property}) {
486
        if (defined $options->{property}) {
482
            $_data = {
487
            $_data = {
483
                $options->{property} => $_data
488
                $options->{property} => $_data
Lines 490-495 sub _process_mappings { Link Here
490
                $_data = substr $_data, $nonfiling_chars;
495
                $_data = substr $_data, $nonfiling_chars;
491
            }
496
            }
492
        }
497
        }
498
499
        $record_document->{$target} //= [];
493
        push @{$record_document->{$target}}, $_data;
500
        push @{$record_document->{$target}}, $_data;
494
    }
501
    }
495
}
502
}
Lines 885-890 sub _field_mappings { Link Here
885
            return $value ? 'true' : 'false';
892
            return $value ? 'true' : 'false';
886
        };
893
        };
887
    }
894
    }
895
    elsif ($target_type eq 'year') {
896
        $default_options->{filter_callbacks} //= [];
897
        push @{$default_options->{filter_callbacks}}, sub {
898
            my ($value) = @_;
899
            return $value =~ /^\d+$/;
900
        };
901
    }
888
902
889
    if ($search) {
903
    if ($search) {
890
        my $mapping = [$target_name, $default_options];
904
        my $mapping = [$target_name, $default_options];
(-)a/Koha/SearchEngine/Elasticsearch/Indexer.pm (+3 lines)
Lines 117-122 sub update_index { Link Here
117
            type => 'data', # is just hard coded in Indexer.pm?
117
            type => 'data', # is just hard coded in Indexer.pm?
118
            body => \@body
118
            body => \@body
119
        );
119
        );
120
        if ($response->{errors}) {
121
            carp "One or more ElasticSearch errors occured when indexing documents";
122
        }
120
    }
123
    }
121
    return $response;
124
    return $response;
122
}
125
}
(-)a/admin/searchengine/elasticsearch/field_config.yaml (+2 lines)
Lines 25-30 search: Link Here
25
    type: integer
25
    type: integer
26
    null_value: 0
26
    null_value: 0
27
    ignore_malformed: true
27
    ignore_malformed: true
28
  year:
29
    type: short
28
  stdno:
30
  stdno:
29
    type: text
31
    type: text
30
    analyzer: analyzer_stdno
32
    analyzer: analyzer_stdno
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt (+5 lines)
Lines 196-201 a.add, a.delete { Link Here
196
                          [% ELSE %]
196
                          [% ELSE %]
197
                            <option value="date">Date</option>
197
                            <option value="date">Date</option>
198
                          [% END %]
198
                          [% END %]
199
                          [% IF search_field.type == "year" %]
200
                            <option value="year" selected="selected">Year</option>
201
                          [% ELSE %]
202
                            <option value="year">Year</option>
203
                          [% END %]
199
                          [% IF search_field.type == "number" %]
204
                          [% IF search_field.type == "number" %]
200
                            <option value="number" selected="selected">Number</option>
205
                            <option value="number" selected="selected">Number</option>
201
                          [% ELSE %]
206
                          [% ELSE %]
(-)a/t/db_dependent/Koha/SearchEngine/Elasticsearch.t (-2 / +21 lines)
Lines 132-138 subtest 'get_elasticsearch_mappings() tests' => sub { Link Here
132
132
133
subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' => sub {
133
subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' => sub {
134
134
135
    plan tests => 53;
135
    plan tests => 56;
136
136
137
    t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
137
    t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
138
    t::lib::Mocks::mock_preference('ElasticsearchMARCFormat', 'ISO2709');
138
    t::lib::Mocks::mock_preference('ElasticsearchMARCFormat', 'ISO2709');
Lines 298-303 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
298
            marc_type => 'marc21',
298
            marc_type => 'marc21',
299
            marc_field => '952l',
299
            marc_field => '952l',
300
        },
300
        },
301
        {
302
            name => 'date-of-publication',
303
            type => 'year',
304
            facet => 0,
305
            suggestible => 0,
306
            searchable => 1,
307
            sort => 1,
308
            marc_type => 'marc21',
309
            marc_field => '008_/7-10',
310
        },
311
301
    );
312
    );
302
313
303
    my $se = Test::MockModule->new('Koha::SearchEngine::Elasticsearch');
314
    my $se = Test::MockModule->new('Koha::SearchEngine::Elasticsearch');
Lines 329-334 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
329
    $marc_record_1->append_fields(
340
    $marc_record_1->append_fields(
330
        MARC::Field->new('001', '123'),
341
        MARC::Field->new('001', '123'),
331
        MARC::Field->new('007', 'ku'),
342
        MARC::Field->new('007', 'ku'),
343
        MARC::Field->new('008', '901111s1962 xxk|||| |00| ||eng c'),
332
        MARC::Field->new('020', '', '', a => '1-56619-909-3'),
344
        MARC::Field->new('020', '', '', a => '1-56619-909-3'),
333
        MARC::Field->new('100', '', '', a => 'Author 1'),
345
        MARC::Field->new('100', '', '', a => 'Author 1'),
334
        MARC::Field->new('110', '', '', a => 'Corp Author'),
346
        MARC::Field->new('110', '', '', a => 'Corp Author'),
Lines 344-349 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
344
    my $marc_record_2 = MARC::Record->new();
356
    my $marc_record_2 = MARC::Record->new();
345
    $marc_record_2->leader('     cam  22      a 4500');
357
    $marc_record_2->leader('     cam  22      a 4500');
346
    $marc_record_2->append_fields(
358
    $marc_record_2->append_fields(
359
        MARC::Field->new('008', '901111s19uu xxk|||| |00| ||eng c'),
347
        MARC::Field->new('100', '', '', a => 'Author 2'),
360
        MARC::Field->new('100', '', '', a => 'Author 2'),
348
        # MARC::Field->new('210', '', '', a => 'Title 2'),
361
        # MARC::Field->new('210', '', '', a => 'Title 2'),
349
        # MARC::Field->new('245', '', '', a => 'Title: second record'),
362
        # MARC::Field->new('245', '', '', a => 'Title: second record'),
Lines 470-475 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
470
        'First document uniform_title__sort field should contain the title with the first four initial characters removed'
483
        'First document uniform_title__sort field should contain the title with the first four initial characters removed'
471
    );
484
    );
472
485
486
    # Tests for 'year' type and 'filter_callbacks'
487
    is(scalar @{$docs->[0]->{'date-of-publication'}}, 1, 'First document date-of-publication field should contain one value');
488
    is_deeply($docs->[0]->{'date-of-publication'}, ['1962'], 'First document date-of-publication field should be set correctly');
489
473
    # Second record:
490
    # Second record:
474
491
475
    is(scalar @{$docs->[1]->{author}}, 1, 'Second document author field should contain one value');
492
    is(scalar @{$docs->[1]->{author}}, 1, 'Second document author field should contain one value');
Lines 494-499 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' Link Here
494
        'Second document local_classification__sort field should be set correctly'
511
        'Second document local_classification__sort field should be set correctly'
495
    );
512
    );
496
513
514
    # Tests for 'year' type and 'filter_callbacks'
515
    ok(!(defined $docs->[1]->{'date-of-publication'}), "Second document invalid date-of-publication value should have been removed");
516
497
    # Mappings marc_type:
517
    # Mappings marc_type:
498
518
499
    ok(!(defined $docs->[0]->{unimarc_title}), "No mapping when marc_type doesn't match marc flavour");
519
    ok(!(defined $docs->[0]->{unimarc_title}), "No mapping when marc_type doesn't match marc flavour");
500
- 

Return to bug 24807