expose simple string slice based sorting to top-level bleve
this change means simple sort requirements no longer require importing the search package (high-level API goal) also the sort test at the top-level was changed to use this form
This commit is contained in:
parent
27ba6187bc
commit
27f5c6ec92
|
@ -743,10 +743,7 @@ func TestSortMatchSearch(t *testing.T) {
|
|||
}
|
||||
|
||||
req := NewSearchRequest(NewMatchQuery("One"))
|
||||
req.SortBy(search.SortOrder{
|
||||
&search.SortField{Field: "Day"},
|
||||
&search.SortField{Field: "Name"},
|
||||
})
|
||||
req.SortBy([]string{"Day", "Name"})
|
||||
req.Fields = []string{"*"}
|
||||
sr, err := index.Search(req)
|
||||
if err != nil {
|
||||
|
|
12
search.go
12
search.go
|
@ -223,7 +223,17 @@ func (r *SearchRequest) AddFacet(facetName string, f *FacetRequest) {
|
|||
}
|
||||
|
||||
// SortBy changes the request to use the requested sort order
|
||||
func (r *SearchRequest) SortBy(order search.SortOrder) {
|
||||
// this form uses the simplified syntax with an array of strings
|
||||
// each string can either be a field name
|
||||
// or the magic value _id and _score which refer to the doc id and search score
|
||||
// any of these values can optionally be prefixed with - to reverse the order
|
||||
func (r *SearchRequest) SortBy(order []string) {
|
||||
so := search.ParseSortOrderStrings(order)
|
||||
r.Sort = so
|
||||
}
|
||||
|
||||
// SortByCustom changes the request to use the requested sort order
|
||||
func (r *SearchRequest) SortByCustom(order search.SortOrder) {
|
||||
r.Sort = order
|
||||
}
|
||||
|
||||
|
|
|
@ -98,7 +98,7 @@ func ParseSearchSortObj(input map[string]interface{}) (SearchSort, error) {
|
|||
return nil, fmt.Errorf("unknown search sort by: %s", by)
|
||||
}
|
||||
|
||||
func ParseSearchSortString(input string) (SearchSort, error) {
|
||||
func ParseSearchSortString(input string) SearchSort {
|
||||
descending := false
|
||||
if strings.HasPrefix(input, "-") {
|
||||
descending = true
|
||||
|
@ -109,16 +109,16 @@ func ParseSearchSortString(input string) (SearchSort, error) {
|
|||
if input == "_id" {
|
||||
return &SortDocID{
|
||||
Descending: descending,
|
||||
}, nil
|
||||
}
|
||||
} else if input == "_score" {
|
||||
return &SortScore{
|
||||
Descending: descending,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
return &SortField{
|
||||
Field: input,
|
||||
Descending: descending,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func ParseSearchSortJSON(input json.RawMessage) (SearchSort, error) {
|
||||
|
@ -133,7 +133,16 @@ func ParseSearchSortJSON(input json.RawMessage) (SearchSort, error) {
|
|||
}
|
||||
return ParseSearchSortObj(sortObj)
|
||||
}
|
||||
return ParseSearchSortString(sortString)
|
||||
return ParseSearchSortString(sortString), nil
|
||||
}
|
||||
|
||||
func ParseSortOrderStrings(in []string) SortOrder {
|
||||
rv := make(SortOrder, 0, len(in))
|
||||
for _, i := range in {
|
||||
ss := ParseSearchSortString(i)
|
||||
rv = append(rv, ss)
|
||||
}
|
||||
return rv
|
||||
}
|
||||
|
||||
func ParseSortOrderJSON(in []json.RawMessage) (SortOrder, error) {
|
||||
|
|
Loading…
Reference in New Issue