diff --git a/lib/Elastijk.pm b/lib/Elastijk.pm index ac9a028..87e42b5 100644 --- a/lib/Elastijk.pm +++ b/lib/Elastijk.pm @@ -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}}); } diff --git a/lib/Elastijk/oo.pm b/lib/Elastijk/oo.pm index 60db779..b6020ce 100644 --- a/lib/Elastijk/oo.pm +++ b/lib/Elastijk/oo.pm @@ -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)); } diff --git a/t/live-oo-index-create-and-delete.t b/t/live-oo-index-create-and-delete.t index aa3e4c6..22e7825 100644 --- a/t/live-oo-index-create-and-delete.t +++ b/t/live-oo-index-create-and-delete.t @@ -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, @@ -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, } } }