Skip to content

Commit

Permalink
ESFF-2708: allow sorts to set multiple (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesmitharoo authored May 2, 2024
1 parent dfb993a commit 91b3efc
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 27 deletions.
2 changes: 1 addition & 1 deletion aggs_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (agg *TermsAggregation) Aggs(aggs ...Aggregation) *TermsAggregation {
return agg
}

// Order sets the sort for terms agg
// Order sets the sorts for terms agg
func (agg *TermsAggregation) Order(order map[string]string) *TermsAggregation {
agg.order = order
return agg
Expand Down
17 changes: 6 additions & 11 deletions aggs_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ type TopHitsAgg struct {
name string
from uint64
size uint64
sort []map[string]interface{}
sorts Sorts
source Source
}

Expand Down Expand Up @@ -445,15 +445,10 @@ func (agg *TopHitsAgg) Size(size uint64) *TopHitsAgg {
return agg
}

// Sort sets how the top matching hits should be sorted. By default the hits are
// Sorts sets how the top matching hits should be sorted. By default the hits are
// sorted by the score of the main query.
func (agg *TopHitsAgg) Sort(name string, order Order) *TopHitsAgg {
agg.sort = append(agg.sort, map[string]interface{}{
name: map[string]interface{}{
"order": order,
},
})

func (agg *TopHitsAgg) Sorts(sorts ...map[string]Sort) *TopHitsAgg {
agg.sorts = sorts
return agg
}

Expand All @@ -474,8 +469,8 @@ func (agg *TopHitsAgg) Map() map[string]interface{} {
if agg.size > 0 {
innerMap["size"] = agg.size
}
if len(agg.sort) > 0 {
innerMap["sort"] = agg.sort
if len(agg.sorts) > 0 {
innerMap["sort"] = agg.sorts
}
if len(agg.source.includes) > 0 {
innerMap["_source"] = agg.source.Map()
Expand Down
33 changes: 33 additions & 0 deletions aggs_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,38 @@ func TestMetricAggs(t *testing.T) {
},
},
},
{
"top_hits agg",
TopHits("top_hits").Sorts(
Sorts{
{
"field_1": {
Order: OrderDesc,
},
},
{
"field_2": {
Order: OrderAsc,
},
},
}...,
),
map[string]interface{}{
"top_hits": map[string]interface{}{
"sort": []map[string]interface{}{
{
"field_1": map[string]interface{}{
"order": OrderDesc,
},
},
{
"field_2": map[string]interface{}{
"order": OrderAsc,
},
},
},
},
},
},
})
}
8 changes: 6 additions & 2 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ func (source Source) Map() map[string]interface{} {
return m
}

// Sort represents a list of keys to sort by.
type Sort []map[string]interface{}
// Sorts represents a list of keys to sort by.
type Sorts []map[string]Sort

type Sort struct {
Order Order `json:"order"`
}

// Order is the ordering for a sort key (ascending, descending).
type Order string
Expand Down
17 changes: 6 additions & 11 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type SearchRequest struct {
postFilter Mappable
query Mappable
size *uint64
sort Sort
sorts Sorts
source Source
timeout *time.Duration
}
Expand Down Expand Up @@ -64,14 +64,9 @@ func (req *SearchRequest) Size(size uint64) *SearchRequest {
return req
}

// Sort sets how the results should be sorted.
func (req *SearchRequest) Sort(name string, order Order) *SearchRequest {
req.sort = append(req.sort, map[string]interface{}{
name: map[string]interface{}{
"order": order,
},
})

// Sorts sets how the results should be sorted.
func (req *SearchRequest) Sorts(sorts ...map[string]Sort) *SearchRequest {
req.sorts = sorts
return req
}

Expand Down Expand Up @@ -133,8 +128,8 @@ func (req *SearchRequest) Map() map[string]interface{} {
if req.size != nil {
m["size"] = *req.size
}
if len(req.sort) > 0 {
m["sort"] = req.sort
if len(req.sorts) > 0 {
m["sort"] = req.sorts
}
if req.from != nil {
m["from"] = *req.from
Expand Down
16 changes: 14 additions & 2 deletions search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,20 @@ func TestSearchMaps(t *testing.T) {
Size(30).
From(5).
Explain(true).
Sort("field_1", OrderDesc).
Sort("field_2", OrderAsc).
Sorts(
Sorts{
{
"field_1": {
Order: OrderDesc,
},
},
{
"field_2": {
Order: OrderAsc,
},
},
}...,
).
SourceIncludes("field_1", "field_2").
SourceExcludes("field_3").
Timeout(time.Duration(20000000000)),
Expand Down

0 comments on commit 91b3efc

Please sign in to comment.