use OpenSRF::Utils::SettingsClient;
use OpenILS::Utils::CStoreEditor qw/:funcs/;
use Search::Elasticsearch;
+use Data::Dumper;
sub new {
my ($class, $cluster) = @_;
sub index_document {
my ($self, $id, $body) = @_;
- my $result = $self->es->index(
- index => $self->index_name,
- id => $id,
- body => $body
- );
+ my $result;
+
+ eval {
+ $result = $self->es->index(
+ index => $self->index_name,
+ type => 'record', # deprecated in v6
+ id => $id,
+ body => $body
+ );
+ };
+
+ if ($@) {
+ $logger->error("ES index_document failed with $@");
+ return undef;
+ }
$logger->debug("ES index command returned $result");
+ return $result;
}
sub search {
my ($self, $query) = @_;
- return $self->es->search(
- index => $self->index_name,
- body => $query
- );
+ my $result;
+
+ eval {
+ $result = $self->es->search(
+ index => $self->index_name,
+ body => $query
+ );
+ };
+
+ if ($@) {
+ $logger->error("ES search failed with $@");
+ $logger->error("ES failed query: " . Dumper($query));
+ return undef;
+ }
+
+ return $result;
}
+
1;
use OpenSRF::Utils::Logger qw/:logger/;
use OpenILS::Elastic;
use base qw/OpenILS::Elastic/;
+use Data::Dumper;
+$Data::Dumper::Indent = 2;
my $INDEX_NAME = 'bib-search';
sub index {
my $self = shift;
return $self->{index} if $self->{index};
- ($self->{index}) = grep {$_->{name} eq $INDEX_NAME} @{$self->{indices}};
+ ($self->{index}) = grep {$_->{purpose} eq $INDEX_NAME} @{$self->{indices}};
return $self->{index};
}
}
my $settings = $BASE_INDEX_SETTINGS;
- $settings->{number_of_replicas} = scalar($self->{servers});
+ $settings->{number_of_replicas} = scalar(@{$self->{servers}});
$settings->{number_of_shards} = $self->index->{num_shards};
my $conf = {
index => $INDEX_NAME,
body => {
settings => $settings,
- mappings => {properties => $mappings}
+ mappings => {record => {properties => $mappings}}
+ # document type (i.e. 'record') deprecated in v6
+ #mappings => {properties => $mappings}
}
};
if ($@) {
$logger->error("ES failed to create index cluster=".
$self->cluster. "index=$INDEX_NAME error=$@");
+ print "$@\n\n";
return 0;
}
$body->{$field} = $val;
}
- $self->index_document($INDEX_NAME, $bib_id, $body);
+ return 0 unless
+ $self->index_document($bib_id, $body);
$state->{last_bib_id} = $bib_id;
$index_count++;
use OpenILS::Elastic::BibSearch;
my $help;
-my $elastic_config;
my $osrf_config = '/openils/conf/opensrf_core.xml';
my $cluster = 'main';
my $create_index;
my $delete_index;
-my $index_name; # use "all" to affect all configured indexes
+my $index_name;
my $populate;
-my $partial;
GetOptions(
'help' => \$help,
- 'elastic-config=s' => \$elastic_config,
'osrf-config=s' => \$osrf_config,
'cluster=s' => \$cluster,
'create-index' => \$create_index,
'delete-index' => \$delete_index,
'index=s' => \$index_name,
- 'populate' => \$populate,
- 'partial' => \$partial
+ 'populate' => \$populate
) || die "\nSee --help for more\n";
# connect to osrf...
$es->connect;
-$es->delete_index if $delete_index;
+if ($delete_index) {
+ $es->delete_index or die "Index delete failed.\n";
+}
-$es->create_index if $create_index;
+if ($create_index) {
+ $es->create_index or die "Index create failed.\n";
+}
-$es->populate_index if $populate;
+if ($populate) {
+ $es->populate_index or die "Index populate failed.\n";
+}
use Time::HiRes qw/time/;
use OpenSRF::Utils::JSON;
use OpenILS::Utils::Fieldmapper;
-use OpenILS::Elastic;
+use OpenILS::Elastic::BibSearch;
my $help;
-my $elastic_config;
my $osrf_config = '/openils/conf/opensrf_core.xml';
my $cluster = 'main';
my $index = 'bib-search';
GetOptions(
'help' => \$help,
- 'elastic-config=s' => \$elastic_config,
'osrf-config=s' => \$osrf_config,
'cluster=s' => \$cluster,
'index=s' => \$index,
}
};
-my $es = OpenILS::Utils::ElasticSearch->new(
- config_file => $elastic_config
-);
+my $es = OpenILS::Elastic::BibSearch->new($cluster);
-$es->connect($cluster);
+$es->connect;
my $start = time();
-my $results = $es->search($index, $query);
+my $results = $es->search($query);
my $duration = substr(time() - $start, 0, 6);
print OpenSRF::Utils::JSON->perl2JSON($results) . "\n\n";