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

(-)a/Koha/SearchEngine/Elasticsearch.pm (-3 / +3 lines)
Lines 90-97 instance level and will be reused if method is called multiple times. Link Here
90
sub get_elasticsearch {
90
sub get_elasticsearch {
91
    my $self = shift @_;
91
    my $self = shift @_;
92
    unless (defined $self->{elasticsearch}) {
92
    unless (defined $self->{elasticsearch}) {
93
        my $conf = $self->get_elasticsearch_params();
93
        $self->{elasticsearch} = Search::Elasticsearch->new(
94
        $self->{elasticsearch} = Search::Elasticsearch->new($conf);
94
            $self->get_elasticsearch_params()
95
        );
95
    }
96
    }
96
    return $self->{elasticsearch};
97
    return $self->{elasticsearch};
97
}
98
}
Lines 1127-1133 A string that indicates the MARC type that this mapping is for, e.g. 'marc21', Link Here
1127
=item C<$marc_field>
1128
=item C<$marc_field>
1128
1129
1129
A string that describes the MARC field that contains the data to extract.
1130
A string that describes the MARC field that contains the data to extract.
1130
These are of a form suited to Catmandu's MARC fixers.
1131
1131
1132
=back
1132
=back
1133
1133
(-)a/Koha/SearchEngine/Elasticsearch/Indexer.pm (-18 / +12 lines)
Lines 24-38 use List::Util qw(any); Link Here
24
use base qw(Koha::SearchEngine::Elasticsearch);
24
use base qw(Koha::SearchEngine::Elasticsearch);
25
use Data::Dumper;
25
use Data::Dumper;
26
26
27
# For now just marc, but we can do anything here really
28
use Catmandu::Importer::MARC;
29
use Catmandu::Store::ElasticSearch;
30
31
use Koha::Exceptions;
27
use Koha::Exceptions;
32
use C4::Context;
28
use C4::Context;
33
29
34
Koha::SearchEngine::Elasticsearch::Indexer->mk_accessors(qw( store ));
35
36
=head1 NAME
30
=head1 NAME
37
31
38
Koha::SearchEngine::Elasticsearch::Indexer - handles adding new records to the index
32
Koha::SearchEngine::Elasticsearch::Indexer - handles adding new records to the index
Lines 107-113 sub update_index { Link Here
107
    my $documents = $self->marc_records_to_documents($records);
101
    my $documents = $self->marc_records_to_documents($records);
108
    my @body;
102
    my @body;
109
103
110
    for (my $i=0; $i < scalar @$biblionums; $i++) {
104
    for (my $i = 0; $i < scalar @$biblionums; $i++) {
111
        my $id = $biblionums->[$i];
105
        my $id = $biblionums->[$i];
112
        my $document = $documents->[$i];
106
        my $document = $documents->[$i];
113
        push @body, {
107
        push @body, {
Lines 293-310 C<$biblionums> is an arrayref of biblionumbers to delete from the index. Link Here
293
sub delete_index {
287
sub delete_index {
294
    my ($self, $biblionums) = @_;
288
    my ($self, $biblionums) = @_;
295
289
296
    if ( !$self->store ) {
290
    my $elasticsearch = $self->get_elasticsearch();
297
        my $params  = $self->get_elasticsearch_params();
291
    my $conf  = $self->get_elasticsearch_params();
298
        $self->store(
292
299
            Catmandu::Store::ElasticSearch->new(
293
    my @body = map { { delete => { _id => $_ } } } @{$biblionums};
300
                %$params,
294
    my $result = $elasticsearch->bulk(
301
                index_settings => $self->get_elasticsearch_settings(),
295
        index => $conf->{index_name},
302
                index_mappings => $self->get_elasticsearch_mappings(),
296
        type => 'data',
303
            )
297
        body => \@body,
304
        );
298
    );
299
    if ($result->{errors}) {
300
        croak "An Elasticsearch error occured during bulk delete";
305
    }
301
    }
306
    $self->store->bag->delete("$_") foreach @$biblionums;
307
    $self->store->bag->commit;
308
}
302
}
309
303
310
=head2 delete_index_background($biblionums)
304
=head2 delete_index_background($biblionums)
(-)a/Koha/SearchEngine/Elasticsearch/Search.pm (-17 / +17 lines)
Lines 49-55 use Koha::SearchEngine::QueryBuilder; Link Here
49
use Koha::SearchEngine::Search;
49
use Koha::SearchEngine::Search;
50
use Koha::Exceptions::Elasticsearch;
50
use Koha::Exceptions::Elasticsearch;
51
use MARC::Record;
51
use MARC::Record;
52
use Catmandu::Store::ElasticSearch;
53
use MARC::File::XML;
52
use MARC::File::XML;
54
use Data::Dumper; #TODO remove
53
use Data::Dumper; #TODO remove
55
use Carp qw(cluck);
54
use Carp qw(cluck);
Lines 117-131 faster than pulling all the data in, usually. Link Here
117
116
118
sub count {
117
sub count {
119
    my ( $self, $query ) = @_;
118
    my ( $self, $query ) = @_;
119
    my $elasticsearch = $self->get_elasticsearch();
120
    my $conf = $self->get_elasticsearch_params();
120
121
121
    my $params = $self->get_elasticsearch_params();
122
    # TODO: Probably possible to exclude results
122
    $self->store(
123
    # and just return number of hits
123
        Catmandu::Store::ElasticSearch->new( %$params, trace_calls => 0, ) )
124
    my $result = $elasticsearch->search(
124
      unless $self->store;
125
        index => $conf->{index_name},
126
        body => $query
127
    );
125
128
126
    my $search = $self->store->bag->search( %$query);
129
    return $result->{hits}->{total};
127
    my $count = $search->total() || 0;
128
    return $count;
129
}
130
}
130
131
131
=head2 search_compat
132
=head2 search_compat
Lines 405-422 the default value for this setting in case it is not set) Link Here
405
sub max_result_window {
406
sub max_result_window {
406
    my ($self) = @_;
407
    my ($self) = @_;
407
408
408
    $self->store(
409
    my $elasticsearch = $self->get_elasticsearch();
409
        Catmandu::Store::ElasticSearch->new(%{ $self->get_elasticsearch_params })
410
    my $conf = $self->get_elasticsearch_params();
410
    ) unless $self->store;
411
411
412
    my $index_name = $self->store->index_name;
412
    my $response = $elasticsearch->indices->get_settings(
413
    my $settings = $self->store->es->indices->get_settings(
413
        index => $conf->{index_name},
414
        index  => $index_name,
414
        flat_settings => 'true',
415
        params => { include_defaults => 'true', flat_settings => 'true' },
415
        include_defaults => 'true'
416
    );
416
    );
417
417
418
    my $max_result_window = $settings->{$index_name}->{settings}->{'index.max_result_window'};
418
    my $max_result_window = $response->{$conf->{index_name}}->{settings}->{'index.max_result_window'};
419
    $max_result_window //= $settings->{$index_name}->{defaults}->{'index.max_result_window'};
419
    $max_result_window //= $response->{$conf->{index_name}}->{defaults}->{'index.max_result_window'};
420
420
421
    return $max_result_window;
421
    return $max_result_window;
422
}
422
}
(-)a/t/00-load.t (-3 / +2 lines)
Lines 76-86 sub is_testable { Link Here
76
    my @needed_module_names;
76
    my @needed_module_names;
77
    my $return_value = 1;
77
    my $return_value = 1;
78
    if ( $module_name =~ /Koha::SearchEngine::Elasticsearch::Indexer/xsm ) {
78
    if ( $module_name =~ /Koha::SearchEngine::Elasticsearch::Indexer/xsm ) {
79
        @needed_module_names =
79
        @needed_module_names = ( 'Search::Elasticsearch' );
80
          ( 'Catmandu::Importer::MARC', 'Catmandu::Store::ElasticSearch' );
81
    }
80
    }
82
    elsif ( $module_name =~ /Koha::SearchEngine::Elasticsearch::Search/xsm ) {
81
    elsif ( $module_name =~ /Koha::SearchEngine::Elasticsearch::Search/xsm ) {
83
        @needed_module_names = ( 'Catmandu::Store::ElasticSearch' );
82
        @needed_module_names = ( 'Search::Elasticsearch' );
84
    }
83
    }
85
    elsif ( $module_name =~ /Koha::SearchEngine::Elasticsearch/xsm ) {
84
    elsif ( $module_name =~ /Koha::SearchEngine::Elasticsearch/xsm ) {
86
        @needed_module_names = ( 'Search::Elasticsearch' );
85
        @needed_module_names = ( 'Search::Elasticsearch' );
(-)a/t/db_dependent/Koha/Authorities.t (-6 lines)
Lines 41-52 use Koha::Database; Link Here
41
use t::lib::Mocks;
41
use t::lib::Mocks;
42
use t::lib::TestBuilder;
42
use t::lib::TestBuilder;
43
43
44
BEGIN {
45
    #TODO Helpful as long as we have issues here
46
    my $mock = Test::MockObject->new();
47
    $mock->fake_module( 'Catmandu::Store::ElasticSearch' );
48
}
49
50
my $schema = Koha::Database->new->schema;
44
my $schema = Koha::Database->new->schema;
51
$schema->storage->txn_begin;
45
$schema->storage->txn_begin;
52
46
(-)a/t/db_dependent/Koha/SearchEngine/Elasticsearch/Search.t (-5 / +9 lines)
Lines 115-124 SKIP: { Link Here
115
    is ( $count = $searcher->count_auth_use($searcher,1), 0, 'Testing count_auth_use');
115
    is ( $count = $searcher->count_auth_use($searcher,1), 0, 'Testing count_auth_use');
116
116
117
    is ($searcher->max_result_window, 10000, 'By default, max_result_window is 10000');
117
    is ($searcher->max_result_window, 10000, 'By default, max_result_window is 10000');
118
    $searcher->store->es->indices->put_settings(index => $searcher->store->index_name, body => {
118
119
        'index' => {
119
    $searcher->get_elasticsearch()->indices->put_settings(
120
            'max_result_window' => 12000,
120
        index => $searcher->get_elasticsearch_params()->{index_name},
121
        },
121
        body => {
122
    });
122
            'index' => {
123
                'max_result_window' => 12000,
124
            },
125
        }
126
    );
123
    is ($searcher->max_result_window, 12000, 'max_result_window returns the correct value');
127
    is ($searcher->max_result_window, 12000, 'max_result_window returns the correct value');
124
}
128
}
(-)a/t/db_dependent/Koha/SearchEngine/Search.t (-6 lines)
Lines 17-27 use t::lib::Mocks; Link Here
17
use Koha::Database;
17
use Koha::Database;
18
use Koha::SearchEngine::Search;
18
use Koha::SearchEngine::Search;
19
19
20
BEGIN {
21
    my $mock = Test::MockObject->new();
22
    $mock->fake_module( 'Catmandu::Store::ElasticSearch' );
23
}
24
25
my $schema  = Koha::Database->new->schema;
20
my $schema  = Koha::Database->new->schema;
26
$schema->storage->txn_begin;
21
$schema->storage->txn_begin;
27
22
28
- 

Return to bug 24823