DecayScoringFunction   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 37
c 1
b 0
f 0
dl 0
loc 163
rs 10

11 Methods

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