Merge pull request #29 from pib/pointer_response

Return *Response instead of Response so response data is not copied on return
This commit is contained in:
Jérôme Renard 2015-07-17 06:29:23 +02:00
commit d40cba666e
2 changed files with 47 additions and 47 deletions

72
goes.go
View File

@ -41,7 +41,7 @@ func (c *Connection) WithClient(cl *http.Client) *Connection {
} }
// CreateIndex creates a new index represented by a name and a mapping // CreateIndex creates a new index represented by a name and a mapping
func (c *Connection) CreateIndex(name string, mapping interface{}) (Response, error) { func (c *Connection) CreateIndex(name string, mapping interface{}) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
Query: mapping, Query: mapping,
@ -53,7 +53,7 @@ func (c *Connection) CreateIndex(name string, mapping interface{}) (Response, er
} }
// DeleteIndex deletes an index represented by a name // DeleteIndex deletes an index represented by a name
func (c *Connection) DeleteIndex(name string) (Response, error) { func (c *Connection) DeleteIndex(name string) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
IndexList: []string{name}, IndexList: []string{name},
@ -64,7 +64,7 @@ func (c *Connection) DeleteIndex(name string) (Response, error) {
} }
// RefreshIndex refreshes an index represented by a name // RefreshIndex refreshes an index represented by a name
func (c *Connection) RefreshIndex(name string) (Response, error) { func (c *Connection) RefreshIndex(name string) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
IndexList: []string{name}, IndexList: []string{name},
@ -77,7 +77,7 @@ func (c *Connection) RefreshIndex(name string) (Response, error) {
// Optimize an index represented by a name, extra args are also allowed please check: // 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 // 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) { func (c *Connection) Optimize(indexList []string, extraArgs url.Values) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
IndexList: indexList, IndexList: indexList,
@ -90,7 +90,7 @@ func (c *Connection) Optimize(indexList []string, extraArgs url.Values) (Respons
} }
// Stats fetches statistics (_stats) for the current elasticsearch server // Stats fetches statistics (_stats) for the current elasticsearch server
func (c *Connection) Stats(indexList []string, extraArgs url.Values) (Response, error) { func (c *Connection) Stats(indexList []string, extraArgs url.Values) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
IndexList: indexList, IndexList: indexList,
@ -104,7 +104,7 @@ func (c *Connection) Stats(indexList []string, extraArgs url.Values) (Response,
// IndexStatus fetches the status (_status) for the indices defined in // IndexStatus fetches the status (_status) for the indices defined in
// indexList. Use _all in indexList to get stats for all indices // indexList. Use _all in indexList to get stats for all indices
func (c *Connection) IndexStatus(indexList []string) (Response, error) { func (c *Connection) IndexStatus(indexList []string) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
IndexList: indexList, IndexList: indexList,
@ -116,7 +116,7 @@ func (c *Connection) IndexStatus(indexList []string) (Response, error) {
} }
// Bulk adds multiple documents in bulk mode // Bulk adds multiple documents in bulk mode
func (c *Connection) BulkSend(documents []Document) (Response, error) { func (c *Connection) BulkSend(documents []Document) (*Response, error) {
// We do not generate a traditional JSON here (often a one liner) // We do not generate a traditional JSON here (often a one liner)
// Elasticsearch expects one line of JSON per line (EOL = \n) // Elasticsearch expects one line of JSON per line (EOL = \n)
// plus an extra \n at the very end of the document // plus an extra \n at the very end of the document
@ -148,7 +148,7 @@ func (c *Connection) BulkSend(documents []Document) (Response, error) {
}) })
if err != nil { if err != nil {
return Response{}, err return &Response{}, err
} }
bulkData[i] = action bulkData[i] = action
@ -165,7 +165,7 @@ func (c *Connection) BulkSend(documents []Document) (Response, error) {
typeOfFields = typeOfFields.Elem() typeOfFields = typeOfFields.Elem()
} }
if typeOfFields.Kind() != reflect.Struct { if typeOfFields.Kind() != reflect.Struct {
return Response{}, fmt.Errorf("Document fields not in struct or map[string]interface{} format") return &Response{}, fmt.Errorf("Document fields not in struct or map[string]interface{} format")
} }
if typeOfFields.NumField() == 0 { if typeOfFields.NumField() == 0 {
continue continue
@ -174,7 +174,7 @@ func (c *Connection) BulkSend(documents []Document) (Response, error) {
sources, err := json.Marshal(doc.Fields) sources, err := json.Marshal(doc.Fields)
if err != nil { if err != nil {
return Response{}, err return &Response{}, err
} }
bulkData[i] = sources bulkData[i] = sources
@ -196,7 +196,7 @@ func (c *Connection) BulkSend(documents []Document) (Response, error) {
} }
// Search executes a search query against an index // Search executes a search query against an index
func (c *Connection) Search(query interface{}, indexList []string, typeList []string, extraArgs url.Values) (Response, error) { func (c *Connection) Search(query interface{}, indexList []string, typeList []string, extraArgs url.Values) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
Query: query, Query: query,
@ -211,7 +211,7 @@ func (c *Connection) Search(query interface{}, indexList []string, typeList []st
} }
// Count executes a count query against an index, use the Count field in the response for the result // Count executes a count query against an index, use the Count field in the response for the result
func (c *Connection) Count(query interface{}, indexList []string, typeList []string, extraArgs url.Values) (Response, error) { func (c *Connection) Count(query interface{}, indexList []string, typeList []string, extraArgs url.Values) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
Query: query, Query: query,
@ -228,7 +228,7 @@ func (c *Connection) Count(query interface{}, indexList []string, typeList []str
//Query runs a query against an index using the provided http method. //Query runs a query against an index using the provided http method.
//This method can be used to execute a delete by query, just pass in "DELETE" //This method can be used to execute a delete by query, just pass in "DELETE"
//for the HTTP method. //for the HTTP method.
func (c *Connection) Query(query interface{}, indexList []string, typeList []string, httpMethod string, extraArgs url.Values) (Response, error) { func (c *Connection) Query(query interface{}, indexList []string, typeList []string, httpMethod string, extraArgs url.Values) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
Query: query, Query: query,
@ -243,7 +243,7 @@ func (c *Connection) Query(query interface{}, indexList []string, typeList []str
} }
// Scan starts scroll over an index // Scan starts scroll over an index
func (c *Connection) Scan(query interface{}, indexList []string, typeList []string, timeout string, size int) (Response, error) { func (c *Connection) Scan(query interface{}, indexList []string, typeList []string, timeout string, size int) (*Response, error) {
v := url.Values{} v := url.Values{}
v.Add("search_type", "scan") v.Add("search_type", "scan")
v.Add("scroll", timeout) v.Add("scroll", timeout)
@ -263,7 +263,7 @@ func (c *Connection) Scan(query interface{}, indexList []string, typeList []stri
} }
// Scroll fetches data by scroll id // Scroll fetches data by scroll id
func (c *Connection) Scroll(scrollId string, timeout string) (Response, error) { func (c *Connection) Scroll(scrollId string, timeout string) (*Response, error) {
v := url.Values{} v := url.Values{}
v.Add("scroll", timeout) v.Add("scroll", timeout)
@ -279,7 +279,7 @@ func (c *Connection) Scroll(scrollId string, timeout string) (Response, error) {
} }
// Get a typed document by its id // Get a typed document by its id
func (c *Connection) Get(index string, documentType string, id string, extraArgs url.Values) (Response, error) { func (c *Connection) Get(index string, documentType string, id string, extraArgs url.Values) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
IndexList: []string{index}, IndexList: []string{index},
@ -294,7 +294,7 @@ func (c *Connection) Get(index string, documentType string, id string, extraArgs
// Index indexes a Document // Index indexes a Document
// The extraArgs is a list of url.Values that you can send to elasticsearch as // 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. // URL arguments, for example, to control routing, ttl, version, op_type, etc.
func (c *Connection) Index(d Document, extraArgs url.Values) (Response, error) { func (c *Connection) Index(d Document, extraArgs url.Values) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
Query: d.Fields, Query: d.Fields,
@ -315,7 +315,7 @@ func (c *Connection) Index(d Document, extraArgs url.Values) (Response, error) {
// Delete deletes a Document d // Delete deletes a Document d
// The extraArgs is a list of url.Values that you can send to elasticsearch as // The extraArgs is a list of url.Values that you can send to elasticsearch as
// URL arguments, for example, to control routing. // URL arguments, for example, to control routing.
func (c *Connection) Delete(d Document, extraArgs url.Values) (Response, error) { func (c *Connection) Delete(d Document, extraArgs url.Values) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
IndexList: []string{d.Index.(string)}, IndexList: []string{d.Index.(string)},
@ -330,7 +330,7 @@ func (c *Connection) Delete(d Document, extraArgs url.Values) (Response, error)
// Run executes an elasticsearch Request. It converts data to Json, sends the // Run executes an elasticsearch Request. It converts data to Json, sends the
// request and return the Response obtained // request and return the Response obtained
func (req *Request) Run() (Response, error) { func (req *Request) Run() (*Response, error) {
postData := []byte{} postData := []byte{}
// XXX : refactor this // XXX : refactor this
@ -341,7 +341,7 @@ func (req *Request) Run() (Response, error) {
} else { } else {
b, err := json.Marshal(req.Query) b, err := json.Marshal(req.Query)
if err != nil { if err != nil {
return Response{}, err return &Response{}, err
} }
postData = b postData = b
} }
@ -350,7 +350,7 @@ func (req *Request) Run() (Response, error) {
newReq, err := http.NewRequest(req.method, req.Url(), reader) newReq, err := http.NewRequest(req.method, req.Url(), reader)
if err != nil { if err != nil {
return Response{}, err return &Response{}, err
} }
if req.method == "POST" || req.method == "PUT" { if req.method == "POST" || req.method == "PUT" {
@ -359,18 +359,18 @@ func (req *Request) Run() (Response, error) {
resp, err := req.Conn.Client.Do(newReq) resp, err := req.Conn.Client.Do(newReq)
if err != nil { if err != nil {
return Response{}, err return &Response{}, err
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return Response{}, err return &Response{}, err
} }
if resp.StatusCode > 201 && resp.StatusCode < 400 { if resp.StatusCode > 201 && resp.StatusCode < 400 {
return Response{}, errors.New(string(body)) return &Response{}, errors.New(string(body))
} }
esResp := new(Response) esResp := new(Response)
@ -379,7 +379,7 @@ func (req *Request) Run() (Response, error) {
} else { } else {
err = json.Unmarshal(body, &esResp) err = json.Unmarshal(body, &esResp)
if err != nil { if err != nil {
return Response{}, err return &Response{}, err
} }
json.Unmarshal(body, &esResp.Raw) json.Unmarshal(body, &esResp.Raw)
} }
@ -388,18 +388,18 @@ func (req *Request) Run() (Response, error) {
for _, item := range esResp.Items { for _, item := range esResp.Items {
for _, i := range item { for _, i := range item {
if i.Error != "" { if i.Error != "" {
return Response{}, &SearchError{i.Error, i.Status} return &Response{}, &SearchError{i.Error, i.Status}
} }
} }
} }
return Response{}, &SearchError{Msg: "Unknown error while bulk indexing"} return &Response{}, &SearchError{Msg: "Unknown error while bulk indexing"}
} }
if esResp.Error != "" { if esResp.Error != "" {
return Response{}, &SearchError{esResp.Error, esResp.Status} return &Response{}, &SearchError{esResp.Error, esResp.Status}
} }
return *esResp, nil return esResp, nil
} }
// Url builds a Request for a URL // Url builds a Request for a URL
@ -459,7 +459,7 @@ func (b Bucket) Aggregation(name string) Aggregation {
} }
// PutMapping registers a specific mapping for one or more types in one or more indexes // PutMapping registers a specific mapping for one or more types in one or more indexes
func (c *Connection) PutMapping(typeName string, mapping interface{}, indexes []string) (Response, error) { func (c *Connection) PutMapping(typeName string, mapping interface{}, indexes []string) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
@ -472,7 +472,7 @@ func (c *Connection) PutMapping(typeName string, mapping interface{}, indexes []
return r.Run() return r.Run()
} }
func (c *Connection) GetMapping(types []string, indexes []string) (Response, error) { func (c *Connection) GetMapping(types []string, indexes []string) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
@ -498,7 +498,7 @@ func (c *Connection) IndicesExist(indexes []string) (bool, error) {
return resp.Status == 200, err return resp.Status == 200, err
} }
func (c *Connection) Update(d Document, query interface{}, extraArgs url.Values) (Response, error) { func (c *Connection) Update(d Document, query interface{}, extraArgs url.Values) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
Query: query, Query: query,
@ -517,7 +517,7 @@ func (c *Connection) Update(d Document, query interface{}, extraArgs url.Values)
} }
// DeleteMapping deletes a mapping along with all data in the type // DeleteMapping deletes a mapping along with all data in the type
func (c *Connection) DeleteMapping(typeName string, indexes []string) (Response, error) { func (c *Connection) DeleteMapping(typeName string, indexes []string) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
@ -529,7 +529,7 @@ func (c *Connection) DeleteMapping(typeName string, indexes []string) (Response,
return r.Run() return r.Run()
} }
func (c *Connection) modifyAlias(action string, alias string, indexes []string) (Response, error) { func (c *Connection) modifyAlias(action string, alias string, indexes []string) (*Response, error) {
command := map[string]interface{}{ command := map[string]interface{}{
"actions": make([]map[string]interface{}, 1), "actions": make([]map[string]interface{}, 1),
} }
@ -554,12 +554,12 @@ func (c *Connection) modifyAlias(action string, alias string, indexes []string)
} }
// AddAlias creates an alias to one or more indexes // AddAlias creates an alias to one or more indexes
func (c *Connection) AddAlias(alias string, indexes []string) (Response, error) { func (c *Connection) AddAlias(alias string, indexes []string) (*Response, error) {
return c.modifyAlias("add", alias, indexes) return c.modifyAlias("add", alias, indexes)
} }
// RemoveAlias removes an alias to one or more indexes // RemoveAlias removes an alias to one or more indexes
func (c *Connection) RemoveAlias(alias string, indexes []string) (Response, error) { func (c *Connection) RemoveAlias(alias string, indexes []string) (*Response, error) {
return c.modifyAlias("remove", alias, indexes) return c.modifyAlias("remove", alias, indexes)
} }

View File

@ -156,7 +156,7 @@ func (s *GoesTestSuite) TestDeleteIndexInexistantIndex(c *C) {
resp, err := conn.DeleteIndex("foobar") resp, err := conn.DeleteIndex("foobar")
c.Assert(err.Error(), Equals, "[404] IndexMissingException[[foobar] missing]") c.Assert(err.Error(), Equals, "[404] IndexMissingException[[foobar] missing]")
c.Assert(resp, DeepEquals, Response{}) c.Assert(resp, DeepEquals, &Response{})
} }
func (s *GoesTestSuite) TestDeleteIndexExistingIndex(c *C) { func (s *GoesTestSuite) TestDeleteIndexExistingIndex(c *C) {
@ -171,7 +171,7 @@ func (s *GoesTestSuite) TestDeleteIndexExistingIndex(c *C) {
resp, err := conn.DeleteIndex(indexName) resp, err := conn.DeleteIndex(indexName)
c.Assert(err, IsNil) c.Assert(err, IsNil)
expectedResponse := Response{} expectedResponse := &Response{}
expectedResponse.Acknowledged = true expectedResponse.Acknowledged = true
resp.Raw = nil resp.Raw = nil
c.Assert(resp, DeepEquals, expectedResponse) c.Assert(resp, DeepEquals, expectedResponse)
@ -378,7 +378,7 @@ func (s *GoesTestSuite) TestIndexWithFieldsInStruct(c *C) {
response, err := conn.Index(d, extraArgs) response, err := conn.Index(d, extraArgs)
c.Assert(err, IsNil) c.Assert(err, IsNil)
expectedResponse := Response{ expectedResponse := &Response{
Index: indexName, Index: indexName,
Id: docId, Id: docId,
Type: docType, Type: docType,
@ -443,7 +443,7 @@ func (s *GoesTestSuite) TestIndexIdDefined(c *C) {
response, err := conn.Index(d, extraArgs) response, err := conn.Index(d, extraArgs)
c.Assert(err, IsNil) c.Assert(err, IsNil)
expectedResponse := Response{ expectedResponse := &Response{
Index: indexName, Index: indexName,
Id: docId, Id: docId,
Type: docType, Type: docType,
@ -512,7 +512,7 @@ func (s *GoesTestSuite) TestDelete(c *C) {
response, err := conn.Delete(d, url.Values{}) response, err := conn.Delete(d, url.Values{})
c.Assert(err, IsNil) c.Assert(err, IsNil)
expectedResponse := Response{ expectedResponse := &Response{
Found: true, Found: true,
Index: indexName, Index: indexName,
Type: docType, Type: docType,
@ -526,7 +526,7 @@ func (s *GoesTestSuite) TestDelete(c *C) {
response, err = conn.Delete(d, url.Values{}) response, err = conn.Delete(d, url.Values{})
c.Assert(err, IsNil) c.Assert(err, IsNil)
expectedResponse = Response{ expectedResponse = &Response{
Found: false, Found: false,
Index: indexName, Index: indexName,
Type: docType, Type: docType,
@ -587,7 +587,7 @@ func (s *GoesTestSuite) TestDeleteByQuery(c *C) {
c.Assert(err, IsNil) c.Assert(err, IsNil)
expectedResponse := Response{ expectedResponse := &Response{
Found: false, Found: false,
Index: "", Index: "",
Type: "", Type: "",
@ -632,7 +632,7 @@ func (s *GoesTestSuite) TestGet(c *C) {
response, err := conn.Get(indexName, docType, docId, url.Values{}) response, err := conn.Get(indexName, docType, docId, url.Values{})
c.Assert(err, IsNil) c.Assert(err, IsNil)
expectedResponse := Response{ expectedResponse := &Response{
Index: indexName, Index: indexName,
Type: docType, Type: docType,
Id: docId, Id: docId,
@ -649,7 +649,7 @@ func (s *GoesTestSuite) TestGet(c *C) {
response, err = conn.Get(indexName, docType, docId, fields) response, err = conn.Get(indexName, docType, docId, fields)
c.Assert(err, IsNil) c.Assert(err, IsNil)
expectedResponse = Response{ expectedResponse = &Response{
Index: indexName, Index: indexName,
Type: docType, Type: docType,
Id: docId, Id: docId,
@ -1146,7 +1146,7 @@ func (s *GoesTestSuite) TestUpdate(c *C) {
c.Assert(err, IsNil) c.Assert(err, IsNil)
time.Sleep(200 * time.Millisecond) time.Sleep(200 * time.Millisecond)
expectedResponse := Response{ expectedResponse := &Response{
Index: indexName, Index: indexName,
Id: docId, Id: docId,
Type: docType, Type: docType,
@ -1316,7 +1316,7 @@ func (s *GoesTestSuite) TestAddAlias(c *C) {
response, err := conn.Get(aliasName, docType, docId, url.Values{}) response, err := conn.Get(aliasName, docType, docId, url.Values{})
c.Assert(err, IsNil) c.Assert(err, IsNil)
expectedResponse := Response{ expectedResponse := &Response{
Index: indexName, Index: indexName,
Type: docType, Type: docType,
Id: docId, Id: docId,