Skip to content

Commit

Permalink
Fix filter-only queries having null scores which dont serialize, closes
Browse files Browse the repository at this point in the history
  • Loading branch information
araddon committed Feb 8, 2013
1 parent 2eee4e6 commit da60386
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
26 changes: 24 additions & 2 deletions core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/mattbaird/elastigo/api"
"log"
"net/url"
"strconv"
)

var (
Expand Down Expand Up @@ -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
}
5 changes: 4 additions & 1 deletion search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit da60386

Please sign in to comment.