From 121db02e3dd7bd847aa9661f183bbad7e96a9dfa Mon Sep 17 00:00:00 2001 From: Marin Bek Date: Fri, 27 Feb 2015 15:32:56 +0100 Subject: [PATCH] AddAlias implementation --- goes.go | 25 +++++++++++++++++++++++++ goes_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/goes.go b/goes.go index a85cd7f..ce6bc29 100644 --- a/goes.go +++ b/goes.go @@ -528,3 +528,28 @@ func (c *Connection) DeleteMapping(typeName string, indexes []string) (Response, return r.Run() } + +// AddAlias creates an alias to one or more indexes +func (c *Connection) AddAlias(alias string, indexes []string) (Response, error) { + command := map[string]interface{}{ + "actions": make([]map[string]interface{}, 1), + } + + for _, index := range indexes { + command["actions"] = append(command["actions"].([]map[string]interface{}), map[string]interface{}{ + "add": map[string]interface{}{ + "index": index, + "alias": alias, + }, + }) + } + + r := Request{ + Conn: c, + Query: command, + method: "POST", + api: "_aliases", + } + + return r.Run() +} diff --git a/goes_test.go b/goes_test.go index fdb6023..30f34d0 100644 --- a/goes_test.go +++ b/goes_test.go @@ -1272,3 +1272,52 @@ func (s *GoesTestSuite) TestDeleteMapping(c *C) { c.Assert(response.Acknowledged, Equals, true) c.Assert(response.TimedOut, Equals, false) } + +func (s *GoesTestSuite) TestAddAlias(c *C) { + aliasName := "testAlias" + indexName := "testalias_1" + docType := "testDoc" + docId := "1234" + source := map[string]interface{}{ + "user": "foo", + "message": "bar", + } + + conn := NewConnection(ES_HOST, ES_PORT) + defer conn.DeleteIndex(indexName) + + _, err := conn.CreateIndex(indexName, map[string]interface{}{}) + c.Assert(err, IsNil) + defer conn.DeleteIndex(indexName) + + d := Document{ + Index: indexName, + Type: docType, + Id: docId, + Fields: source, + } + + // Index data + _, err = conn.Index(d, url.Values{}) + c.Assert(err, IsNil) + + // Add alias + _, err = conn.AddAlias(aliasName, []string{indexName}) + c.Assert(err, IsNil) + + // Get document via alias + response, err := conn.Get(aliasName, docType, docId, url.Values{}) + c.Assert(err, IsNil) + + expectedResponse := Response{ + Index: indexName, + Type: docType, + Id: docId, + Version: 1, + Found: true, + Source: source, + } + + response.Raw = nil + c.Assert(response, DeepEquals, expectedResponse) +}