-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsort.go
104 lines (97 loc) · 2.99 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package es
import (
Mode "github.com/Trendyol/es-query-builder/es/enums/sort/mode"
Order "github.com/Trendyol/es-query-builder/es/enums/sort/order"
)
type sortType Object
// Sort creates a new es.sortType object with the specified field.
//
// This function initializes an es.sortType object with a given field name. The
// field is used to specify the sorting criteria in the search query. The
// resulting es.sortType can be further configured with sorting order and mode.
//
// Example usage:
//
// s := es.Sort("age")
// // s now includes an es.sortType with an "age" field that can be further configured.
//
// Parameters:
// - field: A string representing the field to sort by.
//
// Returns:
//
// An es.sortType object with the specified field.
func Sort(field string) sortType {
return sortType{
field: Object{},
}
}
// Order sets the "order" parameter in an es.sortType object.
//
// This method specifies the order in which the results should be sorted.
// It configures the es.sortType object to sort the results in ascending or
// descending order.
//
// Example usage:
//
// s := es.Sort("age").Order(Order.Desc)
// // s now includes an "order" parameter with the value "desc".
//
// Parameters:
// - order: An Order.Order value indicating the sorting order (e.g., ascending or descending).
//
// Returns:
//
// The updated es.sortType object with the "order" parameter set.
func (s sortType) Order(order Order.Order) sortType {
return s.putInTheField("order", order)
}
// Mode sets the "mode" parameter in an es.sortType object.
//
// This method specifies the mode used for sorting the results. The mode
// determines how sorting should be handled, such as by specifying different
// tie-breaking strategies.
//
// Example usage:
//
// s := es.Sort("age").Mode(Mode.Avg)
// // s now includes a "mode" parameter with the value "avg".
//
// Parameters:
// - mode: A Mode.Mode value indicating the sorting mode (e.g., average, minimum, maximum).
//
// Returns:
//
// The updated es.sortType object with the "mode" parameter set.
func (s sortType) Mode(mode Mode.Mode) sortType {
return s.putInTheField("mode", mode)
}
// Nested sets the "nested" parameter in an es.sortType object.
//
// This method specifies a nested sorting configuration for sorting fields
// within nested objects. It allows defining sorting behavior for fields
// inside nested documents.
//
// Example usage:
//
// s := es.Sort("user.age").Nested(es.NestedSort().Path("user"))
// // s now includes a "nested" parameter with the specified nested sorting configuration.
//
// Parameters:
// - nested: A nestedSortType value defining the nested sorting configuration.
//
// Returns:
//
// The updated es.sortType object with the "nested" parameter set.
func (s sortType) Nested(nested nestedSortType) sortType {
return s.putInTheField("nested", nested)
}
func (s sortType) putInTheField(key string, value any) sortType {
for _, fieldObj := range s {
if fieldObject, ok := fieldObj.(Object); ok {
fieldObject[key] = value
break
}
}
return s
}