sub get_elasticsearch {
my $self = shift @_;
unless (defined $self->{elasticsearch}) {
- my $conf = $self->get_elasticsearch_params();
- $self->{elasticsearch} = Search::Elasticsearch->new($conf);
+ $self->{elasticsearch} = Search::Elasticsearch->new(
+ $self->get_elasticsearch_params()
+ );
}
return $self->{elasticsearch};
}
=item C<$marc_field>
A string that describes the MARC field that contains the data to extract.
-These are of a form suited to Catmandu's MARC fixers.
=back
use base qw(Koha::SearchEngine::Elasticsearch);
use Data::Dumper;
-# For now just marc, but we can do anything here really
-use Catmandu::Importer::MARC;
-use Catmandu::Store::ElasticSearch;
-
use Koha::Exceptions;
use C4::Context;
-Koha::SearchEngine::Elasticsearch::Indexer->mk_accessors(qw( store ));
-
=head1 NAME
Koha::SearchEngine::Elasticsearch::Indexer - handles adding new records to the index
my $documents = $self->marc_records_to_documents($records);
my @body;
- for (my $i=0; $i < scalar @$biblionums; $i++) {
+ for (my $i = 0; $i < scalar @$biblionums; $i++) {
my $id = $biblionums->[$i];
my $document = $documents->[$i];
push @body, {
sub delete_index {
my ($self, $biblionums) = @_;
- if ( !$self->store ) {
- my $params = $self->get_elasticsearch_params();
- $self->store(
- Catmandu::Store::ElasticSearch->new(
- %$params,
- index_settings => $self->get_elasticsearch_settings(),
- index_mappings => $self->get_elasticsearch_mappings(),
- )
- );
+ my $elasticsearch = $self->get_elasticsearch();
+ my $conf = $self->get_elasticsearch_params();
+
+ my @body = map { { delete => { _id => $_ } } } @{$biblionums};
+ my $result = $elasticsearch->bulk(
+ index => $conf->{index_name},
+ type => 'data',
+ body => \@body,
+ );
+ if ($result->{errors}) {
+ croak "An Elasticsearch error occured during bulk delete";
}
- $self->store->bag->delete("$_") foreach @$biblionums;
- $self->store->bag->commit;
}
=head2 delete_index_background($biblionums)
use Koha::SearchEngine::Search;
use Koha::Exceptions::Elasticsearch;
use MARC::Record;
-use Catmandu::Store::ElasticSearch;
use MARC::File::XML;
use Data::Dumper; #TODO remove
use Carp qw(cluck);
sub count {
my ( $self, $query ) = @_;
+ my $elasticsearch = $self->get_elasticsearch();
+ my $conf = $self->get_elasticsearch_params();
- my $params = $self->get_elasticsearch_params();
- $self->store(
- Catmandu::Store::ElasticSearch->new( %$params, trace_calls => 0, ) )
- unless $self->store;
+ # TODO: Probably possible to exclude results
+ # and just return number of hits
+ my $result = $elasticsearch->search(
+ index => $conf->{index_name},
+ body => $query
+ );
- my $search = $self->store->bag->search( %$query);
- my $count = $search->total() || 0;
- return $count;
+ return $result->{hits}->{total};
}
=head2 search_compat
sub max_result_window {
my ($self) = @_;
- $self->store(
- Catmandu::Store::ElasticSearch->new(%{ $self->get_elasticsearch_params })
- ) unless $self->store;
+ my $elasticsearch = $self->get_elasticsearch();
+ my $conf = $self->get_elasticsearch_params();
- my $index_name = $self->store->index_name;
- my $settings = $self->store->es->indices->get_settings(
- index => $index_name,
- params => { include_defaults => 'true', flat_settings => 'true' },
+ my $response = $elasticsearch->indices->get_settings(
+ index => $conf->{index_name},
+ flat_settings => 'true',
+ include_defaults => 'true'
);
- my $max_result_window = $settings->{$index_name}->{settings}->{'index.max_result_window'};
- $max_result_window //= $settings->{$index_name}->{defaults}->{'index.max_result_window'};
+ my $max_result_window = $response->{$conf->{index_name}}->{settings}->{'index.max_result_window'};
+ $max_result_window //= $response->{$conf->{index_name}}->{defaults}->{'index.max_result_window'};
return $max_result_window;
}
my @needed_module_names;
my $return_value = 1;
if ( $module_name =~ /Koha::SearchEngine::Elasticsearch::Indexer/xsm ) {
- @needed_module_names =
- ( 'Catmandu::Importer::MARC', 'Catmandu::Store::ElasticSearch' );
+ @needed_module_names = ( 'Search::Elasticsearch' );
}
elsif ( $module_name =~ /Koha::SearchEngine::Elasticsearch::Search/xsm ) {
- @needed_module_names = ( 'Catmandu::Store::ElasticSearch' );
+ @needed_module_names = ( 'Search::Elasticsearch' );
}
elsif ( $module_name =~ /Koha::SearchEngine::Elasticsearch/xsm ) {
@needed_module_names = ( 'Search::Elasticsearch' );
use t::lib::Mocks;
use t::lib::TestBuilder;
-BEGIN {
- #TODO Helpful as long as we have issues here
- my $mock = Test::MockObject->new();
- $mock->fake_module( 'Catmandu::Store::ElasticSearch' );
-}
-
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
is ( $count = $searcher->count_auth_use($searcher,1), 0, 'Testing count_auth_use');
is ($searcher->max_result_window, 10000, 'By default, max_result_window is 10000');
- $searcher->store->es->indices->put_settings(index => $searcher->store->index_name, body => {
- 'index' => {
- 'max_result_window' => 12000,
- },
- });
+
+ $searcher->get_elasticsearch()->indices->put_settings(
+ index => $searcher->get_elasticsearch_params()->{index_name},
+ body => {
+ 'index' => {
+ 'max_result_window' => 12000,
+ },
+ }
+ );
is ($searcher->max_result_window, 12000, 'max_result_window returns the correct value');
}
use Koha::Database;
use Koha::SearchEngine::Search;
-BEGIN {
- my $mock = Test::MockObject->new();
- $mock->fake_module( 'Catmandu::Store::ElasticSearch' );
-}
-
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;