diff --git a/goes.go b/goes.go index 107ce6a..7a57c72 100644 --- a/goes.go +++ b/goes.go @@ -32,7 +32,7 @@ func (err *SearchError) Error() string { // This function is pretty useless for now but might be useful in a near future // if wee need more features like connection pooling or load balancing. func NewConnection(host string, port string) *Connection { - return &Connection{host, port, http.DefaultClient} + return &Connection{host, port, http.DefaultClient, false} } func (c *Connection) WithClient(cl *http.Client) *Connection { @@ -342,6 +342,27 @@ func (c *Connection) Delete(d Document, extraArgs url.Values) (*Response, error) return r.Run() } +// Enable use number behaviour +func (c *Connection) UseNumberEnable() { + c.UseNumber = true +} + +func (req *Request) unmarshalBody(body []byte, esResp *Response) error { + if !req.Conn.UseNumber { + err := json.Unmarshal(body, esResp) + if err != nil { + return err + } + } else { + dec := json.NewDecoder(bytes.NewReader(body)) + dec.UseNumber() + if err := dec.Decode(esResp); err != nil { + return err + } + } + return nil +} + // Run executes an elasticsearch Request. It converts data to Json, sends the // request and returns the Response obtained func (req *Request) Run() (*Response, error) { @@ -353,7 +374,7 @@ func (req *Request) Run() (*Response, error) { } if req.method != "HEAD" { - err = json.Unmarshal(body, &esResp) + err = req.unmarshalBody(body, esResp) if err != nil { return esResp, err } diff --git a/goes_test.go b/goes_test.go index a69fcb1..c5c583d 100644 --- a/goes_test.go +++ b/goes_test.go @@ -41,7 +41,7 @@ func (s *GoesTestSuite) SetUpTest(c *C) { func (s *GoesTestSuite) TestNewConnection(c *C) { conn := NewConnection(ES_HOST, ES_PORT) - c.Assert(conn, DeepEquals, &Connection{ES_HOST, ES_PORT, http.DefaultClient}) + c.Assert(conn, DeepEquals, &Connection{ES_HOST, ES_PORT, http.DefaultClient, false}) } func (s *GoesTestSuite) TestWithClient(c *C) { @@ -54,7 +54,7 @@ func (s *GoesTestSuite) TestWithClient(c *C) { } conn := NewConnection(ES_HOST, ES_PORT).WithClient(cl) - c.Assert(conn, DeepEquals, &Connection{ES_HOST, ES_PORT, cl}) + c.Assert(conn, DeepEquals, &Connection{ES_HOST, ES_PORT, cl, false}) c.Assert(conn.Client.Transport.(*http.Transport).DisableCompression, Equals, true) c.Assert(conn.Client.Transport.(*http.Transport).ResponseHeaderTimeout, Equals, 1*time.Second) } diff --git a/structs.go b/structs.go index c726461..70bdbab 100644 --- a/structs.go +++ b/structs.go @@ -20,6 +20,9 @@ type Connection struct { // Client is the http client used to make requests, allowing settings things // such as timeouts etc Client *http.Client + + // Specify to use numbers when unmarshal + UseNumber bool } // Represents a Request to elasticsearch