From 5e4a4d663a9b9e5e9a220561fdddca16a18561b4 Mon Sep 17 00:00:00 2001 From: Marin Bek Date: Fri, 27 Feb 2015 16:46:08 +0100 Subject: [PATCH] AliasExists implementation --- goes.go | 14 ++++++++++++++ goes_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/goes.go b/goes.go index 076bcb7..5637228 100644 --- a/goes.go +++ b/goes.go @@ -562,3 +562,17 @@ func (c *Connection) AddAlias(alias string, indexes []string) (Response, error) func (c *Connection) RemoveAlias(alias string, indexes []string) (Response, error) { return c.modifyAlias("remove", alias, indexes) } + +// AliasExists checks whether alias is defined on the server +func (c *Connection) AliasExists(alias string) (bool, error) { + + r := Request{ + Conn: c, + method: "HEAD", + api: "_alias/" + alias, + } + + resp, err := r.Run() + + return resp.Status == 200, err +} diff --git a/goes_test.go b/goes_test.go index e402f5b..3334d30 100644 --- a/goes_test.go +++ b/goes_test.go @@ -1363,3 +1363,28 @@ func (s *GoesTestSuite) TestRemoveAlias(c *C) { _, err = conn.Get(aliasName, docType, docId, url.Values{}) c.Assert(err.Error(), Equals, "[404] IndexMissingException[["+aliasName+"] missing]") } + +func (s *GoesTestSuite) TestAliasExists(c *C) { + index := "testaliasexist_1" + alias := "testaliasexists" + + conn := NewConnection(ES_HOST, ES_PORT) + // just in case + conn.DeleteIndex(index) + + exists, err := conn.AliasExists(alias) + c.Assert(exists, Equals, false) + + _, err = conn.CreateIndex(index, map[string]interface{}{}) + c.Assert(err, IsNil) + defer conn.DeleteIndex(index) + time.Sleep(200 * time.Millisecond) + + _, err = conn.AddAlias(alias, []string{index}) + c.Assert(err, IsNil) + time.Sleep(200 * time.Millisecond) + defer conn.RemoveAlias(alias, []string{index}) + + exists, err = conn.AliasExists(alias) + c.Assert(exists, Equals, true) +}