From 8413c690d4a885a614c42084bfbb51f14a9ee0e6 Mon Sep 17 00:00:00 2001 From: Marin Bek Date: Fri, 8 Jan 2016 10:09:46 +0100 Subject: [PATCH 1/4] support for parent-child relationship --- goes.go | 4 ++++ goes_test.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ structs.go | 1 + 3 files changed, 61 insertions(+) diff --git a/goes.go b/goes.go index 107ce6a..237e02f 100644 --- a/goes.go +++ b/goes.go @@ -309,6 +309,10 @@ func (c *Connection) Get(index string, documentType string, id string, extraArgs // The extraArgs is a list of url.Values that you can send to elasticsearch as // URL arguments, for example, to control routing, ttl, version, op_type, etc. func (c *Connection) Index(d Document, extraArgs url.Values) (*Response, error) { + if parent, ok := d.Parent.(string); ok { + extraArgs.Set("parent", parent) + } + r := Request{ Conn: c, Query: d.Fields, diff --git a/goes_test.go b/goes_test.go index 7866558..338141d 100644 --- a/goes_test.go +++ b/goes_test.go @@ -1424,3 +1424,59 @@ func (s *GoesTestSuite) TestAliasExists(c *C) { exists, err = conn.AliasExists(alias) c.Assert(exists, Equals, true) } + +func (s *GoesTestSuite) TestIndexWithChild(c *C) { + indexName := "testindexwithchild" + docType := "tweet" + + conn := NewConnection(ES_HOST, ES_PORT) + // just in case + conn.DeleteIndex(indexName) + + _, err := conn.CreateIndex(indexName, map[string]interface{}{}) + c.Assert(err, IsNil) + //defer conn.DeleteIndex(indexName) + + authorDoc := Document{ + Index: indexName, + Type: "author", + Fields: map[string]interface{}{ + "name": "An Author", + }, + Id: "aut", + } + response, err := conn.Index(authorDoc, url.Values{}) + c.Assert(err, IsNil) + + mapping := map[string]interface{}{ + "tweet": map[string]interface{}{ + "properties": map[string]interface{}{ + "count": map[string]interface{}{ + "type": "integer", + "index": "not_analyzed", + "store": true, + }, + }, + "_parent": map[string]interface{}{ + "type": "author", + }, + }, + } + response, err = conn.PutMapping("tweet", mapping, []string{indexName}) + c.Assert(err, IsNil) + + c.Assert(response.Acknowledged, Equals, true) + c.Assert(response.TimedOut, Equals, false) + + d := Document{ + Index: indexName, + Type: docType, + Fields: map[string]interface{}{ + "count": 5, + }, + Parent: "aut", + } + + response, err = conn.Index(d, url.Values{}) + c.Assert(err, IsNil) +} diff --git a/structs.go b/structs.go index c726461..cbcc317 100644 --- a/structs.go +++ b/structs.go @@ -107,6 +107,7 @@ type Document struct { Id interface{} BulkCommand string Fields interface{} + Parent interface{} } // Represents the "items" field in a _bulk response From d2a91702fdf0a622a5fc8305cb51745a800ecc97 Mon Sep 17 00:00:00 2001 From: Marin Bek Date: Wed, 7 Sep 2016 15:34:49 +0200 Subject: [PATCH 2/4] Open and Close indices --- goes.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/goes.go b/goes.go index 237e02f..5f5399e 100644 --- a/goes.go +++ b/goes.go @@ -605,3 +605,27 @@ func (c *Connection) AliasExists(alias string) (bool, error) { return resp.Status == 200, err } + +// CloseIndex closes an index represented by a name +func (c *Connection) CloseIndex(name string) (*Response, error) { + r := Request{ + Conn: c, + IndexList: []string{name}, + method: "POST", + api: "_close", + } + + return r.Run() +} + +// OpenIndex opens an index represented by a name +func (c *Connection) OpenIndex(name string) (*Response, error) { + r := Request{ + Conn: c, + IndexList: []string{name}, + method: "POST", + api: "_open", + } + + return r.Run() +} From 71d6bc3694c34a6c8606226b6fafd63e1924e420 Mon Sep 17 00:00:00 2001 From: Marin Bek Date: Fri, 9 Sep 2016 13:22:47 +0200 Subject: [PATCH 3/4] get aliases --- goes.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/goes.go b/goes.go index 5f5399e..684c024 100644 --- a/goes.go +++ b/goes.go @@ -629,3 +629,15 @@ func (c *Connection) OpenIndex(name string) (*Response, error) { return r.Run() } + +func (c *Connection) GetAliases(indexes []string) (*Response, error) { + + r := Request{ + Conn: c, + IndexList: indexes, + method: "GET", + api: "_alias/", + } + + return r.Run() +} From 6d8c5d2699145dfc0968306c4e8024fd1ed8a3b5 Mon Sep 17 00:00:00 2001 From: Marin Bek Date: Fri, 9 Sep 2016 17:43:35 +0200 Subject: [PATCH 4/4] replace index in alias --- goes.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/goes.go b/goes.go index 684c024..9c54a90 100644 --- a/goes.go +++ b/goes.go @@ -641,3 +641,29 @@ func (c *Connection) GetAliases(indexes []string) (*Response, error) { return r.Run() } + +func (c *Connection) ReplaceIndexInAlias(alias string, old_index string, new_index string) (*Response, error) { + command := map[string]interface{}{ + "actions": make([]map[string]interface{}, 1), + } + + command["actions"] = append(command["actions"].([]map[string]interface{}), map[string]interface{}{ + "remove": map[string]interface{}{ + "index": old_index, + "alias": alias, + }, + "add": map[string]interface{}{ + "index": new_index, + "alias": alias, + }, + }) + + r := Request{ + Conn: c, + Query: command, + method: "POST", + api: "_aliases", + } + + return r.Run() +}