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

(-)a/Koha/SearchEngine/Elasticsearch.pm (-4 / +16 lines)
Lines 275-285 sub _get_elasticsearch_mapping { Link Here
275
    return;
275
    return;
276
}
276
}
277
277
278
sub reset_elasticsearch_mappings {
278
=head2 _load_elasticsearch_mappings
279
    my ( $reset_fields ) = @_;
279
280
Load Elasticsearch mappings in the format of mappings.yaml.
281
282
$indexes = _load_elasticsearch_mappings();
283
284
=cut
285
286
sub _load_elasticsearch_mappings {
280
    my $mappings_yaml = C4::Context->config('elasticsearch_index_mappings');
287
    my $mappings_yaml = C4::Context->config('elasticsearch_index_mappings');
281
    $mappings_yaml ||= C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/mappings.yaml';
288
    $mappings_yaml ||= C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/mappings.yaml';
282
    my $indexes = LoadFile( $mappings_yaml );
289
    return LoadFile( $mappings_yaml );
290
}
291
292
sub reset_elasticsearch_mappings {
293
    my ( $self ) = @_;
294
    my $indexes = $self->_load_elasticsearch_mappings();
283
295
284
    while ( my ( $index_name, $fields ) = each %$indexes ) {
296
    while ( my ( $index_name, $fields ) = each %$indexes ) {
285
        while ( my ( $field_name, $data ) = each %$fields ) {
297
        while ( my ( $field_name, $data ) = each %$fields ) {
Lines 314-320 sub reset_elasticsearch_mappings { Link Here
314
                    facet => $mapping->{facet} || 0,
326
                    facet => $mapping->{facet} || 0,
315
                    suggestible => $mapping->{suggestible} || 0,
327
                    suggestible => $mapping->{suggestible} || 0,
316
                    sort => $mapping->{sort},
328
                    sort => $mapping->{sort},
317
                    search => $mapping->{search} || 1
329
                    search => $mapping->{search} // 1
318
                });
330
                });
319
            }
331
            }
320
        }
332
        }
(-)a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t (-38 / +77 lines)
Lines 384-437 subtest 'build query from form subtests' => sub { Link Here
384
};
384
};
385
385
386
subtest 'build_query with weighted fields tests' => sub {
386
subtest 'build_query with weighted fields tests' => sub {
387
    plan tests => 2;
387
    plan tests => 4;
388
388
389
    my $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'mydb' } );
389
    $se->mock( '_load_elasticsearch_mappings', sub {
390
    my $db_builder = t::lib::TestBuilder->new();
390
        return {
391
391
            biblios => {
392
    Koha::SearchFields->search({})->delete;
392
                abstract => {
393
    $clear_search_fields_cache->();
393
                    label => 'abstract',
394
394
                    type => 'string',
395
    $db_builder->build({
395
                    opac => 1,
396
        source => 'SearchField',
396
                    staff_client => 0,
397
        value => {
397
                    mappings => [{
398
            name    => 'acqdate',
398
                        marc_field => '520',
399
            label   => 'acqdate',
399
                        marc_type => 'marc21',
400
            weight  => undef,
400
                    }]
401
            staff_client => 1
401
                },
402
        }
402
                acqdate => {
403
                    label => 'acqdate',
404
                    type => 'string',
405
                    opac => 0,
406
                    staff_client => 1,
407
                    mappings => [{
408
                        marc_field => '952d',
409
                        marc_type => 'marc21',
410
                        search => 0,
411
                    }, {
412
                        marc_field => '9955',
413
                        marc_type => 'marc21',
414
                        search => 0,
415
                    }]
416
                },
417
                title => {
418
                    label => 'title',
419
                    type => 'string',
420
                    opac => 0,
421
                    staff_client => 1,
422
                    mappings => [{
423
                        marc_field => '130',
424
                        marc_type => 'marc21'
425
                    }]
426
                },
427
                subject => {
428
                    label => 'subject',
429
                    type => 'string',
430
                    opac => 0,
431
                    staff_client => 1,
432
                    mappings => [{
433
                        marc_field => '600a',
434
                        marc_type => 'marc21'
435
                    }]
436
                }
437
            }
438
        };
403
    });
439
    });
404
440
405
    $db_builder->build({
441
    my $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'biblios' } );
406
        source => 'SearchField',
442
    Koha::SearchFields->search({})->delete;
407
        value => {
443
    Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings();
408
            name    => 'title',
409
            label   => 'title',
410
            weight  => 25,
411
            staff_client => 1
412
        }
413
    });
414
444
415
    $db_builder->build({
445
    my $search_field;
416
        source => 'SearchField',
446
    $search_field = Koha::SearchFields->find({ name => 'title' });
417
        value => {
447
    $search_field->update({ weight => 25.0 });
418
            name    => 'subject',
448
    $search_field = Koha::SearchFields->find({ name => 'subject' });
419
            label   => 'subject',
449
    $search_field->update({ weight => 15.5 });
420
            weight  => 15,
450
    $clear_search_fields_cache->();
421
            staff_client => 1
422
        }
423
    });
424
451
425
    my ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
452
    my ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
426
    undef, undef, undef, { weighted_fields => 1 });
453
    undef, undef, undef, { weighted_fields => 1 });
427
454
428
    my $fields = $query->{query}{query_string}{fields};
455
    my $fields = $query->{query}{query_string}{fields};
429
456
457
    is(@{$fields}, 2, 'Search field with no searchable mappings has been excluded');
458
430
    my @found = grep { $_ eq 'title^25.00' } @{$fields};
459
    my @found = grep { $_ eq 'title^25.00' } @{$fields};
431
    is(@found, 1, 'Search field is title has correct weight'); # Fails
460
    is(@found, 1, 'Search field title has correct weight');
461
462
    @found = grep { $_ eq 'subject^15.50' } @{$fields};
463
    is(@found, 1, 'Search field subject has correct weight');
464
465
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
466
    undef, undef, undef, { weighted_fields => 1, is_opac => 1 });
432
467
433
    @found = grep { $_ eq 'subject^15.00' } @{$fields};
468
    $fields = $query->{query}{query_string}{fields};
434
    is(@found, 1, 'Search field subject has correct weight'); # Fails
469
470
    is_deeply(
471
        $fields,
472
        ['abstract'],
473
        'Only OPAC search fields are used when opac search is performed'
474
    );
435
};
475
};
436
476
437
subtest "_convert_sort_fields() tests" => sub {
477
subtest "_convert_sort_fields() tests" => sub {
438
- 

Return to bug 20589