diff --git a/goes.go b/goes.go index 6d737c4..8fae73c 100644 --- a/goes.go +++ b/goes.go @@ -75,6 +75,20 @@ func (c *Connection) RefreshIndex(name string) (Response, error) { return r.Run() } +// Optimize an index represented by a name, extra args are also allowed please check: +// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-optimize.html#indices-optimize +func (c *Connection) Optimize(indexList []string, extraArgs url.Values) (Response, error) { + r := Request{ + Conn: c, + IndexList: indexList, + ExtraArgs: extraArgs, + method: "POST", + api: "_optimize", + } + + return r.Run() +} + // Stats fetches statistics (_stats) for the current elasticsearch server func (c *Connection) Stats(indexList []string, extraArgs url.Values) (Response, error) { r := Request{ diff --git a/goes_test.go b/goes_test.go index ec3f492..e1db2db 100644 --- a/goes_test.go +++ b/goes_test.go @@ -190,6 +190,26 @@ func (s *GoesTestSuite) TestRefreshIndex(c *C) { c.Assert(err, IsNil) } +func (s *GoesTestSuite) TestOptimize(c *C) { + conn := NewConnection(ES_HOST, ES_PORT) + indexName := "testoptimize" + + conn.DeleteIndex(indexName) + _, err := conn.CreateIndex(indexName, map[string]interface{}{}) + c.Assert(err, IsNil) + + // we must wait for a bit otherwise ES crashes + time.Sleep(1 * time.Second) + + response, err := conn.Optimize([]string{indexName}, url.Values{"max_num_segments" : []string{"1"}}) + c.Assert(err, IsNil) + + c.Assert(response.All.Indices[indexName].Primaries["docs"].Count, Equals, 0) + + _, err = conn.DeleteIndex(indexName) + c.Assert(err, IsNil) +} + func (s *GoesTestSuite) TestBulkSend(c *C) { indexName := "testbulkadd" docType := "tweet"