Newer
Older
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
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchBundle\DSL\Sort;
use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
/**
* Abstract class for sorting.
*/
abstract class AbstractSort implements BuilderInterface
{
/**
* @const ORDER_ASC Sort in ascending order.
*/
const ORDER_ASC = 'asc';
/**
* @const ORDER_DESC Sort in descending order.
*/
const ORDER_DESC = 'desc';
/**
* @const MODE_MIN Pick the lowest value in multi-valued field.
*/
const MODE_MIN = 'min';
/**
* @const MODE_MAX Pick the highest value in multi-valued field.
*/
const MODE_MAX = 'max';
/**
* @const MODE_AVG Use the sum of all values as sort value. Only applicable for number based array fields.
*/
const MODE_AVG = 'avg';
/**
* @const MODE_SUM Use the average of all values as sort value. Only applicable for number based array fields.
*/
const MODE_SUM = 'sum';
/**
* @var string
*/
private $order = self::ORDER_ASC;
/**
* @var string
*/
private $mode;
/**
* @var string
*/
private $field;
/**
* @param string $field Field name.
* @param string $order Order direction.
* @param string $mode Multi-valued field sorting mode [MODE_MIN, MODE_MAX, MODE_AVG, MODE_SUM].
*/
public function __construct($field, $order, $mode)
{
$this->setField($field);
$this->setOrder($order);
$this->setMode($mode);
}
/**
* Set multi-valued field sorting mode [MODE_MIN, MODE_MAX, MODE_AVG, MODE_SUM].
*
* @param string $mode
*/
public function setMode($mode)
{
$this->mode = $mode;
}
/**
* Returns mode.
*
* @return string
*/
public function getMode()
{
return $this->mode;
}
/**
* Set order direction.
*
* @param string|array $order
*/
public function setOrder($order)
{
$this->order = $order;
}
/**
* @return string
*/
public function getOrder()
{
return $this->order;
}
/**
* @param string $field
*/
public function setField($field)
{
$this->field = $field;
}
/**
* @return string
*/
public function getField()
{
return $this->field;
}
/**
* @return string
*/
abstract public function getType();
/**
* {@inheritdoc}
*/
public function toArray()
{
$value = [];
if ($this->getOrder()) {
$value['order'] = $this->getOrder();
}
if ($this->getMode() !== null) {
$value['mode'] = $this->getMode();
}
return $value;
}
}