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') . '/etc/searchengine/elasticsearch/mappings.yaml';
254
    $mappings_yaml ||= C4::Context->config('intranetdir') . '/etc/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 (-4 / +16 lines)
Lines 99-110 if ( $op eq 'edit' ) { Link Here
99
}
99
}
100
elsif( $op eq 'reset' ) {
100
elsif( $op eq 'reset' ) {
101
    # TODO Move this feature to the interface
101
    # TODO Move this feature to the interface
102
    my $sure = $input->param('i_know_what_i_am_doing');
102
    if ( $input->param('i_know_what_i_am_doing') ) {
103
    if ( $sure ) {
104
        Koha::SearchMarcMaps->search->delete;
105
        Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
103
        Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
106
    }
104
    }
107
}
105
}
106
elsif( $op eq 'add' ) {
107
    if ( $input->param('i_know_what_i_am_doing') ) {
108
        Koha::SearchEngine::Elasticsearch->sync_elasticsearch_mappings({ 'insert_only' => 1 });
109
    }
110
}
111
elsif( $op eq 'revert' ) {
112
    if ( $input->param('i_know_what_i_am_doing') ) {
113
        Koha::SearchEngine::Elasticsearch->sync_elasticsearch_mappings({ 'revert_mappings' => 1 });
114
    }
115
}
116
elsif( $op eq 'merge' ) {
117
    if ( $input->param('i_know_what_i_am_doing') ) {
118
        Koha::SearchEngine::Elasticsearch->sync_elasticsearch_mappings();
119
    }
120
}
108
121
109
my @indexes;
122
my @indexes;
110
123
111
- 

Return to bug 19707