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

(-)a/Koha/SearchField.pm (+45 lines)
Lines 20-25 use Modern::Perl; Link Here
20
use Carp;
20
use Carp;
21
21
22
use Koha::Database;
22
use Koha::Database;
23
use Koha::SearchMarcMaps;
23
24
24
use base qw(Koha::Object);
25
use base qw(Koha::Object);
25
26
Lines 38-43 sub add_to_search_marc_maps { Link Here
38
    return $self->_result()->add_to_search_marc_maps($search_field->_result, $params);
39
    return $self->_result()->add_to_search_marc_maps($search_field->_result, $params);
39
}
40
}
40
41
42
=head3 search_marc_maps
43
44
my $search_marc_maps = $search_field->search_marc_maps;
45
46
=cut
47
48
sub search_marc_maps {
49
    my ( $self ) = @_;
50
51
    my $marc_type = lc C4::Context->preference('marcflavour');
52
    my @marc_maps = ();
53
54
    my $schema = Koha::Database->new->schema;
55
    my @marc_map_fields = $schema->resultset('SearchMarcToField')->search({
56
        search_field_id => $self->id
57
    });
58
59
    return @marc_maps unless @marc_map_fields;
60
61
    foreach my $marc_field ( @marc_map_fields ) {
62
        my $marc_map = Koha::SearchMarcMaps->find( $marc_field->search_marc_map_id );
63
        push @marc_maps, $marc_map if $marc_map->marc_type eq $marc_type;
64
    }
65
66
    return @marc_maps;
67
68
}
69
70
=head3 is_mapped_biblios
71
72
my $is_mapped_biblios = $search_field->is_mapped_biblios
73
74
=cut
75
76
sub is_mapped_biblios {
77
    my ( $self ) = @_;
78
79
    foreach my $marc_map ( $self->search_marc_maps ) {
80
        return 1 if $marc_map->index_name eq 'biblios';
81
    }
82
83
    return 0;
84
}
85
41
=head3 type
86
=head3 type
42
87
43
=cut
88
=cut
(-)a/admin/searchengine/elasticsearch/mappings.pl (-2 / +12 lines)
Lines 51-56 if ( $op eq 'edit' ) { Link Here
51
    my @field_name = $input->param('search_field_name');
51
    my @field_name = $input->param('search_field_name');
52
    my @field_label = $input->param('search_field_label');
52
    my @field_label = $input->param('search_field_label');
53
    my @field_type = $input->param('search_field_type');
53
    my @field_type = $input->param('search_field_type');
54
    my @field_weight = $input->param('search_field_weight');
54
55
55
    my @index_name          = $input->param('mapping_index_name');
56
    my @index_name          = $input->param('mapping_index_name');
56
    my @search_field_name  = $input->param('mapping_search_field_name');
57
    my @search_field_name  = $input->param('mapping_search_field_name');
Lines 65-73 if ( $op eq 'edit' ) { Link Here
65
            my $field_name = $field_name[$i];
66
            my $field_name = $field_name[$i];
66
            my $field_label = $field_label[$i];
67
            my $field_label = $field_label[$i];
67
            my $field_type = $field_type[$i];
68
            my $field_type = $field_type[$i];
69
            my $field_weight = $field_weight[$i];
70
68
            my $search_field = Koha::SearchFields->find( { name => $field_name }, { key => 'name' } );
71
            my $search_field = Koha::SearchFields->find( { name => $field_name }, { key => 'name' } );
69
            $search_field->label($field_label);
72
            $search_field->label($field_label);
70
            $search_field->type($field_type);
73
            $search_field->type($field_type);
74
            $search_field->weight($field_weight || '');
71
            $search_field->store;
75
            $search_field->store;
72
        }
76
        }
73
77
Lines 133-140 for my $index_name (qw| biblios authorities |) { Link Here
133
    push @indexes, { index_name => $index_name, mappings => \@mappings };
137
    push @indexes, { index_name => $index_name, mappings => \@mappings };
134
}
138
}
135
139
136
my $search_fields = $schema->resultset('SearchField')->search;
140
my $search_fields = Koha::SearchFields->search( {}, { order_by => ['name'] } );
137
my @all_search_fields = $search_fields->search( {}, { order_by => ['name'] } );
141
my @all_search_fields;
142
while ( my $search_field = $search_fields->next ) {
143
    my $search_field_unblessed = $search_field->unblessed;
144
    $search_field_unblessed->{mapped_biblios} = 1 if $search_field->is_mapped_biblios;
145
    push @all_search_fields, $search_field_unblessed;
146
}
147
138
$template->param(
148
$template->param(
139
    indexes           => \@indexes,
149
    indexes           => \@indexes,
140
    all_search_fields => \@all_search_fields,
150
    all_search_fields => \@all_search_fields,
(-)a/installer/data/mysql/atomicupdate/bug_18316_add-weight-column.perl (+9 lines)
Line 0 Link Here
1
$DBversion = 'XXX';
2
if( CheckVersion( $DBversion ) ) {
3
    if( !column_exists( 'search_field', 'weight' ) ) {
4
        $dbh->do( "ALTER TABLE search_field ADD COLUMN weight int" );
5
    }
6
7
    SetVersion( $DBversion );
8
    print "Upgrade to $DBversion done (Bug 18316 - Add column search_field.weight)\n";
9
}
(-)a/installer/data/mysql/kohastructure.sql (+1 lines)
Lines 1460-1465 CREATE TABLE `search_field` ( Link Here
1460
  `name` varchar(255) NOT NULL COMMENT 'the name of the field as it will be stored in the search engine',
1460
  `name` varchar(255) NOT NULL COMMENT 'the name of the field as it will be stored in the search engine',
1461
  `label` varchar(255) NOT NULL COMMENT 'the human readable name of the field, for display',
1461
  `label` varchar(255) NOT NULL COMMENT 'the human readable name of the field, for display',
1462
  `type` ENUM('', 'string', 'date', 'number', 'boolean', 'sum') NOT NULL COMMENT 'what type of data this holds, relevant when storing it in the search engine',
1462
  `type` ENUM('', 'string', 'date', 'number', 'boolean', 'sum') NOT NULL COMMENT 'what type of data this holds, relevant when storing it in the search engine',
1463
  `weight` int default NULL,
1463
  PRIMARY KEY (`id`),
1464
  PRIMARY KEY (`id`),
1464
  UNIQUE KEY (`name` (191))
1465
  UNIQUE KEY (`name` (191))
1465
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
1466
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt (+19 lines)
Lines 89-94 a.add, a.delete { Link Here
89
    <h1>Search engine configuration</h1>
89
    <h1>Search engine configuration</h1>
90
    <div class="warning">
90
    <div class="warning">
91
        Warning: Any changes to the configuration will only take effect after a full reindex. Until then searching may not work correctly.
91
        Warning: Any changes to the configuration will only take effect after a full reindex. Until then searching may not work correctly.
92
93
        <p>Weight: define weight between 1 and 99.</p>
94
        <ol>
95
          <li>only search fields mapped with biblios can be weighted</li>
96
          <li>search will be done on weighted fields only</li>
97
          <li>if no field is weighted, search will be done on all the record</li>
98
        </ol>
92
    </div>
99
    </div>
93
    [% IF errors %]
100
    [% IF errors %]
94
        <div class="error">
101
        <div class="error">
Lines 126-131 a.add, a.delete { Link Here
126
                    <th>Name</th>
133
                    <th>Name</th>
127
                    <th>Label</th>
134
                    <th>Label</th>
128
                    <th>Type</th>
135
                    <th>Type</th>
136
                    <th>Weight</th>
129
                  </tr>
137
                  </tr>
130
                </thead>
138
                </thead>
131
                <tbody>
139
                <tbody>
Lines 165-170 a.add, a.delete { Link Here
165
                          [% END %]
173
                          [% END %]
166
                        </select>
174
                        </select>
167
                      </td>
175
                      </td>
176
                      [% IF search_field.mapped_biblios %]
177
                        <td>
178
                          [% IF search_field.weight %]
179
                            <input type="number" name="search_field_weight" value="[% search_field.weight %]" min="1" max="99" />
180
                          [% ELSE %]
181
                            <input type="number" name="search_field_weight" min="1" max="99" />
182
                          [% END %]
183
                        </td>
184
                      [% ELSE %]
185
                        <td><input type="hidden" name="search_field_weight" value=""></td>
186
                      [% END %]
168
                    </tr>
187
                    </tr>
169
                  [% END %]
188
                  [% END %]
170
                </tbody>
189
                </tbody>
(-)a/t/db_dependent/Koha/SearchField.t (-1 / +135 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2018 Biblibre
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 8;
23
24
use Koha::Database;
25
use Koha::SearchFields;
26
27
use t::lib::Mocks;
28
use t::lib::TestBuilder;
29
30
my $schema = Koha::Database->new->schema;
31
$schema->storage->txn_begin;
32
33
my $builder = t::lib::TestBuilder->new;
34
35
t::lib::Mocks::mock_preference('marcflavour', 'marc21');
36
37
my $sf = $builder->build({
38
    source => 'SearchField',
39
});
40
41
my $smm = $builder->build({
42
    source => 'SearchMarcMap',
43
    value => {
44
        index_name  => 'biblios',
45
        marc_type   => 'marc21',
46
        marc_field  => '410abcdef'
47
    }
48
});
49
50
my $smtf = $builder->build({
51
    source => 'SearchMarcToField',
52
    value => {
53
        search_marc_map_id  => $smm->{id},
54
        search_field_id     => $sf->{id}
55
    }
56
});
57
58
my $smm2 = $builder->build({
59
    source => 'SearchMarcMap',
60
    value => {
61
        index_name  => 'biblios',
62
        marc_type   => 'unimarc',
63
        marc_field  => '410abcdef'
64
    }
65
});
66
67
my $smtf2 = $builder->build({
68
    source => 'SearchMarcToField',
69
    value => {
70
        search_marc_map_id  => $smm2->{id},
71
        search_field_id     => $sf->{id}
72
    }
73
});
74
75
my $search_field = Koha::SearchFields->find($sf->{id});
76
my @marc_map = $search_field->search_marc_maps;
77
is(scalar(@marc_map), 1, 'search_marc_maps should return 1 marc map');
78
is($marc_map[0]->index_name, 'biblios', 'Marc map index name is biblios');
79
is($marc_map[0]->marc_type, 'marc21', 'Marc map type is marc21');
80
is($marc_map[0]->marc_field, '410abcdef', 'Marc map field is 410abcdef');
81
82
ok($search_field->is_mapped_biblios, 'Search field 1 is mapped with biblios');
83
84
my $sf2 = $builder->build({
85
    source => 'SearchField',
86
});
87
88
my $smm3 = $builder->build({
89
    source => 'SearchMarcMap',
90
    value => {
91
        index_name  => 'authorities',
92
        marc_type   => 'marc21',
93
        marc_field  => '700a'
94
    }
95
});
96
97
my $smtf3 = $builder->build({
98
    source => 'SearchMarcToField',
99
    value => {
100
        search_marc_map_id  => $smm3->{id},
101
        search_field_id     => $sf2->{id}
102
    }
103
});
104
105
$search_field = Koha::SearchFields->find($sf2->{id});
106
ok(!$search_field->is_mapped_biblios, 'Search field is mapped with authorities only');
107
108
my $smm4 = $builder->build({
109
    source => 'SearchMarcMap',
110
    value => {
111
        index_name  => 'biblios',
112
        marc_type   => 'marc21',
113
        marc_field  => '200a'
114
    }
115
});
116
117
my $smtf4 = $builder->build({
118
    source => 'SearchMarcToField',
119
    value => {
120
        search_marc_map_id  => $smm4->{id},
121
        search_field_id     => $sf2->{id}
122
    }
123
});
124
125
$search_field = Koha::SearchFields->find($sf2->{id});
126
ok($search_field->is_mapped_biblios, 'Search field is mapped with authorities and biblios');
127
128
my $sf3 = $builder->build({
129
    source => 'SearchField',
130
});
131
132
$search_field = Koha::SearchFields->find($sf3->{id});
133
ok(!$search_field->is_mapped_biblios, 'Search field is not mapped');
134
135
$schema->storage->txn_rollback;

Return to bug 18316