diff --git a/goes.go b/goes.go index 107ce6a..1b7d46d 100644 --- a/goes.go +++ b/goes.go @@ -353,8 +353,9 @@ func (req *Request) Run() (*Response, error) { } if req.method != "HEAD" { - err = json.Unmarshal(body, &esResp) - if err != nil { + dec := json.NewDecoder(bytes.NewReader(body)) + dec.UseNumber() + if err := dec.Decode(&esResp); err != nil { return esResp, err } err = json.Unmarshal(body, &esResp.Raw) diff --git a/goes_test.go b/goes_test.go index a69fcb1..3c5ee86 100644 --- a/goes_test.go +++ b/goes_test.go @@ -13,6 +13,7 @@ import ( "time" . "github.com/go-check/check" + "encoding/json" ) var ( @@ -21,7 +22,9 @@ var ( ) // Hook up gocheck into the gotest runner. -func Test(t *testing.T) { TestingT(t) } +func Test(t *testing.T) { + TestingT(t) +} type GoesTestSuite struct{} @@ -56,7 +59,7 @@ func (s *GoesTestSuite) TestWithClient(c *C) { c.Assert(conn, DeepEquals, &Connection{ES_HOST, ES_PORT, cl}) c.Assert(conn.Client.Transport.(*http.Transport).DisableCompression, Equals, true) - c.Assert(conn.Client.Transport.(*http.Transport).ResponseHeaderTimeout, Equals, 1*time.Second) + c.Assert(conn.Client.Transport.(*http.Transport).ResponseHeaderTimeout, Equals, 1 * time.Second) } func (s *GoesTestSuite) TestUrl(c *C) { @@ -71,21 +74,21 @@ func (s *GoesTestSuite) TestUrl(c *C) { api: "_search", } - c.Assert(r.Url(), Equals, "http://"+ES_HOST+":"+ES_PORT+"/i/_search") + c.Assert(r.Url(), Equals, "http://" + ES_HOST + ":" + ES_PORT + "/i/_search") r.IndexList = []string{"a", "b"} - c.Assert(r.Url(), Equals, "http://"+ES_HOST+":"+ES_PORT+"/a,b/_search") + c.Assert(r.Url(), Equals, "http://" + ES_HOST + ":" + ES_PORT + "/a,b/_search") r.TypeList = []string{"c", "d"} - c.Assert(r.Url(), Equals, "http://"+ES_HOST+":"+ES_PORT+"/a,b/c,d/_search") + c.Assert(r.Url(), Equals, "http://" + ES_HOST + ":" + ES_PORT + "/a,b/c,d/_search") r.ExtraArgs = make(url.Values, 1) r.ExtraArgs.Set("version", "1") - c.Assert(r.Url(), Equals, "http://"+ES_HOST+":"+ES_PORT+"/a,b/c,d/_search?version=1") + c.Assert(r.Url(), Equals, "http://" + ES_HOST + ":" + ES_PORT + "/a,b/c,d/_search?version=1") r.id = "1234" r.api = "" - c.Assert(r.Url(), Equals, "http://"+ES_HOST+":"+ES_PORT+"/a,b/c,d/1234/?version=1") + c.Assert(r.Url(), Equals, "http://" + ES_HOST + ":" + ES_PORT + "/a,b/c,d/1234/?version=1") } func (s *GoesTestSuite) TestEsDown(c *C) { @@ -735,6 +738,13 @@ func (s *GoesTestSuite) TestSearch(c *C) { } response, err := conn.Search(query, []string{indexName}, []string{docType}, url.Values{}) + + //Type of response.Hits.MaxScore is json.Number, and DeepEquals will return false, + // so we need to change it manualy + jsonNumberMaxScore, _ := response.Hits.MaxScore.(json.Number) + maxScore, _ := jsonNumberMaxScore.Float64() + response.Hits.MaxScore = maxScore + expectedHits := Hits{ Total: 1, MaxScore: 1.0, @@ -1066,18 +1076,30 @@ func (s *GoesTestSuite) TestAggregations(c *C) { c.Assert(user.Buckets()[1].Key(), Equals, "foo") barAge := user.Buckets()[0].Aggregation("age") - c.Assert(barAge["count"], Equals, 1.0) - c.Assert(barAge["sum"], Equals, 30.0) + jCount, _ := barAge["count"].(json.Number) + countInt64, _ := jCount.Int64() + c.Assert(countInt64, Equals, int64(1)) + jSum, _ := barAge["sum"].(json.Number) + sumFloat64, _ := jSum.Float64() + c.Assert(sumFloat64, Equals, 30.0) fooAge := user.Buckets()[1].Aggregation("age") - c.Assert(fooAge["count"], Equals, 1.0) - c.Assert(fooAge["sum"], Equals, 25.0) + jCount, _ = fooAge["count"].(json.Number) + countInt64, _ = jCount.Int64() + c.Assert(countInt64, Equals, int64(1)) + jSum, _ = fooAge["sum"].(json.Number) + sumFloat64, _ = jSum.Float64() + c.Assert(sumFloat64, Equals, 25.0) age, ok := resp.Aggregations["age"] c.Assert(ok, Equals, true) - c.Assert(age["count"], Equals, 2.0) - c.Assert(age["sum"], Equals, 25.0+30.0) + jCount, _ = age["count"].(json.Number) + countInt64, _ = jCount.Int64() + c.Assert(countInt64, Equals, int64(2)) + jSum, _ = age["sum"].(json.Number) + sumFloat64, _ = jSum.Float64() + c.Assert(sumFloat64, Equals, 25.0 + 30.0) } func (s *GoesTestSuite) TestPutMapping(c *C) { @@ -1397,7 +1419,7 @@ func (s *GoesTestSuite) TestRemoveAlias(c *C) { // Get document via alias _, err = conn.Get(aliasName, docType, docId, url.Values{}) - c.Assert(err.Error(), Equals, "[404] IndexMissingException[["+aliasName+"] missing]") + c.Assert(err.Error(), Equals, "[404] IndexMissingException[[" + aliasName + "] missing]") } func (s *GoesTestSuite) TestAliasExists(c *C) {