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

(-)a/Koha/SearchEngine/Elasticsearch.pm (-9 / +43 lines)
Lines 39-44 use MARC::File::XML; Link Here
39
use MIME::Base64;
39
use MIME::Base64;
40
use Encode qw(encode);
40
use Encode qw(encode);
41
use Business::ISBN;
41
use Business::ISBN;
42
use Koha::Database;
42
43
43
__PACKAGE__->mk_ro_accessors(qw( index ));
44
__PACKAGE__->mk_ro_accessors(qw( index ));
44
__PACKAGE__->mk_accessors(qw( sort_fields ));
45
__PACKAGE__->mk_accessors(qw( sort_fields ));
Lines 274-299 sub _get_elasticsearch_mapping { Link Here
274
    return;
275
    return;
275
}
276
}
276
277
277
sub reset_elasticsearch_mappings {
278
sub sync_elasticsearch_mappings {
278
    my ( $reset_fields ) = @_;
279
    my ($self, $options) = @_;
280
    $options //= {};
279
    my $mappings_yaml = C4::Context->config('elasticsearch_index_mappings');
281
    my $mappings_yaml = C4::Context->config('elasticsearch_index_mappings');
280
    $mappings_yaml ||= C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/mappings.yaml';
282
    $mappings_yaml ||= C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/mappings.yaml';
281
    my $indexes = LoadFile( $mappings_yaml );
283
    my $indexes = LoadFile( $mappings_yaml );
282
284
283
    while ( my ( $index_name, $fields ) = each %$indexes ) {
285
    while ( my ( $index_name, $fields ) = each %$indexes ) {
284
        while ( my ( $field_name, $data ) = each %$fields ) {
286
        while ( my ( $field_name, $data ) = each %$fields ) {
285
            my $field_type = $data->{type};
287
            my $search_field = Koha::SearchFields->find({ 'name' => $field_name });
286
            my $field_label = $data->{label};
288
            if ($search_field) {
287
            my $mappings = $data->{mappings};
289
                next if $options->{insert_only};
288
            my $search_field = Koha::SearchFields->find_or_create({ name => $field_name, label => $field_label, type => $field_type }, { key => 'name' });
290
            }
289
            for my $mapping ( @$mappings ) {
291
            else {
290
                my $marc_field = Koha::SearchMarcMaps->find_or_create({ index_name => $index_name, marc_type => $mapping->{marc_type}, marc_field => $mapping->{marc_field} });
292
                my $rs = Koha::SearchFields->_resultset()->create({ name => $field_name, label => $data->{label}, type => $data->{type}});
291
                $search_field->add_to_search_marc_maps($marc_field, { facet => $mapping->{facet} || 0, suggestible => $mapping->{suggestible} || 0, sort => $mapping->{sort} } );
293
                $search_field = Koha::SearchFields->object_class->_new_from_dbic($rs);
294
            }
295
            if ($options->{revert_mappings}) {
296
                # Delete all current marc_targets for field
297
                my $rs = $search_field->_result()->search_marc_maps();
298
                while (my $marc_map = $rs->next) {
299
                    $search_field->_result()->remove_from_search_marc_maps($marc_map);
300
                    # Check if other search fields uses mapping, if not delete
301
                    $marc_map->delete unless (defined $marc_map->search_fields()->first);
302
                }
303
            }
304
            for my $mapping ( @{$data->{mappings}} ) {
305
                my $marc_field = Koha::SearchMarcMaps->find_or_create({
306
                    index_name => $index_name,
307
                    marc_type => $mapping->{marc_type},
308
                    marc_field => $mapping->{marc_field}
309
                });
310
                # If merging mappings relation may already exist, remove to avoid duplicate entry
311
                if(!($options->{revert_mappings} || $options->{insert_only})) {
312
                    $search_field->_result()->remove_from_search_marc_maps($marc_field->_result());
313
                }
314
                $search_field->add_to_search_marc_maps($marc_field, {
315
                    facet => $mapping->{facet} || 0,
316
                    suggestible => $mapping->{suggestible} || 0,
317
                    sort => $mapping->{sort}
318
                });
292
            }
319
            }
293
        }
320
        }
294
    }
321
    }
295
}
322
}
296
323
324
sub reset_elasticsearch_mappings {
325
    my $self = shift;
326
    Koha::SearchFields->search->delete;
327
    Koha::SearchMarcMaps->search->delete;
328
    $self->sync_elasticsearch_mappings();
329
}
330
297
# This overrides the accessor provided by Class::Accessor so that if
331
# This overrides the accessor provided by Class::Accessor so that if
298
# sort_fields isn't set, then it'll generate it.
332
# sort_fields isn't set, then it'll generate it.
299
sub sort_fields {
333
sub sort_fields {
(-)a/admin/searchengine/elasticsearch/mappings.pl (-3 / +21 lines)
Lines 136-150 if ( $op eq 'edit' ) { Link Here
136
    }
136
    }
137
}
137
}
138
elsif( $op eq 'reset_confirmed' ) {
138
elsif( $op eq 'reset_confirmed' ) {
139
    Koha::SearchMarcMaps->delete;
140
    Koha::SearchFields->delete;
141
    Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
139
    Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
142
    push @messages, { type => 'message', code => 'success_on_reset' };
140
    push @messages, { type => 'message', code => 'success_on_reset' };
143
}
141
}
144
elsif( $op eq 'reset_confirm' ) {
142
elsif( $op eq 'reset_confirm' ) {
145
    $template->param( reset_confirm => 1 );
143
    $template->param( reset_confirm => 1 );
146
}
144
}
147
145
elsif( $op eq 'add_confirmed' ) {
146
    Koha::SearchEngine::Elasticsearch->sync_elasticsearch_mappings({ 'insert_only' => 1 });
147
    push @messages, { type => 'message', code => 'success_on_add' };
148
}
149
elsif( $op eq 'add_confirm' ) {
150
    $template->param( add_confirm => 1 );
151
}
152
elsif( $op eq 'revert_confirmed' ) {
153
    Koha::SearchEngine::Elasticsearch->sync_elasticsearch_mappings({ 'revert_mappings' => 1 });
154
    push @messages, { type => 'message', code => 'success_on_revert' };
155
}
156
elsif( $op eq 'revert_confirm' ) {
157
    $template->param( revert_confirm => 1 );
158
}
159
elsif( $op eq 'merge_confirmed' ) {
160
    Koha::SearchEngine::Elasticsearch->sync_elasticsearch_mappings();
161
    push @messages, { type => 'message', code => 'success_on_merge' };
162
}
163
elsif( $op eq 'merge_confirm' ) {
164
    $template->param( merge_confirm => 1 );
165
}
148
166
149
my @indexes;
167
my @indexes;
150
168
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt (-1 / +48 lines)
Lines 91-96 a.add, a.delete { Link Here
91
          Mappings updated successfully.
91
          Mappings updated successfully.
92
        [% CASE 'success_on_reset' %]
92
        [% CASE 'success_on_reset' %]
93
          Mappings have been reset successfully.
93
          Mappings have been reset successfully.
94
        [% CASE 'success_on_add' %]
95
          Mappings have been added successfully.
96
        [% CASE 'success_on_revert' %]
97
          Mappings have been reverted successfully.
98
        [% CASE 'success_on_merge' %]
99
          Mappings have been merged successfully.
94
        [% CASE %]
100
        [% CASE %]
95
          [% m.code | html %]
101
          [% m.code | html %]
96
        [% END %]
102
        [% END %]
Lines 138-143 a.add, a.delete { Link Here
138
            </form>
144
            </form>
139
        </div>
145
        </div>
140
    [% END %]
146
    [% END %]
147
    [% IF add_confirm %]
148
        <div class="dialog alert">
149
            <h3>The current mappings you see on the screen will be presevred and mappings for new fields found in mappings.yam file will be added.</h3>
150
            <form method="post">
151
                <input type="hidden" name="op" value="add_confirmed" />
152
                <button type="submit" class="approve"><i class="fa fa-fw fa-check"></i> Yes, add mappings</button>
153
            </form>
154
            <br>
155
            <form method="post">
156
                <button type="submit" class="deny"><i class="fa fa-fw fa-remove"></i> No, do not add mappings</button>
157
            </form>
158
        </div>
159
    [% END %]
160
    [% IF revert_confirm %]
161
        <div class="dialog alert">
162
            <h3>The current mappings you see on the screen will be reset to defaults if the field is also found in the mappings.yaml file.</h3>
163
            <form method="post">
164
                <input type="hidden" name="op" value="revert_confirmed" />
165
                <button type="submit" class="approve"><i class="fa fa-fw fa-check"></i> Yes, revert mappings</button>
166
            </form>
167
            <br>
168
            <form method="post">
169
                <button type="submit" class="deny"><i class="fa fa-fw fa-remove"></i> No, do not revert mappings</button>
170
            </form>
171
        </div>
172
    [% END %]
173
    [% IF merge_confirm %]
174
        <div class="dialog alert">
175
            <h3>The current mappings you see on the screen will be preserved but new field mappings in mappings.yaml will be appended.</h3>
176
            <form method="post">
177
                <input type="hidden" name="op" value="merge_confirmed" />
178
                <button type="submit" class="approve"><i class="fa fa-fw fa-check"></i> Yes, merge mappings</button>
179
            </form>
180
            <br>
181
            <form method="post">
182
                <button type="submit" class="deny"><i class="fa fa-fw fa-remove"></i> No, do not merge mappings</button>
183
            </form>
184
        </div>
185
    [% END %]
141
    <form method="post">
186
    <form method="post">
142
        <div id="tabs" class="toptabs" style="clear:both">
187
        <div id="tabs" class="toptabs" style="clear:both">
143
            <ul>
188
            <ul>
Lines 337-342 a.add, a.delete { Link Here
337
        <p>
382
        <p>
338
            <button class="btn btn-default" type="submit" name="op" value="edit"><i class="fa fa-hdd-o" aria-hidden="true"></i> Save</button>
383
            <button class="btn btn-default" type="submit" name="op" value="edit"><i class="fa fa-hdd-o" aria-hidden="true"></i> Save</button>
339
            <button class="btn btn-default" type="submit" name="op" value="reset_confirm"><i class="fa fa-refresh" aria-hidden="true"></i> Reset Mappings</button>
384
            <button class="btn btn-default" type="submit" name="op" value="reset_confirm"><i class="fa fa-refresh" aria-hidden="true"></i> Reset Mappings</button>
385
            <button class="btn btn-default" type="submit" name="op" value="add_confirm"><i class="fa fa-plus" aria-hidden="true"></i> Add Mappings</button>
386
            <button class="btn btn-default" type="submit" name="op" value="revert_confirm"><i class="fa fa-undo" aria-hidden="true"></i> Revert Mappings</button>
387
            <button class="btn btn-default" type="submit" name="op" value="merge_confirm"></i> <i class="fa fa-plus-square" aria-hidden="true"></i> Merge Mappings</button>
340
        </p>
388
        </p>
341
    </form>
389
    </form>
342
390
343
- 

Return to bug 19707