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
|
|
|
*/ |
30
|
|
|
public function __construct(?string $field = null) |
31
|
|
|
{ |
32
|
|
|
if ($field !== null) { |
33
|
|
|
$this->setField($field); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
|
|
public function toArray() |
41
|
|
|
{ |
42
|
|
|
$options = $this->applyOptions([]); |
43
|
|
|
|
44
|
|
|
$missing = $this->getMissing(); |
45
|
|
|
if (null !== $missing) { |
46
|
|
|
$options['missing'] = $missing; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$unmappedType = $this->getUnmappedType(); |
50
|
|
|
if (null !== $unmappedType) { |
51
|
|
|
$options['unmapped_type'] = $unmappedType; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (empty($options)) { |
55
|
|
|
return $this->getField(); |
|
|
|
|
56
|
|
|
} else { |
57
|
|
|
return [$this->getField() => $options]; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $missing |
64
|
|
|
* @return FieldSort |
65
|
|
|
*/ |
66
|
|
|
public function setMissing($missing) |
67
|
|
|
{ |
68
|
|
|
$this->missing = $missing; |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string|null |
75
|
|
|
*/ |
76
|
|
|
public function getMissing(): ?string |
77
|
|
|
{ |
78
|
|
|
return $this->missing; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $unmappedType |
84
|
|
|
* @return FieldSort |
85
|
|
|
*/ |
86
|
|
|
public function setUnmappedType($unmappedType) |
87
|
|
|
{ |
88
|
|
|
$this->unmappedType = $unmappedType; |
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string|null |
95
|
|
|
*/ |
96
|
|
|
public function getUnmappedType(): ?string |
97
|
|
|
{ |
98
|
|
|
return $this->unmappedType; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: