parse search results by converting strings back to errors
This commit is contained in:
parent
3badeb5fe1
commit
760057afb6
12
search.go
12
search.go
|
@ -295,6 +295,18 @@ func (iem IndexErrMap) MarshalJSON() ([]byte, error) {
|
|||
return json.Marshal(tmp)
|
||||
}
|
||||
|
||||
func (iem IndexErrMap) UnmarshalJSON(data []byte) error {
|
||||
var tmp map[string]string
|
||||
err := json.Unmarshal(data, &tmp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for k, v := range tmp {
|
||||
iem[k] = fmt.Errorf("%s", v)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SearchStatus is a secion in the SearchResult reporting how many
|
||||
// underlying indexes were queried, how many were successful/failed
|
||||
// and a map of any errors that were encountered
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
package bleve
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
@ -130,3 +131,50 @@ func TestSearchResultMerge(t *testing.T) {
|
|||
t.Errorf("expected %#v, got %#v", expected, l)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalingSearchResult(t *testing.T) {
|
||||
|
||||
searchResponse := []byte(`{
|
||||
"status":{
|
||||
"total":1,
|
||||
"failed":1,
|
||||
"successful":0,
|
||||
"errors":{
|
||||
"default_index_362ce020b3d62b13_348f5c3c":"context deadline exceeded"
|
||||
}
|
||||
},
|
||||
"request":{
|
||||
"query":{
|
||||
"match":"emp",
|
||||
"field":"type",
|
||||
"boost":1,
|
||||
"prefix_length":0,
|
||||
"fuzziness":0
|
||||
},
|
||||
"size":10000000,
|
||||
"from":0,
|
||||
"highlight":null,
|
||||
"fields":[],
|
||||
"facets":null,
|
||||
"explain":false
|
||||
},
|
||||
"hits":null,
|
||||
"total_hits":0,
|
||||
"max_score":0,
|
||||
"took":0,
|
||||
"facets":null
|
||||
}`)
|
||||
|
||||
rv := &SearchResult{
|
||||
Status: &SearchStatus{
|
||||
Errors: make(map[string]error),
|
||||
},
|
||||
}
|
||||
err = json.Unmarshal(searchResponse, rv)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if len(rv.Status.Errors) != 1 {
|
||||
t.Errorf("expected 1 error, got %d", len(rv.Status.Errors))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue