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

(-)a/Koha/SearchEngine/Elasticsearch.pm (-2 / +59 lines)
Lines 37-43 __PACKAGE__->mk_ro_accessors(qw( index )); Link Here
37
__PACKAGE__->mk_accessors(qw( sort_fields ));
37
__PACKAGE__->mk_accessors(qw( sort_fields ));
38
38
39
# Constants to refer to the standard index names
39
# Constants to refer to the standard index names
40
Readonly our $BIBLIOS_INDEX     => 'biblios';
40
Readonly our $BIBLIOS_INDEX     => 'biblios'; #These are going to be the suffixes for our aliases - most access to indexes will be via alias
41
Readonly our $AUTHORITIES_INDEX => 'authorities';
41
Readonly our $AUTHORITIES_INDEX => 'authorities';
42
42
43
=head1 NAME
43
=head1 NAME
Lines 236-241 sub get_elasticsearch_mappings { Link Here
236
    return $mappings;
236
    return $mappings;
237
}
237
}
238
238
239
=head2 get_alias_index
240
241
Get the ES specific index names that an alias points to
242
243
Returns an index name or undef
244
245
=cut
246
247
sub get_alias_index {
248
    my ($self) = @_;
249
    my $alias = $self->get_elasticsearch_params()->{index_name}; #FIXME this should probably be an object property/method
250
    if ( !$self->store ) {
251
        my $params  = $self->get_elasticsearch_params();
252
        $self->store(
253
            Catmandu::Store::ElasticSearch->new(
254
                %$params,
255
                index_settings => $self->get_elasticsearch_settings(),
256
                index_mappings => $self->get_elasticsearch_mappings(),
257
            )
258
        );
259
    }
260
    if ( $self->store->es->indices->exists_alias( name => $alias ) ) { #use the embedded es object to access ES methods directly and check if alias points to anything
261
        my $current_index_info = $self->store->es->indices->get_alias( name => $alias ); #we get a hashref of info
262
        my @current_indices = keys %$current_index_info; #the keys contain the names of indexes pointed to be the alias
263
        return $current_indices[0]; #For now we are maintaining a 1:1 ratio, pointing to several is possible
264
    } else {
265
        return undef;
266
    }
267
}
268
269
=head2 update_alias_indices
270
271
Takes our ES alias name and two index names and swaps the old for the new
272
return
273
274
=cut
275
276
sub update_alias {
277
    my $self = shift;
278
    my ( $params ) = @_;
279
    my $alias = $self->get_elasticsearch_params()->{index_name}; #FIXME this should probably be an object property/method
280
    die "Must supply old_index and new_index parameters" unless ( $params->{old_index} && $params->{new_index} );
281
    return $self->store->es->indices->update_aliases( #this is atomic so we won't delete unless this works
282
        body => {
283
            actions => [
284
                { add    => { alias => $alias, index => $params->{new_index} } },
285
                { remove => { alias => $alias, index => $params->{old_index} } }
286
            ]
287
        }
288
    );
289
}
290
239
=head2 _elasticsearch_mapping_for_*
291
=head2 _elasticsearch_mapping_for_*
240
292
241
Get the ES mappings for the given data type or a special mapping case
293
Get the ES mappings for the given data type or a special mapping case
Lines 418-427 These are of a form suited to Catmandu's MARC fixers. Link Here
418
sub _foreach_mapping {
470
sub _foreach_mapping {
419
    my ( $self, $sub ) = @_;
471
    my ( $self, $sub ) = @_;
420
472
473
    my $index_type;
474
475
    $self->index =~ /biblio/ ? $index_type = 'biblios' :
476
        $self->index =~ /authorities/ ? $index_type = 'authorities' : return;
477
421
    # TODO use a caching framework here
478
    # TODO use a caching framework here
422
    my $search_fields = Koha::Database->schema->resultset('SearchField')->search(
479
    my $search_fields = Koha::Database->schema->resultset('SearchField')->search(
423
        {
480
        {
424
            'search_marc_map.index_name' => $self->index,
481
            'search_marc_map.index_name' => $index_type,
425
        },
482
        },
426
        {   join => { search_marc_to_fields => 'search_marc_map' },
483
        {   join => { search_marc_to_fields => 'search_marc_map' },
427
            '+select' => [
484
            '+select' => [
(-)a/misc/search_tools/rebuild_elastic_search.pl (-14 / +32 lines)
Lines 158-171 if ($index_authorities) { Link Here
158
158
159
sub do_reindex {
159
sub do_reindex {
160
    my ( $next, $index_name ) = @_;
160
    my ( $next, $index_name ) = @_;
161
162
    my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new( { index => $index_name } );
161
    my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new( { index => $index_name } );
163
    if ($delete) {
164
162
165
        # We know it's safe to not recreate the indexer because update_index
163
    if (scalar @biblionumbers && !$delete) { #case where we index onto existing alias
166
        # hasn't been called yet.
164
        _index_records( $next, $indexer );
167
        $indexer->drop_index();
165
    } else {
166
        my $new_index_name = $index_name.time; #In the case of full reindex or deletion we are indexing into a new index
167
                                    #and then updating the alias - time gives us a unique name
168
        my $new_indexer = Koha::SearchEngine::Elasticsearch::Indexer->new( { index => $new_index_name } );
169
        _index_records( $next, $new_indexer );
170
        if ( my $current_index = $indexer->get_alias_index() ){
171
            $indexer->update_alias({
172
                old_index => $current_index,
173
                new_index => $new_indexer->get_elasticsearch_params()->{index_name}
174
            });
175
            $current_index =~ s/koha_kohadev_//; #need short index name to build object
176
            $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new( { index => $current_index } );
177
            $indexer->drop_index();
178
        } else {
179
            $indexer->store->es->indices->put_alias(
180
                name  => $indexer->get_elasticsearch_params()->{index_name},
181
                index => $new_indexer->get_elasticsearch_params()->{index_name}
182
            );
183
        }
168
    }
184
    }
185
}
186
187
# Checks some basic stuff to ensure that it's sane before we start.
188
sub sanity_check {
189
    # Do we have an elasticsearch block defined?
190
    my $conf = C4::Context->config('elasticsearch');
191
    die "No 'elasticsearch' block is defined in koha-conf.xml.\n" if ( !$conf );
192
}
193
194
sub _index_records {
195
    my ( $next, $indexer ) = @_;
169
196
170
    my $count        = 0;
197
    my $count        = 0;
171
    my $commit_count = $commit;
198
    my $commit_count = $commit;
Lines 186-204 sub do_reindex { Link Here
186
            @commit_buffer = ();
213
            @commit_buffer = ();
187
        }
214
        }
188
    }
215
    }
189
190
    # There are probably uncommitted records
216
    # There are probably uncommitted records
191
    $indexer->update_index( \@id_buffer, \@commit_buffer );
217
    $indexer->update_index( \@id_buffer, \@commit_buffer );
192
    _log( 1, "$count records indexed.\n" );
218
    _log( 1, "$count records indexed.\n" );
193
}
219
}
194
220
195
# Checks some basic stuff to ensure that it's sane before we start.
196
sub sanity_check {
197
    # Do we have an elasticsearch block defined?
198
    my $conf = C4::Context->config('elasticsearch');
199
    die "No 'elasticsearch' block is defined in koha-conf.xml.\n" if ( !$conf );
200
}
201
202
# Output progress information.
221
# Output progress information.
203
#
222
#
204
#   _log($level, $msg);
223
#   _log($level, $msg);
205
- 

Return to bug 18948