@@ -, +, @@ - apply this patch, - update schema file (perl misc/devel/update_dbix_class_files.pl), - go to Administration > Search engine configuration, - in search fields tab, define weights for some fields and save, - check weights has been saved, - reset some weights (empty) and save, - check weights has been reset --- Koha/SearchField.pm | 45 +++++++ admin/searchengine/elasticsearch/mappings.pl | 14 ++- .../atomicupdate/bug_18316_add-weight-column.perl | 9 ++ installer/data/mysql/kohastructure.sql | 1 + .../admin/searchengine/elasticsearch/mappings.tt | 19 +++ t/db_dependent/Koha/SearchField.t | 135 +++++++++++++++++++++ 6 files changed, 221 insertions(+), 2 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_18316_add-weight-column.perl create mode 100644 t/db_dependent/Koha/SearchField.t --- a/Koha/SearchField.pm +++ a/Koha/SearchField.pm @@ -20,6 +20,7 @@ use Modern::Perl; use Carp; use Koha::Database; +use Koha::SearchMarcMaps; use base qw(Koha::Object); @@ -38,6 +39,50 @@ sub add_to_search_marc_maps { return $self->_result()->add_to_search_marc_maps($search_field->_result, $params); } +=head3 search_marc_maps + +my $search_marc_maps = $search_field->search_marc_maps; + +=cut + +sub search_marc_maps { + my ( $self ) = @_; + + my $marc_type = lc C4::Context->preference('marcflavour'); + my @marc_maps = (); + + my $schema = Koha::Database->new->schema; + my @marc_map_fields = $schema->resultset('SearchMarcToField')->search({ + search_field_id => $self->id + }); + + return @marc_maps unless @marc_map_fields; + + foreach my $marc_field ( @marc_map_fields ) { + my $marc_map = Koha::SearchMarcMaps->find( $marc_field->search_marc_map_id ); + push @marc_maps, $marc_map if $marc_map->marc_type eq $marc_type; + } + + return @marc_maps; + +} + +=head3 is_mapped_biblios + +my $is_mapped_biblios = $search_field->is_mapped_biblios + +=cut + +sub is_mapped_biblios { + my ( $self ) = @_; + + foreach my $marc_map ( $self->search_marc_maps ) { + return 1 if $marc_map->index_name eq 'biblios'; + } + + return 0; +} + =head3 type =cut --- a/admin/searchengine/elasticsearch/mappings.pl +++ a/admin/searchengine/elasticsearch/mappings.pl @@ -51,6 +51,7 @@ if ( $op eq 'edit' ) { my @field_name = $input->param('search_field_name'); my @field_label = $input->param('search_field_label'); my @field_type = $input->param('search_field_type'); + my @field_weight = $input->param('search_field_weight'); my @index_name = $input->param('mapping_index_name'); my @search_field_name = $input->param('mapping_search_field_name'); @@ -65,9 +66,12 @@ if ( $op eq 'edit' ) { my $field_name = $field_name[$i]; my $field_label = $field_label[$i]; my $field_type = $field_type[$i]; + my $field_weight = $field_weight[$i]; + my $search_field = Koha::SearchFields->find( { name => $field_name }, { key => 'name' } ); $search_field->label($field_label); $search_field->type($field_type); + $search_field->weight($field_weight || ''); $search_field->store; } @@ -136,8 +140,14 @@ for my $index_name (qw| biblios authorities |) { push @indexes, { index_name => $index_name, mappings => \@mappings }; } -my $search_fields = $schema->resultset('SearchField')->search; -my @all_search_fields = $search_fields->search( {}, { order_by => ['name'] } ); +my $search_fields = Koha::SearchFields->search( {}, { order_by => ['name'] } ); +my @all_search_fields; +while ( my $search_field = $search_fields->next ) { + my $search_field_unblessed = $search_field->unblessed; + $search_field_unblessed->{mapped_biblios} = 1 if $search_field->is_mapped_biblios; + push @all_search_fields, $search_field_unblessed; +} + $template->param( indexes => \@indexes, all_search_fields => \@all_search_fields, --- a/installer/data/mysql/atomicupdate/bug_18316_add-weight-column.perl +++ a/installer/data/mysql/atomicupdate/bug_18316_add-weight-column.perl @@ -0,0 +1,9 @@ +$DBversion = 'XXX'; +if( CheckVersion( $DBversion ) ) { + if( !column_exists( 'search_field', 'weight' ) ) { + $dbh->do( "ALTER TABLE search_field ADD COLUMN weight int" ); + } + + SetVersion( $DBversion ); + print "Upgrade to $DBversion done (Bug 18316 - Add column search_field.weight)\n"; +} --- a/installer/data/mysql/kohastructure.sql +++ a/installer/data/mysql/kohastructure.sql @@ -1462,6 +1462,7 @@ CREATE TABLE `search_field` ( `name` varchar(255) NOT NULL COMMENT 'the name of the field as it will be stored in the search engine', `label` varchar(255) NOT NULL COMMENT 'the human readable name of the field, for display', `type` ENUM('', 'string', 'date', 'number', 'boolean', 'sum', 'isbn', 'stdno') NOT NULL COMMENT 'what type of data this holds, relevant when storing it in the search engine', + `weight` int default NULL, PRIMARY KEY (`id`), UNIQUE KEY (`name` (191)) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt @@ -93,6 +93,13 @@ a.add, a.delete {
Weight: define weight between 1 and 99.
+