From 1b902612af69dadf4c13deb8bc6c59950eb493e3 Mon Sep 17 00:00:00 2001 From: David Gustafsson Date: Thu, 4 Apr 2019 15:14:19 +0200 Subject: [PATCH] Bug 20589: fix QueryBuilder tests Signed-off-by: Nick Clemens Signed-off-by: Alex Arnaud Signed-off-by: Martin Renvoize --- Koha/SearchEngine/Elasticsearch.pm | 20 ++- .../SearchEngine/Elasticsearch/QueryBuilder.t | 114 ++++++++++++------ 2 files changed, 93 insertions(+), 41 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch.pm b/Koha/SearchEngine/Elasticsearch.pm index 9cf776d140..75bf582b7d 100644 --- a/Koha/SearchEngine/Elasticsearch.pm +++ b/Koha/SearchEngine/Elasticsearch.pm @@ -277,11 +277,23 @@ sub _get_elasticsearch_field_config { return; } -sub reset_elasticsearch_mappings { - my ( $reset_fields ) = @_; +=head2 _load_elasticsearch_mappings + +Load Elasticsearch mappings in the format of mappings.yaml. + +$indexes = _load_elasticsearch_mappings(); + +=cut + +sub _load_elasticsearch_mappings { my $mappings_yaml = C4::Context->config('elasticsearch_index_mappings'); $mappings_yaml ||= C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/mappings.yaml'; - my $indexes = LoadFile( $mappings_yaml ); + return LoadFile( $mappings_yaml ); +} + +sub reset_elasticsearch_mappings { + my ( $self ) = @_; + my $indexes = $self->_load_elasticsearch_mappings(); while ( my ( $index_name, $fields ) = each %$indexes ) { while ( my ( $field_name, $data ) = each %$fields ) { @@ -307,7 +319,7 @@ sub reset_elasticsearch_mappings { facet => $mapping->{facet} || 0, suggestible => $mapping->{suggestible} || 0, sort => $mapping->{sort}, - search => $mapping->{search} || 1 + search => $mapping->{search} // 1 }); } } diff --git a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t index 78fb5d3696..488e4ac80c 100644 --- a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t +++ b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t @@ -466,54 +466,94 @@ subtest 'build query from form subtests' => sub { }; subtest 'build_query with weighted fields tests' => sub { - plan tests => 2; - - my $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'mydb' } ); - my $db_builder = t::lib::TestBuilder->new(); - - Koha::SearchFields->search({})->delete; - $clear_search_fields_cache->(); - - $db_builder->build({ - source => 'SearchField', - value => { - name => 'acqdate', - label => 'acqdate', - weight => undef, - staff_client => 1 - } + plan tests => 4; + + $se->mock( '_load_elasticsearch_mappings', sub { + return { + biblios => { + abstract => { + label => 'abstract', + type => 'string', + opac => 1, + staff_client => 0, + mappings => [{ + marc_field => '520', + marc_type => 'marc21', + }] + }, + acqdate => { + label => 'acqdate', + type => 'string', + opac => 0, + staff_client => 1, + mappings => [{ + marc_field => '952d', + marc_type => 'marc21', + search => 0, + }, { + marc_field => '9955', + marc_type => 'marc21', + search => 0, + }] + }, + title => { + label => 'title', + type => 'string', + opac => 0, + staff_client => 1, + mappings => [{ + marc_field => '130', + marc_type => 'marc21' + }] + }, + subject => { + label => 'subject', + type => 'string', + opac => 0, + staff_client => 1, + mappings => [{ + marc_field => '600a', + marc_type => 'marc21' + }] + } + } + }; }); - $db_builder->build({ - source => 'SearchField', - value => { - name => 'title', - label => 'title', - weight => 25, - staff_client => 1 - } - }); + my $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'biblios' } ); + Koha::SearchFields->search({})->delete; + Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings(); - $db_builder->build({ - source => 'SearchField', - value => { - name => 'subject', - label => 'subject', - weight => 15, - staff_client => 1 - } - }); + my $search_field; + $search_field = Koha::SearchFields->find({ name => 'title' }); + $search_field->update({ weight => 25.0 }); + $search_field = Koha::SearchFields->find({ name => 'subject' }); + $search_field->update({ weight => 15.5 }); + $clear_search_fields_cache->(); my ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { weighted_fields => 1 }); my $fields = $query->{query}{query_string}{fields}; + is(@{$fields}, 2, 'Search field with no searchable mappings has been excluded'); + my @found = grep { $_ eq 'title^25.00' } @{$fields}; - is(@found, 1, 'Search field is title has correct weight'); # Fails + is(@found, 1, 'Search field title has correct weight'); + + @found = grep { $_ eq 'subject^15.50' } @{$fields}; + is(@found, 1, 'Search field subject has correct weight'); + + ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, + undef, undef, undef, { weighted_fields => 1, is_opac => 1 }); - @found = grep { $_ eq 'subject^15.00' } @{$fields}; - is(@found, 1, 'Search field subject has correct weight'); # Fails + $fields = $query->{query}{query_string}{fields}; + + is_deeply( + $fields, + ['abstract'], + 'Only OPAC search fields are used when opac search is performed' + ); }; subtest "_convert_sort_fields() tests" => sub { -- 2.39.5