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

(-)a/Koha/SearchEngine/Elasticsearch.pm (-9 / +44 lines)
Lines 34-39 use Search::Elasticsearch; Link Here
34
use Try::Tiny;
34
use Try::Tiny;
35
use YAML::Syck;
35
use YAML::Syck;
36
36
37
use Koha::Database;
38
37
__PACKAGE__->mk_ro_accessors(qw( index ));
39
__PACKAGE__->mk_ro_accessors(qw( index ));
38
__PACKAGE__->mk_accessors(qw( sort_fields ));
40
__PACKAGE__->mk_accessors(qw( sort_fields ));
39
41
Lines 245-270 sub _get_elasticsearch_mapping { Link Here
245
    return;
247
    return;
246
}
248
}
247
249
248
sub reset_elasticsearch_mappings {
250
sub sync_elasticsearch_mappings {
249
    my ( $reset_fields ) = @_;
251
    my ($self, $options) = @_;
252
    $options //= {};
250
    my $mappings_yaml = C4::Context->config('elasticsearch_index_mappings');
253
    my $mappings_yaml = C4::Context->config('elasticsearch_index_mappings');
251
    $mappings_yaml ||= C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/mappings.yaml';
254
    $mappings_yaml ||= C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/mappings.yaml';
252
    my $indexes = LoadFile( $mappings_yaml );
255
    my $indexes = LoadFile( $mappings_yaml );
253
256
254
    while ( my ( $index_name, $fields ) = each %$indexes ) {
257
    while ( my ( $index_name, $fields ) = each %$indexes ) {
255
        while ( my ( $field_name, $data ) = each %$fields ) {
258
        while ( my ( $field_name, $data ) = each %$fields ) {
256
            my $field_type = $data->{type};
259
            my $search_field = Koha::SearchFields->find({ 'name' => $field_name });
257
            my $field_label = $data->{label};
260
            if ($search_field) {
258
            my $mappings = $data->{mappings};
261
                next if $options->{insert_only};
259
            my $search_field = Koha::SearchFields->find_or_create({ name => $field_name, label => $field_label, type => $field_type }, { key => 'name' });
262
            }
260
            for my $mapping ( @$mappings ) {
263
            else {
261
                my $marc_field = Koha::SearchMarcMaps->find_or_create({ index_name => $index_name, marc_type => $mapping->{marc_type}, marc_field => $mapping->{marc_field} });
264
                my $rs = Koha::SearchFields->_resultset()->create({ name => $field_name, label => $data->{label}, type => $data->{type}});
262
                $search_field->add_to_search_marc_maps($marc_field, { facet => $mapping->{facet} || 0, suggestible => $mapping->{suggestible} || 0, sort => $mapping->{sort} } );
265
                $search_field = Koha::SearchFields->object_class->_new_from_dbic($rs);
266
            }
267
            if ($options->{revert_mappings}) {
268
                # Delete all current marc_targets for field
269
                my $rs = $search_field->_result()->search_marc_maps();
270
                while (my $marc_map = $rs->next) {
271
                    $search_field->_result()->remove_from_search_marc_maps($marc_map);
272
                    # Check if other search fields uses mapping, if not delete
273
                    $marc_map->delete unless (defined $marc_map->search_fields()->first);
274
                }
275
            }
276
            for my $mapping ( @{$data->{mappings}} ) {
277
                my $marc_field = Koha::SearchMarcMaps->find_or_create({
278
                    index_name => $index_name,
279
                    marc_type => $mapping->{marc_type},
280
                    marc_field => $mapping->{marc_field}
281
                });
282
                # If merging mappings relation may already exist, remove to avoid duplicate entry
283
                if(!($options->{revert_mappings} || $options->{insert_only})) {
284
                    $search_field->_result()->remove_from_search_marc_maps($marc_field->_result());
285
                }
286
                $search_field->add_to_search_marc_maps($marc_field, {
287
                    facet => $mapping->{facet} || 0,
288
                    suggestible => $mapping->{suggestible} || 0,
289
                    sort => $mapping->{sort}
290
                });
263
            }
291
            }
264
        }
292
        }
265
    }
293
    }
266
}
294
}
267
295
296
sub reset_elasticsearch_mappings {
297
    my $self = shift;
298
    Koha::SearchFields->search->delete;
299
    Koha::SearchMarcMaps->search->delete;
300
    $self->sync_elasticsearch_mappings();
301
}
302
268
# This overrides the accessor provided by Class::Accessor so that if
303
# This overrides the accessor provided by Class::Accessor so that if
269
# sort_fields isn't set, then it'll generate it.
304
# sort_fields isn't set, then it'll generate it.
270
sub sort_fields {
305
sub sort_fields {
(-)a/admin/searchengine/elasticsearch/mappings.pl (-3 / +21 lines)
Lines 98-112 if ( $op eq 'edit' ) { Link Here
98
    }
98
    }
99
}
99
}
100
elsif( $op eq 'reset_confirmed' ) {
100
elsif( $op eq 'reset_confirmed' ) {
101
    Koha::SearchMarcMaps->delete;
102
    Koha::SearchFields->delete;
103
    Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
101
    Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
104
    push @messages, { type => 'message', code => 'success_on_reset' };
102
    push @messages, { type => 'message', code => 'success_on_reset' };
105
}
103
}
106
elsif( $op eq 'reset_confirm' ) {
104
elsif( $op eq 'reset_confirm' ) {
107
    $template->param( reset_confirm => 1 );
105
    $template->param( reset_confirm => 1 );
108
}
106
}
109
107
elsif( $op eq 'add_confirmed' ) {
108
    Koha::SearchEngine::Elasticsearch->sync_elasticsearch_mappings({ 'insert_only' => 1 });
109
    push @messages, { type => 'message', code => 'success_on_add' };
110
}
111
elsif( $op eq 'add_confirm' ) {
112
    $template->param( add_confirm => 1 );
113
}
114
elsif( $op eq 'revert_confirmed' ) {
115
    Koha::SearchEngine::Elasticsearch->sync_elasticsearch_mappings({ 'revert_mappings' => 1 });
116
    push @messages, { type => 'message', code => 'success_on_revert' };
117
}
118
elsif( $op eq 'revert_confirm' ) {
119
    $template->param( revert_confirm => 1 );
120
}
121
elsif( $op eq 'merge_confirmed' ) {
122
    Koha::SearchEngine::Elasticsearch->sync_elasticsearch_mappings();
123
    push @messages, { type => 'message', code => 'success_on_merge' };
124
}
125
elsif( $op eq 'merge_confirm' ) {
126
    $template->param( merge_confirm => 1 );
127
}
110
128
111
my @indexes;
129
my @indexes;
112
130
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt (-1 / +48 lines)
Lines 84-89 a.add, a.delete { Link Here
84
          Mappings updated successfully.
84
          Mappings updated successfully.
85
        [% CASE 'success_on_reset' %]
85
        [% CASE 'success_on_reset' %]
86
          Mappings have been reset successfully.
86
          Mappings have been reset successfully.
87
        [% CASE 'success_on_add' %]
88
          Mappings have been added successfully.
89
        [% CASE 'success_on_revert' %]
90
          Mappings have been reverted successfully.
91
        [% CASE 'success_on_merge' %]
92
          Mappings have been merged successfully.
87
        [% CASE %]
93
        [% CASE %]
88
          [% m.code | html %]
94
          [% m.code | html %]
89
        [% END %]
95
        [% END %]
Lines 124-129 a.add, a.delete { Link Here
124
            </form>
130
            </form>
125
        </div>
131
        </div>
126
    [% END %]
132
    [% END %]
133
    [% IF add_confirm %]
134
        <div class="dialog alert">
135
            <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>
136
            <form method="post">
137
                <input type="hidden" name="op" value="add_confirmed" />
138
                <button type="submit" class="approve"><i class="fa fa-fw fa-check"></i> Yes, add mappings</button>
139
            </form>
140
            <br>
141
            <form method="post">
142
                <button type="submit" class="deny"><i class="fa fa-fw fa-remove"></i> No, do not add mappings</button>
143
            </form>
144
        </div>
145
    [% END %]
146
    [% IF revert_confirm %]
147
        <div class="dialog alert">
148
            <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>
149
            <form method="post">
150
                <input type="hidden" name="op" value="revert_confirmed" />
151
                <button type="submit" class="approve"><i class="fa fa-fw fa-check"></i> Yes, revert mappings</button>
152
            </form>
153
            <br>
154
            <form method="post">
155
                <button type="submit" class="deny"><i class="fa fa-fw fa-remove"></i> No, do not revert mappings</button>
156
            </form>
157
        </div>
158
    [% END %]
159
    [% IF merge_confirm %]
160
        <div class="dialog alert">
161
            <h3>The current mappings you see on the screen will be preserved but new field mappings in mappings.yaml will be appended.</h3>
162
            <form method="post">
163
                <input type="hidden" name="op" value="merge_confirmed" />
164
                <button type="submit" class="approve"><i class="fa fa-fw fa-check"></i> Yes, merge mappings</button>
165
            </form>
166
            <br>
167
            <form method="post">
168
                <button type="submit" class="deny"><i class="fa fa-fw fa-remove"></i> No, do not merge mappings</button>
169
            </form>
170
        </div>
171
    [% END %]
127
    <form method="post">
172
    <form method="post">
128
        <div id="tabs" class="toptabs" style="clear:both">
173
        <div id="tabs" class="toptabs" style="clear:both">
129
            <ul>
174
            <ul>
Lines 315-320 a.add, a.delete { Link Here
315
        <p>
360
        <p>
316
            <button class="btn btn-default" type="submit" name="op" value="edit"><i class="fa fa-hdd-o" aria-hidden="true"></i> Save</button>
361
            <button class="btn btn-default" type="submit" name="op" value="edit"><i class="fa fa-hdd-o" aria-hidden="true"></i> Save</button>
317
            <button class="btn btn-default" type="submit" name="op" value="reset_confirm"><i class="fa fa-refresh" aria-hidden="true"></i> Reset Mappings</button>
362
            <button class="btn btn-default" type="submit" name="op" value="reset_confirm"><i class="fa fa-refresh" aria-hidden="true"></i> Reset Mappings</button>
363
            <button class="btn btn-default" type="submit" name="op" value="add_confirm"><i class="fa fa-plus" aria-hidden="true"></i> Add Mappings</button>
364
            <button class="btn btn-default" type="submit" name="op" value="revert_confirm"><i class="fa fa-undo" aria-hidden="true"></i> Revert Mappings</button>
365
            <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>
318
        </p>
366
        </p>
319
    </form>
367
    </form>
320
</div>
368
</div>
321
- 

Return to bug 19707