support for parent-child relationship

This commit is contained in:
Marin Bek 2016-01-08 10:09:46 +01:00
parent 23413ed12a
commit 8413c690d4
3 changed files with 61 additions and 0 deletions

View File

@ -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,

View File

@ -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)
}

View File

@ -107,6 +107,7 @@ type Document struct {
Id interface{}
BulkCommand string
Fields interface{}
Parent interface{}
}
// Represents the "items" field in a _bulk response