Skip to content

Commit 24e123a

Browse files
committed
Added ability to sort terms aggregation
1 parent 8d313c9 commit 24e123a

File tree

3 files changed

+166
-23
lines changed

3 files changed

+166
-23
lines changed

src/Aggregation/Term.php

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,38 @@
99
class Term implements LeafAggregationInterface
1010
{
1111

12-
/**
13-
* @var string
14-
*/
15-
private $field;
16-
17-
/**
18-
* @var int
19-
*/
20-
private $size;
21-
22-
/**
23-
* @var ?int
24-
*/
25-
private $missing;
26-
27-
/**
28-
* @var ?string
29-
*/
30-
private $key;
12+
private string $field;
13+
14+
private int $size;
15+
16+
private ?int $missing;
17+
18+
private ?string $key;
19+
20+
private \Spameri\ElasticQuery\Aggregation\Terms\OrderCollection $order;
21+
22+
private ?string $include;
23+
24+
private ?string $exclude;
3125

3226

3327
public function __construct(
34-
string $field
35-
, int $size = 5
36-
, int $missing = NULL
37-
, string $key = NULL
28+
string $field,
29+
int $size = 0,
30+
int $missing = NULL,
31+
?\Spameri\ElasticQuery\Aggregation\Terms\OrderCollection $order = NULL,
32+
?string $include = NULL,
33+
?string $exclude = NULL,
34+
?string $key = NULL
3835
)
3936
{
4037
$this->field = $field;
4138
$this->size = $size;
4239
$this->missing = $missing;
4340
$this->key = $key;
41+
$this->order = $order ?? new \Spameri\ElasticQuery\Aggregation\Terms\OrderCollection();
42+
$this->include = $include;
43+
$this->exclude = $exclude;
4444
}
4545

4646

@@ -61,6 +61,18 @@ public function toArray(): array
6161
$array['missing'] = $this->missing;
6262
}
6363

64+
if (\count($this->order)) {
65+
$array['order'] = $this->order->toArray();
66+
}
67+
68+
if ($this->include !== NULL) {
69+
$array['include'] = $this->include;
70+
}
71+
72+
if ($this->exclude !== NULL) {
73+
$array['exclude'] = $this->exclude;
74+
}
75+
6476
return [
6577
'terms' => $array,
6678
];

src/Aggregation/Terms/Order.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Aggregation\Terms;
4+
5+
class Order implements \Spameri\ElasticQuery\Collection\Item
6+
{
7+
8+
private string $field;
9+
10+
private string $type;
11+
12+
13+
public function __construct(
14+
string $field,
15+
string $type
16+
) {
17+
$this->field = $field;
18+
$this->type = $type;
19+
}
20+
21+
22+
public function key(): string
23+
{
24+
return 'order_' . $this->field . '_' . $this->type;
25+
}
26+
27+
28+
public function toArray(): array
29+
{
30+
return [
31+
$this->field => $this->type,
32+
];
33+
}
34+
35+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Aggregation\Terms;
4+
5+
class OrderCollection implements
6+
\Spameri\ElasticQuery\Collection\SimpleCollectionInterface,
7+
\Countable
8+
{
9+
10+
/**
11+
* @var array<\Spameri\ElasticQuery\Aggregation\Terms\Order>
12+
*/
13+
private array $collection;
14+
15+
16+
public function __construct(
17+
\Spameri\ElasticQuery\Aggregation\Terms\Order ... $collection
18+
) {
19+
foreach ($collection as $order) {
20+
$this->add($order);
21+
}
22+
}
23+
24+
25+
public function remove(string $key): bool
26+
{
27+
if (isset($this->collection[$key])) {
28+
unset($this->collection[$key]);
29+
30+
return TRUE;
31+
}
32+
33+
return FALSE;
34+
}
35+
36+
37+
public function get(string $key): ?\Spameri\ElasticQuery\Aggregation\Terms\Order
38+
{
39+
return $this->collection[$key] ?? NULL;
40+
}
41+
42+
43+
public function isValue(string $key): bool
44+
{
45+
return isset($this->collection[$key]);
46+
}
47+
48+
49+
public function keys(): array
50+
{
51+
return \array_keys($this->collection);
52+
}
53+
54+
55+
public function clear(): void
56+
{
57+
$this->collection = [];
58+
}
59+
60+
61+
/**
62+
* @param \Spameri\ElasticQuery\Aggregation\Terms\Order $item
63+
*/
64+
public function add($item): void
65+
{
66+
$this->collection[$item->key()] = $item;
67+
}
68+
69+
70+
public function getIterator(): \ArrayIterator
71+
{
72+
return new \ArrayIterator($this->collection);
73+
}
74+
75+
76+
public function count(): int
77+
{
78+
return \count($this->collection);
79+
}
80+
81+
82+
public function toArray(): array
83+
{
84+
if (\count($this->collection) === 1) {
85+
return \reset($this->collection)->toArray();
86+
}
87+
88+
$array = [];
89+
foreach ($this->collection as $order) {
90+
$array[] = $order->toArray();
91+
}
92+
93+
return $array;
94+
}
95+
96+
}

0 commit comments

Comments
 (0)