Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: keep sort in search hit as is #1576

Open
wants to merge 1 commit into
base: release-branch.v7
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion search.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,8 @@ type SearchHit struct {
Version *int64 `json:"_version,omitempty"` // version number, when Version is set to true in SearchService
SeqNo *int64 `json:"_seq_no"`
PrimaryTerm *int64 `json:"_primary_term"`
Sort []interface{} `json:"sort,omitempty"` // sort information
Sort []interface{} `json:"-"` // Deprecated, use RawSort instead
RawSort []json.RawMessage `json:"sort,omitempty"` // raw sort information, should be keeped as-is
Highlight SearchHitHighlight `json:"highlight,omitempty"` // highlighter information
Source json.RawMessage `json:"_source,omitempty"` // stored document source
Fields SearchHitFields `json:"fields,omitempty"` // returned (stored) fields
Expand All @@ -783,6 +784,25 @@ type SearchHit struct {
// MatchedFilters
}

type typeSearchHit SearchHit

// UnmarshalJSON unmarshals sort as []json.RawMessage into RawSort to keep it as is,
// and as []interface{} into Sort to keep backward compatible
func (sh *SearchHit) UnmarshalJSON(data []byte) error {
if e := json.Unmarshal(data, (*typeSearchHit)(sh)); e != nil {
return e
}
for _, s := range sh.RawSort {
var sort interface{}
if e := json.Unmarshal(s, &sort); e != nil {
return e
}
sh.Sort = append(sh.Sort, sort)
}

return nil
}

// SearchHitFields helps to simplify resolving slices of specific types.
type SearchHitFields map[string]interface{}

Expand Down