Skip to content

Commit

Permalink
fix test failures with Elasticsearch 6.x
Browse files Browse the repository at this point in the history
The URL "type exists" API[1] has changed and somehow, with my local
installation, it does not allow "HEAD" method, but respond correctly
with "GET" method.

[1]: https://www.elastic.co/guide/en/elasticsearch/reference/6.3/indices-types-exists.html
  • Loading branch information
gugod committed Jul 4, 2018
1 parent 2ad9be1 commit ae46c10
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/Elastijk.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ our $JSON = JSON->new->utf8;
sub _build_hijk_request_args {
my $args = $_[0];
my ($path, $qs, $uri_param);
$path = "/". join("/", (map { defined($_) ? ( uri_escape_utf8($_) ) : () } @{$args}{qw(index type id)}), (exists $args->{command} ? $args->{command} : ()));
$path = (exists $args->{path}) ? $args->{path} : ("/". join("/", (map { defined($_) ? ( uri_escape_utf8($_) ) : () } @{$args}{qw(index type id)}), (exists $args->{command} ? $args->{command} : ())));
if ($args->{uri_param}) {
$qs = join('&', map { uri_escape_utf8($_) . "=" . uri_escape_utf8($args->{uri_param}{$_}) } keys %{$args->{uri_param}});
}
Expand Down
16 changes: 14 additions & 2 deletions lib/Elastijk/oo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,20 @@ sub post {
}

sub exists {
my $self = shift;
my ($status,$res) = $self->request(method => "HEAD", @_);
my ($self, %args) = @_;
my $index = exists($args{index}) ? $args{index} : $self->{index};

my ($status,$res);
$res = $self->request(method => "GET", path => '/');

if (($res->{version}{number} ge '6') && $index && exists($args{type}) && !exists($args{id})) {
# https://www.elastic.co/guide/en/elasticsearch/reference/6.0/indices-types-exists.html
my $path = '/' . $index . '/_mappings/' . $args{type};
($status,$res) = $self->request(method => "GET", path => $path);
} else {
($status,$res) = $self->request(method => "HEAD", %args);
}

return ($status,'2' eq substr($status,0,1));
}

Expand Down
15 changes: 12 additions & 3 deletions t/live-oo-index-create-and-delete.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@ unless ($ENV{TEST_LIVE}) {
use Elastijk;

my $es = Elastijk->new( host => "localhost", port => "9200" );

my $res = $es->get(path => '/');
my $es_server_version = $res->{version}{number};
my $es_version_6_or_newer = ($es_server_version ge '6.0.0');

my $test_index_name = "test_index_$$";
my $res;

subtest "create an index with settings and mappings" => sub {
# Check if the index exists
$res = $es->exists( index => $test_index_name );
ok !$res, "The index $test_index_name should not exist because we have not created it yet.";

my $Str = { type => "string" };
if ($es_version_6_or_newer) {
$Str = { type => 'text' };
}

# Create an index with settings and mappings.
$res = $es->put(
index => $test_index_name,
Expand All @@ -32,8 +41,8 @@ subtest "create an index with settings and mappings" => sub {
mappings => {
cafe => {
properties => {
name => { type => "string" },
address => { type => "string" }
name => $Str,
address => $Str,
}
}
}
Expand Down

0 comments on commit ae46c10

Please sign in to comment.