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

(-)a/Koha/SearchEngine/Elasticsearch.pm (-4 / +16 lines)
Lines 277-287 sub _get_elasticsearch_field_config { Link Here
277
    return;
277
    return;
278
}
278
}
279
279
280
sub reset_elasticsearch_mappings {
280
=head2 _load_elasticsearch_mappings
281
    my ( $reset_fields ) = @_;
281
282
Load Elasticsearch mappings in the format of mappings.yaml.
283
284
$indexes = _load_elasticsearch_mappings();
285
286
=cut
287
288
sub _load_elasticsearch_mappings {
282
    my $mappings_yaml = C4::Context->config('elasticsearch_index_mappings');
289
    my $mappings_yaml = C4::Context->config('elasticsearch_index_mappings');
283
    $mappings_yaml ||= C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/mappings.yaml';
290
    $mappings_yaml ||= C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/mappings.yaml';
284
    my $indexes = LoadFile( $mappings_yaml );
291
    return LoadFile( $mappings_yaml );
292
}
293
294
sub reset_elasticsearch_mappings {
295
    my ( $self ) = @_;
296
    my $indexes = $self->_load_elasticsearch_mappings();
285
297
286
    while ( my ( $index_name, $fields ) = each %$indexes ) {
298
    while ( my ( $index_name, $fields ) = each %$indexes ) {
287
        while ( my ( $field_name, $data ) = each %$fields ) {
299
        while ( my ( $field_name, $data ) = each %$fields ) {
Lines 318-324 sub reset_elasticsearch_mappings { Link Here
318
                    facet => $mapping->{facet} || 0,
330
                    facet => $mapping->{facet} || 0,
319
                    suggestible => $mapping->{suggestible} || 0,
331
                    suggestible => $mapping->{suggestible} || 0,
320
                    sort => $mapping->{sort},
332
                    sort => $mapping->{sort},
321
                    search => $mapping->{search} || 1
333
                    search => $mapping->{search} // 1
322
                });
334
                });
323
            }
335
            }
324
        }
336
        }
(-)a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t (-38 / +77 lines)
Lines 458-511 subtest 'build query from form subtests' => sub { Link Here
458
};
458
};
459
459
460
subtest 'build_query with weighted fields tests' => sub {
460
subtest 'build_query with weighted fields tests' => sub {
461
    plan tests => 2;
461
    plan tests => 4;
462
462
463
    my $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'mydb' } );
463
    $se->mock( '_load_elasticsearch_mappings', sub {
464
    my $db_builder = t::lib::TestBuilder->new();
464
        return {
465
465
            biblios => {
466
    Koha::SearchFields->search({})->delete;
466
                abstract => {
467
    $clear_search_fields_cache->();
467
                    label => 'abstract',
468
468
                    type => 'string',
469
    $db_builder->build({
469
                    opac => 1,
470
        source => 'SearchField',
470
                    staff_client => 0,
471
        value => {
471
                    mappings => [{
472
            name    => 'acqdate',
472
                        marc_field => '520',
473
            label   => 'acqdate',
473
                        marc_type => 'marc21',
474
            weight  => undef,
474
                    }]
475
            staff_client => 1
475
                },
476
        }
476
                acqdate => {
477
                    label => 'acqdate',
478
                    type => 'string',
479
                    opac => 0,
480
                    staff_client => 1,
481
                    mappings => [{
482
                        marc_field => '952d',
483
                        marc_type => 'marc21',
484
                        search => 0,
485
                    }, {
486
                        marc_field => '9955',
487
                        marc_type => 'marc21',
488
                        search => 0,
489
                    }]
490
                },
491
                title => {
492
                    label => 'title',
493
                    type => 'string',
494
                    opac => 0,
495
                    staff_client => 1,
496
                    mappings => [{
497
                        marc_field => '130',
498
                        marc_type => 'marc21'
499
                    }]
500
                },
501
                subject => {
502
                    label => 'subject',
503
                    type => 'string',
504
                    opac => 0,
505
                    staff_client => 1,
506
                    mappings => [{
507
                        marc_field => '600a',
508
                        marc_type => 'marc21'
509
                    }]
510
                }
511
            }
512
        };
477
    });
513
    });
478
514
479
    $db_builder->build({
515
    my $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'biblios' } );
480
        source => 'SearchField',
516
    Koha::SearchFields->search({})->delete;
481
        value => {
517
    Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings();
482
            name    => 'title',
483
            label   => 'title',
484
            weight  => 25,
485
            staff_client => 1
486
        }
487
    });
488
518
489
    $db_builder->build({
519
    my $search_field;
490
        source => 'SearchField',
520
    $search_field = Koha::SearchFields->find({ name => 'title' });
491
        value => {
521
    $search_field->update({ weight => 25.0 });
492
            name    => 'subject',
522
    $search_field = Koha::SearchFields->find({ name => 'subject' });
493
            label   => 'subject',
523
    $search_field->update({ weight => 15.5 });
494
            weight  => 15,
524
    $clear_search_fields_cache->();
495
            staff_client => 1
496
        }
497
    });
498
525
499
    my ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
526
    my ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
500
    undef, undef, undef, { weighted_fields => 1 });
527
    undef, undef, undef, { weighted_fields => 1 });
501
528
502
    my $fields = $query->{query}{query_string}{fields};
529
    my $fields = $query->{query}{query_string}{fields};
503
530
531
    is(@{$fields}, 2, 'Search field with no searchable mappings has been excluded');
532
504
    my @found = grep { $_ eq 'title^25.00' } @{$fields};
533
    my @found = grep { $_ eq 'title^25.00' } @{$fields};
505
    is(@found, 1, 'Search field is title has correct weight'); # Fails
534
    is(@found, 1, 'Search field title has correct weight');
535
536
    @found = grep { $_ eq 'subject^15.50' } @{$fields};
537
    is(@found, 1, 'Search field subject has correct weight');
538
539
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
540
    undef, undef, undef, { weighted_fields => 1, is_opac => 1 });
506
541
507
    @found = grep { $_ eq 'subject^15.00' } @{$fields};
542
    $fields = $query->{query}{query_string}{fields};
508
    is(@found, 1, 'Search field subject has correct weight'); # Fails
543
544
    is_deeply(
545
        $fields,
546
        ['abstract'],
547
        'Only OPAC search fields are used when opac search is performed'
548
    );
509
};
549
};
510
550
511
subtest "_convert_sort_fields() tests" => sub {
551
subtest "_convert_sort_fields() tests" => sub {
512
- 

Return to bug 20589