diff --git a/goes.go b/goes.go index 4439b1a..52c4b8f 100644 --- a/goes.go +++ b/goes.go @@ -423,3 +423,17 @@ func (b Bucket) Aggregation(name string) Aggregation { return Aggregation{} } } + +// PutMapping registers a specific mapping for one or more types in one or more indexes +func (c *Connection) PutMapping(typeName string, mapping map[string]interface{}, indexes ...string) (Response, error) { + + r := Request{ + Conn: c, + Query: mapping, + IndexList: indexes, + method: "PUT", + api: "_mappings/" + typeName, + } + + return r.Run() +} diff --git a/goes_test.go b/goes_test.go index 49914e8..e223b54 100644 --- a/goes_test.go +++ b/goes_test.go @@ -970,3 +970,45 @@ func (s *GoesTestSuite) TestAggregations(c *C) { c.Assert(age["count"], Equals, 2.0) c.Assert(age["sum"], Equals, 25.0+30.0) } + +func (s *GoesTestSuite) TestPutMapping(c *C) { + indexName := "testputmapping" + 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) + + d := Document{ + Index: indexName, + Type: docType, + Fields: map[string]interface{}{ + "user": "foo", + "message": "bar", + }, + } + + response, err := conn.Index(d, 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, + }, + }, + }, + } + response, err = conn.PutMapping("tweet", mapping, indexName) + c.Assert(err, IsNil) + + c.Assert(response.Acknowledged, Equals, true) + c.Assert(response.TimedOut, Equals, false) +}