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

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