From da603867eb60d8bef29ccf06a84d4c38b8043e2c Mon Sep 17 00:00:00 2001 From: Aaron Raddon Date: Fri, 8 Feb 2013 04:45:53 +0000 Subject: [PATCH] Fix filter-only queries having null scores which dont serialize, closes #17 --- core/search.go | 26 ++++++++++++++++++++++++-- search/search.go | 5 ++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/core/search.go b/core/search.go index b02c9aef..15ad6ab1 100644 --- a/core/search.go +++ b/core/search.go @@ -6,6 +6,7 @@ import ( "github.com/mattbaird/elastigo/api" "log" "net/url" + "strconv" ) var ( @@ -131,6 +132,27 @@ type Hit struct { Index string `json:"_index"` Type string `json:"_type,omitempty"` Id string `json:"_id"` - Score float32 `json:"_score"` - Source json.RawMessage `json:"_source"` // marshalling left to consumer + Score Float32Nullable `json:"_score,omitempty"` // Filters (no query) dont have score, so is null + Source json.RawMessage `json:"_source"` // marshalling left to consumer +} + +// Elasticsearch returns some invalid (according to go) json, with floats having... +// +// json: cannot unmarshal null into Go value of type float32 (see last field.) +// +// "hits":{"total":6808,"max_score":null, +// "hits":[{"_index":"10user","_type":"user","_id":"751820","_score":null, +type Float32Nullable float32 + +func (i *Float32Nullable) UnmarshalJSON(data []byte) error { + if len(data) == 0 || string(data) == "null" { + return nil + } + + if in, err := strconv.ParseFloat(string(data), 32); err != nil { + return err + } else { + *i = Float32Nullable(in) + } + return nil } diff --git a/search/search.go b/search/search.go index a9583ab0..f41c0193 100644 --- a/search/search.go +++ b/search/search.go @@ -59,7 +59,10 @@ func (s *SearchDsl) Result() (*core.SearchResult, error) { Logf(ERROR, "%v", err) return nil, err } - jsonErr := json.Unmarshal([]byte(body), &retval) + jsonErr := json.Unmarshal(body, &retval) + if jsonErr != nil { + Logf(ERROR, "%v \n\t%s", jsonErr, string(body)) + } return &retval, jsonErr }