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 307-313 sub reset_elasticsearch_mappings { Link Here
307
                    facet => $mapping->{facet} || 0,
319
                    facet => $mapping->{facet} || 0,
308
                    suggestible => $mapping->{suggestible} || 0,
320
                    suggestible => $mapping->{suggestible} || 0,
309
                    sort => $mapping->{sort},
321
                    sort => $mapping->{sort},
310
                    search => $mapping->{search} || 1
322
                    search => $mapping->{search} // 1
311
                });
323
                });
312
            }
324
            }
313
        }
325
        }
(-)a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t (-38 / +77 lines)
Lines 466-519 subtest 'build query from form subtests' => sub { Link Here
466
};
466
};
467
467
468
subtest 'build_query with weighted fields tests' => sub {
468
subtest 'build_query with weighted fields tests' => sub {
469
    plan tests => 2;
469
    plan tests => 4;
470
470
471
    my $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'mydb' } );
471
    $se->mock( '_load_elasticsearch_mappings', sub {
472
    my $db_builder = t::lib::TestBuilder->new();
472
        return {
473
473
            biblios => {
474
    Koha::SearchFields->search({})->delete;
474
                abstract => {
475
    $clear_search_fields_cache->();
475
                    label => 'abstract',
476
476
                    type => 'string',
477
    $db_builder->build({
477
                    opac => 1,
478
        source => 'SearchField',
478
                    staff_client => 0,
479
        value => {
479
                    mappings => [{
480
            name    => 'acqdate',
480
                        marc_field => '520',
481
            label   => 'acqdate',
481
                        marc_type => 'marc21',
482
            weight  => undef,
482
                    }]
483
            staff_client => 1
483
                },
484
        }
484
                acqdate => {
485
                    label => 'acqdate',
486
                    type => 'string',
487
                    opac => 0,
488
                    staff_client => 1,
489
                    mappings => [{
490
                        marc_field => '952d',
491
                        marc_type => 'marc21',
492
                        search => 0,
493
                    }, {
494
                        marc_field => '9955',
495
                        marc_type => 'marc21',
496
                        search => 0,
497
                    }]
498
                },
499
                title => {
500
                    label => 'title',
501
                    type => 'string',
502
                    opac => 0,
503
                    staff_client => 1,
504
                    mappings => [{
505
                        marc_field => '130',
506
                        marc_type => 'marc21'
507
                    }]
508
                },
509
                subject => {
510
                    label => 'subject',
511
                    type => 'string',
512
                    opac => 0,
513
                    staff_client => 1,
514
                    mappings => [{
515
                        marc_field => '600a',
516
                        marc_type => 'marc21'
517
                    }]
518
                }
519
            }
520
        };
485
    });
521
    });
486
522
487
    $db_builder->build({
523
    my $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'biblios' } );
488
        source => 'SearchField',
524
    Koha::SearchFields->search({})->delete;
489
        value => {
525
    Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings();
490
            name    => 'title',
491
            label   => 'title',
492
            weight  => 25,
493
            staff_client => 1
494
        }
495
    });
496
526
497
    $db_builder->build({
527
    my $search_field;
498
        source => 'SearchField',
528
    $search_field = Koha::SearchFields->find({ name => 'title' });
499
        value => {
529
    $search_field->update({ weight => 25.0 });
500
            name    => 'subject',
530
    $search_field = Koha::SearchFields->find({ name => 'subject' });
501
            label   => 'subject',
531
    $search_field->update({ weight => 15.5 });
502
            weight  => 15,
532
    $clear_search_fields_cache->();
503
            staff_client => 1
504
        }
505
    });
506
533
507
    my ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
534
    my ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
508
    undef, undef, undef, { weighted_fields => 1 });
535
    undef, undef, undef, { weighted_fields => 1 });
509
536
510
    my $fields = $query->{query}{query_string}{fields};
537
    my $fields = $query->{query}{query_string}{fields};
511
538
539
    is(@{$fields}, 2, 'Search field with no searchable mappings has been excluded');
540
512
    my @found = grep { $_ eq 'title^25.00' } @{$fields};
541
    my @found = grep { $_ eq 'title^25.00' } @{$fields};
513
    is(@found, 1, 'Search field is title has correct weight'); # Fails
542
    is(@found, 1, 'Search field title has correct weight');
543
544
    @found = grep { $_ eq 'subject^15.50' } @{$fields};
545
    is(@found, 1, 'Search field subject has correct weight');
546
547
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
548
    undef, undef, undef, { weighted_fields => 1, is_opac => 1 });
514
549
515
    @found = grep { $_ eq 'subject^15.00' } @{$fields};
550
    $fields = $query->{query}{query_string}{fields};
516
    is(@found, 1, 'Search field subject has correct weight'); # Fails
551
552
    is_deeply(
553
        $fields,
554
        ['abstract'],
555
        'Only OPAC search fields are used when opac search is performed'
556
    );
517
};
557
};
518
558
519
subtest "_convert_sort_fields() tests" => sub {
559
subtest "_convert_sort_fields() tests" => sub {
520
- 

Return to bug 20589