Skip to content

Commit 80a64ae

Browse files
committed
Merge branch 'master' of github.com:Spameri/ElasticQuery into v2
2 parents c75fbce + 5bcdc83 commit 80a64ae

File tree

7 files changed

+55
-7
lines changed

7 files changed

+55
-7
lines changed

src/Document.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ public function toArray(): array
3030
$array['body'] = $this->body->toArray();
3131
}
3232

33-
if ($this->id) {
33+
if (
34+
\is_string($this->id)
35+
&& $this->id !== ''
36+
) {
3437
$array['id'] = $this->id;
3538
}
3639

src/ElasticQuery.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class ElasticQuery implements \Spameri\ElasticQuery\Entity\ArrayInterface
1212

1313
private \Spameri\ElasticQuery\Filter\FilterCollection $filter;
1414

15+
private \Spameri\ElasticQuery\Query\QueryCollection $postFilter;
16+
1517
private \Spameri\ElasticQuery\Options\SortCollection $sort;
1618

1719
private \Spameri\ElasticQuery\Aggregation\AggregationCollection $aggregation;
@@ -21,6 +23,7 @@ class ElasticQuery implements \Spameri\ElasticQuery\Entity\ArrayInterface
2123
public function __construct(
2224
\Spameri\ElasticQuery\Query\QueryCollection|null $query = null,
2325
\Spameri\ElasticQuery\Filter\FilterCollection|null $filter = null,
26+
\Spameri\ElasticQuery\Query\QueryCollection|null $postFilter = null,
2427
\Spameri\ElasticQuery\Options\SortCollection|null $sort = null,
2528
\Spameri\ElasticQuery\Aggregation\AggregationCollection|null $aggregation = null,
2629
private \Spameri\ElasticQuery\Highlight|null $highlight = null,
@@ -34,6 +37,9 @@ public function __construct(
3437
if ($filter === null) {
3538
$filter = new \Spameri\ElasticQuery\Filter\FilterCollection();
3639
}
40+
if ($postFilter === null) {
41+
$postFilter = new \Spameri\ElasticQuery\Query\QueryCollection();
42+
}
3743
if ($sort === null) {
3844
$sort = new \Spameri\ElasticQuery\Options\SortCollection();
3945
}
@@ -46,6 +52,7 @@ public function __construct(
4652

4753
$this->query = $query;
4854
$this->filter = $filter;
55+
$this->postFilter = $postFilter;
4956
$this->sort = $sort;
5057
$this->aggregation = $aggregation;
5158
$this->options = $options;
@@ -106,6 +113,18 @@ public function addShouldQuery(\Spameri\ElasticQuery\Query\LeafQueryInterface $l
106113
}
107114

108115

116+
public function postFilter(): \Spameri\ElasticQuery\Query\QueryCollection
117+
{
118+
return $this->postFilter;
119+
}
120+
121+
122+
public function addPostFilterMustQuery(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery): void
123+
{
124+
$this->postFilter->must()->add($leafQuery);
125+
}
126+
127+
109128
public function addFilter(\Spameri\ElasticQuery\Query\LeafQueryInterface $leafQuery): void
110129
{
111130
$this->filter->must()->add($leafQuery);
@@ -136,6 +155,11 @@ public function toArray(): array
136155
$array['query']['bool']['filter'] = $filterArray;
137156
}
138157

158+
$postFilterArray = $this->postFilter->toArray();
159+
if ($postFilterArray) {
160+
$array['post_filter'] = $postFilterArray;
161+
}
162+
139163
$sortArray = $this->sort->toArray();
140164
if ($sortArray) {
141165
$array['sort'] = $sortArray;

src/Options.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ public function toArray(): array
8484
}
8585

8686
foreach ($this->sort as $item) {
87+
if ($item->field === '_score') {
88+
$array['sort'][] = $item->field;
89+
continue;
90+
}
91+
8792
$array['sort'][] = $item->toArray();
8893
}
8994

src/Options/Sort.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ class Sort implements \Spameri\ElasticQuery\Entity\EntityInterface
1717
public const MISSING_FIRST = '_first';
1818

1919
public function __construct(
20-
private string $field,
21-
private string $type = self::DESC,
22-
private string $missing = self::MISSING_LAST,
20+
public string $field,
21+
public string $type = self::DESC,
22+
public string $missing = self::MISSING_LAST,
2323
)
2424
{
2525
if ( ! \in_array($type, [self::ASC, self::DESC], true)) {

src/Options/SortCollection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ public function toArray(): array
1212
{
1313
$array = [];
1414

15+
/** @var \Spameri\ElasticQuery\Options\Sort $sort */
1516
foreach ($this->collection as $sort) {
17+
if ($sort->field === '_score') {
18+
$array[] = $sort->field;
19+
continue;
20+
}
21+
1622
$array[] = $sort->toArray();
1723
}
1824

src/Query/Nested.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,19 @@ public function key(): string
3232

3333
public function toArray(): array
3434
{
35+
$queryArray = $this->query->toArray();
36+
37+
if (count($queryArray) === 0) {
38+
$queryArray = [
39+
'bool' => [],
40+
];
41+
}
42+
3543
return [
3644
'nested' => [
3745
'path' => $this->path,
3846
'query' => [
39-
'bool' => $this->query->toArray(),
47+
$queryArray,
4048
],
4149
],
4250
];

src/Response/ResultMapper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,11 @@ private function mapBucket(
261261
array $bucketArray,
262262
): \Spameri\ElasticQuery\Response\Result\Aggregation\Bucket
263263
{
264+
$bucketKey = $bucketArray['key'] ?? $bucketPosition;
265+
264266
return new \Spameri\ElasticQuery\Response\Result\Aggregation\Bucket(
265-
$bucketArray['key'] ?? (string) $bucketPosition,
266-
$bucketArray['doc_count'],
267+
(string) $bucketKey,
268+
(int) $bucketArray['doc_count'],
267269
\is_int($bucketPosition) ? $bucketPosition : null,
268270
$bucketArray['from'] ?? null,
269271
$bucketArray['to'] ?? null,

0 commit comments

Comments
 (0)