AddAlias implementation

This commit is contained in:
Marin Bek 2015-02-27 15:32:56 +01:00
parent 475a722fea
commit 121db02e3d
2 changed files with 74 additions and 0 deletions

25
goes.go
View File

@ -528,3 +528,28 @@ func (c *Connection) DeleteMapping(typeName string, indexes []string) (Response,
return r.Run() 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()
}

View File

@ -1272,3 +1272,52 @@ func (s *GoesTestSuite) TestDeleteMapping(c *C) {
c.Assert(response.Acknowledged, Equals, true) c.Assert(response.Acknowledged, Equals, true)
c.Assert(response.TimedOut, Equals, false) 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)
}