-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexp_function.go
More file actions
173 lines (158 loc) · 3.93 KB
/
exp_function.go
File metadata and controls
173 lines (158 loc) · 3.93 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package picker
import (
"github.com/chanced/dynamic"
)
type ExpDecayFunctionParams struct {
// Field
Field string
// Weight (float)
Weight interface{}
// The point of origin used for calculating distance. Must be given as a
// number for numeric field, date for date fields and geo point for geo
// fields. Required for geo and numeric field. For date fields the default
// is now. Date math (for example now-1h) is supported for origin.
Origin interface{}
// Required for all types. Defines the distance from origin + offset at
// which the computed score will equal decay parameter. For geo fields: Can
// be defined as number+unit (1km, 12m,…). Default unit is meters. For date
// fields: Can to be defined as a number+unit ("1h", "10d",…). Default unit
// is milliseconds. For numeric field: Any number.
Scale interface{}
// If an offset is defined, the decay function will only compute the decay
// function for documents with a distance greater than the defined offset.
// The default is 0.
Offset interface{}
// The decay parameter defines how documents are scored at the distance
// given at scale. If no decay is defined, documents at the distance scale
// will be scored 0.5.
Decay interface{}
Filter CompleteClauser
}
func (ExpDecayFunctionParams) FuncKind() FuncKind {
return FuncKindExp
}
func (e ExpDecayFunctionParams) Function() (Function, error) {
f := &ExpDecayFunction{}
err := f.SetField(e.Field)
if err != nil {
return f, err
}
err = f.SetWeight(e.Weight)
if err != nil {
return f, err
}
err = f.SetOrigin(e.Origin)
if err != nil {
return f, err
}
err = f.SetFilter(e.Filter)
if err != nil {
return f, err
}
err = f.SetScale(e.Scale)
if err != nil {
return f, err
}
err = f.SetOffset(e.Offset)
if err != nil {
return f, err
}
err = f.SetDecay(e.Decay)
if err != nil {
return f, err
}
return f, nil
}
type ExpDecayFunction struct {
weightParam
field string
origin interface{}
offset dynamic.StringNumberOrTime
filter QueryClause
decay dynamic.Number
scale dynamic.StringOrNumber
}
func (e *ExpDecayFunction) Field() string {
if e == nil {
return ""
}
return e.field
}
func (e *ExpDecayFunction) SetField(field string) error {
if len(field) == 0 {
return ErrFieldRequired
}
e.field = field
return nil
}
func (ExpDecayFunction) FuncKind() FuncKind {
return FuncKindExp
}
func (e ExpDecayFunction) Filter() QueryClause {
return e.filter
}
func (e *ExpDecayFunction) Decay() dynamic.Number {
return e.decay
}
func (e *ExpDecayFunction) SetDecay(value interface{}) error {
return e.decay.Set(value)
}
func (e *ExpDecayFunction) SetScale(scale interface{}) error {
if scale == nil {
return ErrScaleRequired
}
err := e.scale.Set(scale)
if err != nil {
return err
}
if e.scale.IsEmptyString() {
return ErrScaleRequired
}
return nil
}
func (e ExpDecayFunction) Scale() dynamic.StringOrNumber {
return e.scale
}
func (e ExpDecayFunction) Origin() interface{} {
return e.origin
}
func (e *ExpDecayFunction) Offset() dynamic.StringNumberOrTime {
return e.offset
}
func (e *ExpDecayFunction) SetOffset(offset interface{}) error {
return e.offset.Set(offset)
}
func (e *ExpDecayFunction) SetFilter(c CompleteClauser) error {
if c == nil {
e.filter = nil
return nil
}
qc, err := c.Clause()
if err != nil {
return err
}
e.filter = qc
return nil
}
func (e *ExpDecayFunction) SetOrigin(origin interface{}) error {
if origin == nil {
return ErrOriginRequired
}
if s, ok := origin.(string); ok && len(s) == 0 {
return ErrOriginRequired
}
e.origin = origin
return nil
}
func (e *ExpDecayFunction) unmarshalParams(data dynamic.JSON) error {
return unmarshalDecayFunction(data, e)
}
func (e ExpDecayFunction) MarshalBSON() ([]byte, error) {
return e.MarshalJSON()
}
func (e ExpDecayFunction) MarshalJSON() ([]byte, error) {
return marshalFunction(&e)
}
func (e *ExpDecayFunction) marshalParams(data dynamic.JSONObject) error {
return marshalDecayFunction(data, e)
}