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

(-)a/Koha/SearchEngine/Elasticsearch.pm (-8 / +43 lines)
Lines 35-40 use Try::Tiny; Link Here
35
use YAML::Syck;
35
use YAML::Syck;
36
36
37
use Data::Dumper;    # TODO remove
37
use Data::Dumper;    # TODO remove
38
use Koha::Database;
38
39
39
__PACKAGE__->mk_ro_accessors(qw( index ));
40
__PACKAGE__->mk_ro_accessors(qw( index ));
40
__PACKAGE__->mk_accessors(qw( sort_fields ));
41
__PACKAGE__->mk_accessors(qw( sort_fields ));
Lines 278-301 sub _elasticsearch_mapping_for_default { Link Here
278
    };
279
    };
279
}
280
}
280
281
281
sub reset_elasticsearch_mappings {
282
sub sync_elasticsearch_mappings {
283
    my ($self, $options) = @_;
284
    $options //= {};
282
    my $mappings_yaml = C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/mappings.yaml';
285
    my $mappings_yaml = C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/mappings.yaml';
283
    my $indexes = LoadFile( $mappings_yaml );
286
    my $indexes = LoadFile( $mappings_yaml );
284
287
285
    while ( my ( $index_name, $fields ) = each %$indexes ) {
288
    while ( my ( $index_name, $fields ) = each %$indexes ) {
286
        while ( my ( $field_name, $data ) = each %$fields ) {
289
        while ( my ( $field_name, $data ) = each %$fields ) {
287
            my $field_type = $data->{type};
290
            my $search_field = Koha::SearchFields->find({ 'name' => $field_name });
288
            my $field_label = $data->{label};
291
            if ($search_field) {
289
            my $mappings = $data->{mappings};
292
                next if $options->{insert_only};
290
            my $search_field = Koha::SearchFields->find_or_create({ name => $field_name, label => $field_label, type => $field_type }, { key => 'name' });
293
            }
291
            for my $mapping ( @$mappings ) {
294
            else {
292
                my $marc_field = Koha::SearchMarcMaps->find_or_create({ index_name => $index_name, marc_type => $mapping->{marc_type}, marc_field => $mapping->{marc_field} });
295
                my $rs = Koha::SearchFields->_resultset()->create({ name => $field_name, label => $data->{label}, type => $data->{type}});
293
                $search_field->add_to_search_marc_maps($marc_field, { facet => $mapping->{facet} || 0, suggestible => $mapping->{suggestible} || 0, sort => $mapping->{sort} } );
296
                $search_field = Koha::SearchFields->object_class->_new_from_dbic($rs);
297
            }
298
            if ($options->{revert_mappings}) {
299
                # Delete all current marc_targets for field
300
                my $rs = $search_field->_result()->search_marc_maps();
301
                while (my $marc_map = $rs->next) {
302
                    $search_field->_result()->remove_from_search_marc_maps($marc_map);
303
                    # Check if other search fields uses mapping, if not delete
304
                    $marc_map->delete unless (defined $marc_map->search_fields()->first);
305
                }
306
            }
307
            for my $mapping ( @{$data->{mappings}} ) {
308
                my $marc_field = Koha::SearchMarcMaps->find_or_create({
309
                    index_name => $index_name,
310
                    marc_type => $mapping->{marc_type},
311
                    marc_field => $mapping->{marc_field}
312
                });
313
                # If merging mappings relation may already exist, remove to avoid duplicate entry
314
                if(!($options->{revert_mappings} || $options->{insert_only})) {
315
                    $search_field->_result()->remove_from_search_marc_maps($marc_field->_result());
316
                }
317
                $search_field->add_to_search_marc_maps($marc_field, {
318
                    facet => $mapping->{facet} || 0,
319
                    suggestible => $mapping->{suggestible} || 0,
320
                    sort => $mapping->{sort}
321
                });
294
            }
322
            }
295
        }
323
        }
296
    }
324
    }
297
}
325
}
298
326
327
sub reset_elasticsearch_mappings {
328
    my $self = shift;
329
    Koha::SearchFields->search->delete;
330
    Koha::SearchMarcMaps->search->delete;
331
    $self->sync_elasticsearch_mappings();
332
}
333
299
# This overrides the accessor provided by Class::Accessor so that if
334
# This overrides the accessor provided by Class::Accessor so that if
300
# sort_fields isn't set, then it'll generate it.
335
# sort_fields isn't set, then it'll generate it.
301
sub sort_fields {
336
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