From 8413c690d4a885a614c42084bfbb51f14a9ee0e6 Mon Sep 17 00:00:00 2001 From: Marin Bek Date: Fri, 8 Jan 2016 10:09:46 +0100 Subject: [PATCH] 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