forked from mattbaird/elastigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.go
41 lines (36 loc) · 745 Bytes
/
sort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package search
import (
"encoding/json"
"fmt"
)
// Sorting accepts any number of Sort commands
//
// Query().Sort(
// Sort("last_name").Desc(),
// Sort("age"),
// )
func Sort(field string) *SortDsl {
return &SortDsl{Name: field}
}
type SortBody []interface{}
type SortDsl struct {
Name string
IsDesc bool
}
func (s *SortDsl) Desc() *SortDsl {
s.IsDesc = true
return s
}
func (s *SortDsl) Asc() *SortDsl {
s.IsDesc = false
return s
}
func (s *SortDsl) MarshalJSON() ([]byte, error) {
if s.IsDesc {
return json.Marshal(map[string]string{s.Name: "desc"})
}
if s.Name == "_score" {
return []byte(`"_score"`), nil
}
return []byte(fmt.Sprintf(`"%s"`, s.Name)), nil // "user" assuming default = asc?
}