Skip to content

Commit b2c3bf2

Browse files
committed
Added Options object
- created empty if not supplied - ability to set from and size for query - ability to specify min score for result - includes sorting
1 parent f00b656 commit b2c3bf2

File tree

2 files changed

+102
-20
lines changed

2 files changed

+102
-20
lines changed

src/ElasticQuery.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,17 @@ class ElasticQuery implements \Spameri\ElasticQuery\Entity\ArrayInterface
2727
private $aggregation;
2828

2929
/**
30-
* @var ?int
30+
* @var \Spameri\ElasticQuery\Options
3131
*/
32-
private $from;
33-
34-
/**
35-
* @var ?int
36-
*/
37-
private $size;
32+
private $options;
3833

3934

4035
public function __construct(
4136
?\Spameri\ElasticQuery\Query\QueryCollection $query = NULL
4237
, ?\Spameri\ElasticQuery\Filter\FilterCollection $filter = NULL
4338
, ?\Spameri\ElasticQuery\Options\SortCollection $sort = NULL
4439
, ?\Spameri\ElasticQuery\Aggregation\AggregationCollection $aggregation = NULL
45-
, ?int $from = NULL
46-
, ?int $size = NULL
40+
, ?Options $options = NULL
4741
)
4842
{
4943
if ( ! $query) {
@@ -58,12 +52,14 @@ public function __construct(
5852
if ( ! $aggregation) {
5953
$aggregation = new \Spameri\ElasticQuery\Aggregation\AggregationCollection();
6054
}
55+
if ( ! $options) {
56+
$options = new Options();
57+
}
6158
$this->query = $query;
6259
$this->filter = $filter;
6360
$this->sort = $sort;
6461
$this->aggregation = $aggregation;
65-
$this->from = $from;
66-
$this->size = $size;
62+
$this->options = $options;
6763
}
6864

6965

@@ -79,9 +75,21 @@ public function filter() : \Spameri\ElasticQuery\Filter\FilterCollection
7975
}
8076

8177

78+
public function aggregation() : \Spameri\ElasticQuery\Aggregation\AggregationCollection
79+
{
80+
return $this->aggregation;
81+
}
82+
83+
84+
public function options() : \Spameri\ElasticQuery\Options
85+
{
86+
return $this->options;
87+
}
88+
89+
8290
public function toArray() : array
8391
{
84-
$array = [];
92+
$array = $this->options->toArray();
8593

8694
$queryArray = $this->query->toArray();
8795
if ($queryArray) {
@@ -103,14 +111,6 @@ public function toArray() : array
103111
$array['aggs'] = $aggregation;
104112
}
105113

106-
if ($this->size !== NULL) {
107-
$array['size'] = $this->size;
108-
}
109-
110-
if ($this->from !== NULL) {
111-
$array['from'] = $this->from;
112-
}
113-
114114
return $array;
115115
}
116116

src/Options.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery;
4+
5+
6+
class Options
7+
{
8+
9+
/**
10+
* @var int
11+
*/
12+
private $size;
13+
/**
14+
* @var int
15+
*/
16+
private $from;
17+
/**
18+
* @var \Spameri\ElasticQuery\Options\SortCollection
19+
*/
20+
private $sort;
21+
/**
22+
* @var int
23+
*/
24+
private $minScore;
25+
26+
27+
public function __construct(
28+
?int $size = NULL,
29+
?int $from = NULL,
30+
?\Spameri\ElasticQuery\Options\SortCollection $sort = NULL,
31+
float $minScore = NULL
32+
)
33+
{
34+
$this->size = $size;
35+
$this->from = $from;
36+
$this->sort = $sort ?: new \Spameri\ElasticQuery\Options\SortCollection();
37+
$this->minScore = $minScore;
38+
}
39+
40+
41+
public function changeFrom(int $from) : void
42+
{
43+
$this->from = $from;
44+
}
45+
46+
47+
public function changeSize(int $size) : void
48+
{
49+
$this->size = $size;
50+
}
51+
52+
53+
public function toArray() : array
54+
{
55+
$array = [];
56+
57+
if ($this->from !== NULL) {
58+
$array['from'] = $this->from;
59+
}
60+
61+
if ($this->size !== NULL) {
62+
$array['size'] = $this->size;
63+
}
64+
65+
foreach ($this->sort as $item) {
66+
$array['sort'][] = $item->toArray();
67+
}
68+
69+
if ($this->minScore) {
70+
$array['min_score'] = $this->minScore;
71+
}
72+
73+
return $array;
74+
}
75+
76+
77+
public function sort() : \Spameri\ElasticQuery\Options\SortCollection
78+
{
79+
return $this->sort;
80+
}
81+
82+
}

0 commit comments

Comments
 (0)