|
1
|
|
|
<?php namespace Nord\Lumen\Elasticsearch\Search\Sort; |
|
2
|
|
|
|
|
3
|
|
|
use Nord\Lumen\Elasticsearch\Search\Traits\HasField; |
|
4
|
|
|
|
|
5
|
|
|
class FieldSort extends AbstractSort |
|
6
|
|
|
{ |
|
7
|
|
|
use HasField; |
|
8
|
|
|
|
|
9
|
|
|
public const MISSING_FIRST = '_first'; |
|
10
|
|
|
public const MISSING_LAST = '_last'; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var ?string The missing parameter specifies how docs which are missing the field should be treated. The missing |
|
14
|
|
|
* value can be set to _last, _first, or a custom value (that will be used for missing docs as the sort value). |
|
15
|
|
|
*/ |
|
16
|
|
|
private $missing; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var ?string By default, the search request will fail if there is no mapping associated with a field. The |
|
20
|
|
|
* unmapped_type option allows to ignore fields that have no mapping and not sort by them. The value of this |
|
21
|
|
|
* parameter is used to determine what sort values to emit. |
|
22
|
|
|
*/ |
|
23
|
|
|
private $unmappedType; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* FieldSort constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param null|string $field |
|
29
|
|
|
* @param null|string $order |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(?string $field = null, ?string $order = null) |
|
32
|
|
|
{ |
|
33
|
|
|
if ($field !== null) { |
|
34
|
|
|
$this->setField($field); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if ($order !== null) { |
|
38
|
|
|
$this->setOrder($order); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public function toArray() |
|
46
|
|
|
{ |
|
47
|
|
|
$options = $this->applyOptions([]); |
|
48
|
|
|
|
|
49
|
|
|
$missing = $this->getMissing(); |
|
50
|
|
|
if (null !== $missing) { |
|
51
|
|
|
$options['missing'] = $missing; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$unmappedType = $this->getUnmappedType(); |
|
55
|
|
|
if (null !== $unmappedType) { |
|
56
|
|
|
$options['unmapped_type'] = $unmappedType; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (empty($options)) { |
|
60
|
|
|
return $this->getField(); |
|
|
|
|
|
|
61
|
|
|
} else { |
|
62
|
|
|
return [$this->getField() => $options]; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $missing |
|
69
|
|
|
* @return FieldSort |
|
70
|
|
|
*/ |
|
71
|
|
|
public function setMissing($missing) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->missing = $missing; |
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return string|null |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getMissing(): ?string |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->missing; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $unmappedType |
|
89
|
|
|
* @return FieldSort |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setUnmappedType($unmappedType) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->unmappedType = $unmappedType; |
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return string|null |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getUnmappedType(): ?string |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->unmappedType; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: