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

(-)a/Koha/SearchEngine/Elasticsearch.pm (-6 / +33 lines)
Lines 45-50 use MIME::Base64; Link Here
45
use Encode qw(encode);
45
use Encode qw(encode);
46
use Business::ISBN;
46
use Business::ISBN;
47
use Scalar::Util qw(looks_like_number);
47
use Scalar::Util qw(looks_like_number);
48
use Koha::Database;
48
49
49
__PACKAGE__->mk_ro_accessors(qw( index index_name ));
50
__PACKAGE__->mk_ro_accessors(qw( index index_name ));
50
__PACKAGE__->mk_accessors(qw( sort_fields ));
51
__PACKAGE__->mk_accessors(qw( sort_fields ));
Lines 346-353 sub _load_elasticsearch_mappings { Link Here
346
    return LoadFile( $mappings_yaml );
347
    return LoadFile( $mappings_yaml );
347
}
348
}
348
349
349
sub reset_elasticsearch_mappings {
350
sub sync_elasticsearch_mappings {
350
    my ( $self ) = @_;
351
    my ($self, $options) = @_;
352
    $options //= {};
351
    my $indexes = $self->_load_elasticsearch_mappings();
353
    my $indexes = $self->_load_elasticsearch_mappings();
352
354
353
    Koha::SearchMarcMaps->delete;
355
    Koha::SearchMarcMaps->delete;
Lines 364-378 sub reset_elasticsearch_mappings { Link Here
364
366
365
            $sf_params{name} = $field_name;
367
            $sf_params{name} = $field_name;
366
368
367
            my $search_field = Koha::SearchFields->find_or_create( \%sf_params, { key => 'name' } );
369
            my $search_field = Koha::SearchFields->find({ 'name' => $field_name });
368
370
            if ($search_field) {
369
            my $mappings = $data->{mappings};
371
                next if $options->{insert_only};
370
            for my $mapping ( @$mappings ) {
372
            }
373
            else {
374
                my $rs = Koha::SearchFields->_resultset()->create(\%sf_params);
375
                $search_field = Koha::SearchFields->object_class->_new_from_dbic($rs);
376
            }
377
            if ($options->{revert_mappings}) {
378
                # Delete all current marc_targets for field
379
                my $rs = $search_field->_result()->search_marc_maps();
380
                while (my $marc_map = $rs->next) {
381
                    $search_field->_result()->remove_from_search_marc_maps($marc_map);
382
                    # Check if other search fields uses mapping, if not delete
383
                    $marc_map->delete unless (defined $marc_map->search_fields()->first);
384
                }
385
            }
386
            for my $mapping ( @{$data->{mappings}} ) {
371
                my $marc_field = Koha::SearchMarcMaps->find_or_create({
387
                my $marc_field = Koha::SearchMarcMaps->find_or_create({
372
                    index_name => $index_name,
388
                    index_name => $index_name,
373
                    marc_type => $mapping->{marc_type},
389
                    marc_type => $mapping->{marc_type},
374
                    marc_field => $mapping->{marc_field}
390
                    marc_field => $mapping->{marc_field}
375
                });
391
                });
392
                # If merging mappings relation may already exist, remove to avoid duplicate entry
393
                if(!($options->{revert_mappings} || $options->{insert_only})) {
394
                    $search_field->_result()->remove_from_search_marc_maps($marc_field->_result());
395
                }
376
                $search_field->add_to_search_marc_maps($marc_field, {
396
                $search_field->add_to_search_marc_maps($marc_field, {
377
                    facet => $mapping->{facet} || 0,
397
                    facet => $mapping->{facet} || 0,
378
                    suggestible => $mapping->{suggestible} || 0,
398
                    suggestible => $mapping->{suggestible} || 0,
Lines 388-393 sub reset_elasticsearch_mappings { Link Here
388
    # FIXME return the mappings?
408
    # FIXME return the mappings?
389
}
409
}
390
410
411
sub reset_elasticsearch_mappings {
412
    my $self = shift;
413
    Koha::SearchFields->search->delete;
414
    Koha::SearchMarcMaps->search->delete;
415
    $self->sync_elasticsearch_mappings();
416
}
417
391
# This overrides the accessor provided by Class::Accessor so that if
418
# This overrides the accessor provided by Class::Accessor so that if
392
# sort_fields isn't set, then it'll generate it.
419
# sort_fields isn't set, then it'll generate it.
393
sub sort_fields {
420
sub sort_fields {
(-)a/admin/searchengine/elasticsearch/mappings.pl (+21 lines)
Lines 177-182 elsif( $op eq 'reset_confirmed' ) { Link Here
177
elsif( $op eq 'reset_confirm' ) {
177
elsif( $op eq 'reset_confirm' ) {
178
    $template->param( reset_confirm => 1 );
178
    $template->param( reset_confirm => 1 );
179
}
179
}
180
elsif( $op eq 'add_confirmed' ) {
181
    Koha::SearchEngine::Elasticsearch->sync_elasticsearch_mappings({ 'insert_only' => 1 });
182
    push @messages, { type => 'message', code => 'success_on_add' };
183
}
184
elsif( $op eq 'add_confirm' ) {
185
    $template->param( add_confirm => 1 );
186
}
187
elsif( $op eq 'revert_confirmed' ) {
188
    Koha::SearchEngine::Elasticsearch->sync_elasticsearch_mappings({ 'revert_mappings' => 1 });
189
    push @messages, { type => 'message', code => 'success_on_revert' };
190
}
191
elsif( $op eq 'revert_confirm' ) {
192
    $template->param( revert_confirm => 1 );
193
}
194
elsif( $op eq 'merge_confirmed' ) {
195
    Koha::SearchEngine::Elasticsearch->sync_elasticsearch_mappings();
196
    push @messages, { type => 'message', code => 'success_on_merge' };
197
}
198
elsif( $op eq 'merge_confirm' ) {
199
    $template->param( merge_confirm => 1 );
200
}
180
201
181
my @indexes;
202
my @indexes;
182
203
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt (-1 / +48 lines)
Lines 99-104 a.add, a.delete { Link Here
99
          [% t("Mappings have been reset successfully.") | $raw %]
99
          [% t("Mappings have been reset successfully.") | $raw %]
100
        [% CASE 'elasticsearch_disabled' %]
100
        [% CASE 'elasticsearch_disabled' %]
101
          [% t("Elasticsearch is currently disabled.") | $raw %]
101
          [% t("Elasticsearch is currently disabled.") | $raw %]
102
        [% CASE 'success_on_add' %]
103
          [% t("Mappings have been added successfully.") | $raw %]
104
        [% CASE 'success_on_revert' %]
105
          [% t("Mappings have been reverted successfully.") | $raw %]
106
        [% CASE 'success_on_merge' %]
107
          [% t("Mappings have been merged successfully.") | $raw %]
102
        [% CASE %]
108
        [% CASE %]
103
          [% m.code | html %]
109
          [% m.code | html %]
104
        [% END %]
110
        [% END %]
Lines 146-151 a.add, a.delete { Link Here
146
            </form>
152
            </form>
147
        </div>
153
        </div>
148
    [% END %]
154
    [% END %]
155
    [% IF add_confirm %]
156
        <div class="dialog alert">
157
            <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>
158
            <form method="post">
159
                <input type="hidden" name="op" value="add_confirmed" />
160
                <button type="submit" class="approve"><i class="fa fa-fw fa-check"></i> Yes, add mappings</button>
161
            </form>
162
            <br>
163
            <form method="post">
164
                <button type="submit" class="deny"><i class="fa fa-fw fa-remove"></i> No, do not add mappings</button>
165
            </form>
166
        </div>
167
    [% END %]
168
    [% IF revert_confirm %]
169
        <div class="dialog alert">
170
            <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>
171
            <form method="post">
172
                <input type="hidden" name="op" value="revert_confirmed" />
173
                <button type="submit" class="approve"><i class="fa fa-fw fa-check"></i> Yes, revert mappings</button>
174
            </form>
175
            <br>
176
            <form method="post">
177
                <button type="submit" class="deny"><i class="fa fa-fw fa-remove"></i> No, do not revert mappings</button>
178
            </form>
179
        </div>
180
    [% END %]
181
    [% IF merge_confirm %]
182
        <div class="dialog alert">
183
            <h3>The current mappings you see on the screen will be preserved but new field mappings in mappings.yaml will be appended.</h3>
184
            <form method="post">
185
                <input type="hidden" name="op" value="merge_confirmed" />
186
                <button type="submit" class="approve"><i class="fa fa-fw fa-check"></i> Yes, merge mappings</button>
187
            </form>
188
            <br>
189
            <form method="post">
190
                <button type="submit" class="deny"><i class="fa fa-fw fa-remove"></i> No, do not merge mappings</button>
191
            </form>
192
        </div>
193
    [% END %]
149
    <form method="post">
194
    <form method="post">
150
        <div id="tabs" class="toptabs" style="clear:both">
195
        <div id="tabs" class="toptabs" style="clear:both">
151
            <ul>
196
            <ul>
Lines 450-455 a.add, a.delete { Link Here
450
        <p>
495
        <p>
451
            <button class="btn btn-default" type="submit" name="op" value="edit"><i class="fa fa-hdd-o" aria-hidden="true"></i> Save</button>
496
            <button class="btn btn-default" type="submit" name="op" value="edit"><i class="fa fa-hdd-o" aria-hidden="true"></i> Save</button>
452
            <button class="btn btn-default" type="submit" name="op" value="reset_confirm"><i class="fa fa-refresh" aria-hidden="true"></i> Reset Mappings</button>
497
            <button class="btn btn-default" type="submit" name="op" value="reset_confirm"><i class="fa fa-refresh" aria-hidden="true"></i> Reset Mappings</button>
498
            <button class="btn btn-default" type="submit" name="op" value="add_confirm"><i class="fa fa-plus" aria-hidden="true"></i> Add Mappings</button>
499
            <button class="btn btn-default" type="submit" name="op" value="revert_confirm"><i class="fa fa-undo" aria-hidden="true"></i> Revert Mappings</button>
500
            <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>
453
        </p>
501
        </p>
454
    </form>
502
    </form>
455
503
456
- 

Return to bug 19707