1
|
|
|
<?php namespace Nord\Lumen\Elasticsearch\Search\Scoring\Functions; |
2
|
|
|
|
3
|
|
|
use Nord\Lumen\Elasticsearch\Exceptions\InvalidArgument; |
4
|
|
|
use Nord\Lumen\Elasticsearch\Search\Traits\HasField; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Decay functions score a document with a function that decays depending on the distance of a numeric field value of |
8
|
|
|
* the document from a user given origin. This is similar to a range query, but with smooth edges instead of boxes. |
9
|
|
|
* |
10
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-decay |
11
|
|
|
*/ |
12
|
|
|
class DecayScoringFunction extends AbstractScoringFunction |
13
|
|
|
{ |
14
|
|
|
use HasField; |
15
|
|
|
|
16
|
|
|
const DECAY_FUNCTION_LINEAR = 'linear'; |
17
|
|
|
const DECAY_FUNCTION_EXPONENTIAL = 'exp'; |
18
|
|
|
const DECAY_FUNCTION_GAUSSIAN = 'gauss'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string One of the `DECAY_FUNCTION_` constants. |
22
|
|
|
*/ |
23
|
|
|
private $decayFunction; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var mixed The point of origin used for calculating distance. Must be given as a number for numeric field, date |
27
|
|
|
* for date fields and geo point for geo fields. Required for geo and numeric field. For date fields the default is |
28
|
|
|
* now. Date math (for example now-1h) is supported for origin. |
29
|
|
|
*/ |
30
|
|
|
private $origin; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var mixed Required for all types. Defines the distance from origin at which the computed score will equal decay |
34
|
|
|
* parameter. For geo fields: Can be defined as number+unit (1km, 12m,…). Default unit is meters. For date fields: |
35
|
|
|
* Can to be defined as a number+unit ("1h", "10d",…). Default unit is milliseconds. For numeric field: Any number. |
36
|
|
|
*/ |
37
|
|
|
private $scale; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var mixed If an offset is defined, the decay function will only compute the decay function for documents with a |
41
|
|
|
* distance greater that the defined offset. The default is 0. |
42
|
|
|
*/ |
43
|
|
|
private $offset; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var mixed The decay parameter defines how documents are scored at the distance given at scale. If no decay is |
47
|
|
|
* defined, documents at the distance scale will be scored 0.5. |
48
|
|
|
*/ |
49
|
|
|
private $decay; |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
|
|
public function toArray() |
56
|
|
|
{ |
57
|
|
|
$options = [ |
58
|
|
|
'origin' => $this->getOrigin(), |
59
|
|
|
'scale' => $this->getScale(), |
60
|
|
|
]; |
61
|
|
|
$offset = $this->getOffset(); |
62
|
|
|
if (!empty($offset)) { |
63
|
|
|
$options['offset'] = $offset; |
64
|
|
|
} |
65
|
|
|
$decay = $this->getDecay(); |
66
|
|
|
if (!empty($decay)) { |
67
|
|
|
$options['decay'] = $decay; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return [ |
71
|
|
|
$this->getDecayFunction() => [ |
72
|
|
|
$this->getField() => $options |
73
|
|
|
], |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getDecayFunction() |
82
|
|
|
{ |
83
|
|
|
return $this->decayFunction; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $decayFunction |
89
|
|
|
* @return DecayScoringFunction |
90
|
|
|
*/ |
91
|
|
|
public function setDecayFunction($decayFunction) |
92
|
|
|
{ |
93
|
|
|
$this->decayFunction = $decayFunction; |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
public function getOrigin() |
102
|
|
|
{ |
103
|
|
|
return $this->origin; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param mixed $origin |
109
|
|
|
* @return DecayScoringFunction |
110
|
|
|
*/ |
111
|
|
|
public function setOrigin($origin) |
112
|
|
|
{ |
113
|
|
|
$this->origin = $origin; |
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return mixed |
120
|
|
|
*/ |
121
|
|
|
public function getScale() |
122
|
|
|
{ |
123
|
|
|
return $this->scale; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param mixed $scale |
129
|
|
|
* @return DecayScoringFunction |
130
|
|
|
*/ |
131
|
|
|
public function setScale($scale) |
132
|
|
|
{ |
133
|
|
|
$this->scale = $scale; |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return mixed |
140
|
|
|
*/ |
141
|
|
|
public function getOffset() |
142
|
|
|
{ |
143
|
|
|
return $this->offset; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param mixed $offset |
149
|
|
|
* @return DecayScoringFunction |
150
|
|
|
*/ |
151
|
|
|
public function setOffset($offset) |
152
|
|
|
{ |
153
|
|
|
$this->offset = $offset; |
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return mixed |
160
|
|
|
*/ |
161
|
|
|
public function getDecay() |
162
|
|
|
{ |
163
|
|
|
return $this->decay; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param mixed $decay |
169
|
|
|
* @return DecayScoringFunction |
170
|
|
|
*/ |
171
|
|
|
public function setDecay($decay) |
172
|
|
|
{ |
173
|
|
|
$this->decay = $decay; |
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|