Skip to content

Commit abb0caa

Browse files
authored
Merge pull request #2 from greensight/dev-1
New filters, sort order and mode enums.
2 parents ce7c9f1 + a418fc9 commit abb0caa

File tree

17 files changed

+239
-30
lines changed

17 files changed

+239
-30
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ $searchQuery->whereNot('field', 'value'); // equals `where('field', '!=', 'value
4949

5050
List of supported operators: `=, !=, >, <, >=, <=`.
5151

52+
```php
53+
$searchQuery->whereIn('field', ['value1', 'value2']);
54+
$searchQuery->whereNotIn('field', ['value1', 'value2']);
55+
```
56+
57+
```php
58+
$searchQuery->whereNull('field');
59+
$searchQuery->whereNotNull('field');
60+
```
61+
5262
```php
5363
$searchQuery->whereHas('nested_field', fn(BoolQuery $subQuery) => $subQuery->where('field_in_nested', 'value'));
5464
$searchQuery->whereDoesntHave(

src/Raw/Aggregating/Bucket/TermsAggregation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Greensight\LaravelElasticQuery\Raw\Aggregating\Bucket;
44

5-
use Greensight\LaravelElasticQuery\Raw\Aggregating\Bucket;
65
use Greensight\LaravelElasticQuery\Raw\Aggregating\BucketCollection;
6+
use Greensight\LaravelElasticQuery\Raw\Aggregating\Result;
77
use Greensight\LaravelElasticQuery\Raw\Contracts\Aggregation;
88
use Webmozart\Assert\Assert;
99

@@ -34,7 +34,7 @@ public function toDSL(): array
3434
public function parseResults(array $response): array
3535
{
3636
$buckets = array_map(
37-
fn (array $bucket) => new Bucket($bucket['key'], (int)$bucket['doc_count']),
37+
fn (array $bucket) => Result::parseBucket($bucket),
3838
$response[$this->name]['buckets'] ?? []
3939
);
4040

src/Raw/Aggregating/Metrics/MinMaxAggregation.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Greensight\LaravelElasticQuery\Raw\Aggregating\Metrics;
44

55
use Greensight\LaravelElasticQuery\Raw\Aggregating\MinMax;
6+
use Greensight\LaravelElasticQuery\Raw\Aggregating\Result;
67
use Greensight\LaravelElasticQuery\Raw\Contracts\Aggregation;
78
use Webmozart\Assert\Assert;
89

@@ -30,8 +31,8 @@ public function toDSL(): array
3031
public function parseResults(array $response): array
3132
{
3233
return [$this->name => new MinMax(
33-
$response["{$this->name}_min"]['value'] ?? 0,
34-
$response["{$this->name}_max"]['value'] ?? 0,
34+
Result::parseValue($response["{$this->name}_min"]) ?? 0,
35+
Result::parseValue($response["{$this->name}_max"]) ?? 0,
3536
)];
3637
}
3738
}

src/Raw/Aggregating/Result.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Greensight\LaravelElasticQuery\Raw\Aggregating;
4+
5+
class Result
6+
{
7+
public static function parseValue(array $source): mixed
8+
{
9+
return self::parse($source, 'value');
10+
}
11+
12+
public static function parseBucket(array $source): Bucket
13+
{
14+
return new Bucket(self::parse($source, 'key'), (int)($source['doc_count'] ?? 0));
15+
}
16+
17+
public static function parse(array $source, string $key): mixed
18+
{
19+
$stringValue = $source["{$key}_as_string"] ?? null;
20+
21+
if ($stringValue === null) {
22+
return $source[$key] ?? null;
23+
}
24+
25+
if ($stringValue === 'true') {
26+
return true;
27+
}
28+
29+
if ($stringValue === 'false') {
30+
return false;
31+
}
32+
33+
return $stringValue;
34+
}
35+
}

src/Raw/Concerns/DecoratesBoolQuery.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Greensight\LaravelElasticQuery\Raw\Filtering\BoolQueryBuilder;
7+
use Illuminate\Contracts\Support\Arrayable;
78
use Illuminate\Support\Traits\ForwardsCalls;
89

910
trait DecoratesBoolQuery
@@ -39,4 +40,32 @@ public function whereDoesntHave(string $nested, Closure $filter): static
3940

4041
return $this;
4142
}
43+
44+
public function whereIn(string $field, array|Arrayable $values): static
45+
{
46+
$this->forwardCallTo($this->boolQuery(), __FUNCTION__, func_get_args());
47+
48+
return $this;
49+
}
50+
51+
public function whereNotIn(string $field, array|Arrayable $values): static
52+
{
53+
$this->forwardCallTo($this->boolQuery(), __FUNCTION__, func_get_args());
54+
55+
return $this;
56+
}
57+
58+
public function whereNull(string $field): static
59+
{
60+
$this->forwardCallTo($this->boolQuery(), __FUNCTION__, func_get_args());
61+
62+
return $this;
63+
}
64+
65+
public function whereNotNull(string $field): static
66+
{
67+
$this->forwardCallTo($this->boolQuery(), __FUNCTION__, func_get_args());
68+
69+
return $this;
70+
}
4271
}

src/Raw/Concerns/ExtendsSort.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,38 @@
22

33
namespace Greensight\LaravelElasticQuery\Raw\Concerns;
44

5+
use Greensight\LaravelElasticQuery\Raw\Contracts\SortMode;
6+
use Greensight\LaravelElasticQuery\Raw\Contracts\SortOrder;
7+
58
/**
69
* @psalm-require-implements \Greensight\LaravelElasticQuery\Raw\Contracts\SortableQuery
710
*
8-
* @method static sortBy(string $field, string $order = 'asc', ?string $mode = null)
11+
* @method static sortBy(string $field, string $order = SortOrder::ASC, ?string $mode = null)
912
*/
1013
trait ExtendsSort
1114
{
12-
public function minSortBy(string $field, string $order = 'asc'): static
15+
public function minSortBy(string $field, string $order = SortOrder::ASC): static
1316
{
14-
return $this->sortBy($field, $order, 'min');
17+
return $this->sortBy($field, $order, SortMode::MIN);
1518
}
1619

17-
public function maxSortBy(string $field, string $order = 'asc'): static
20+
public function maxSortBy(string $field, string $order = SortOrder::ASC): static
1821
{
19-
return $this->sortBy($field, $order, 'max');
22+
return $this->sortBy($field, $order, SortMode::MAX);
2023
}
2124

22-
public function avgSortBy(string $field, string $order = 'asc'): static
25+
public function avgSortBy(string $field, string $order = SortOrder::ASC): static
2326
{
24-
return $this->sortBy($field, $order, 'avg');
27+
return $this->sortBy($field, $order, SortMode::AVG);
2528
}
2629

27-
public function sumSortBy(string $field, string $order = 'asc'): static
30+
public function sumSortBy(string $field, string $order = SortOrder::ASC): static
2831
{
29-
return $this->sortBy($field, $order, 'sum');
32+
return $this->sortBy($field, $order, SortMode::SUM);
3033
}
3134

32-
public function medianSortBy(string $field, string $order = 'asc'): static
35+
public function medianSortBy(string $field, string $order = SortOrder::ASC): static
3336
{
34-
return $this->sortBy($field, $order, 'median');
37+
return $this->sortBy($field, $order, SortMode::MEDIAN);
3538
}
3639
}

src/Raw/Contracts/BoolQuery.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@
33
namespace Greensight\LaravelElasticQuery\Raw\Contracts;
44

55
use Closure;
6+
use Illuminate\Contracts\Support\Arrayable;
67

78
interface BoolQuery
89
{
910
public function where(string $field, mixed $operator, mixed $value = null): static;
1011

1112
public function whereNot(string $field, mixed $value): static;
1213

14+
public function whereIn(string $field, array|Arrayable $values): static;
15+
16+
public function whereNotIn(string $field, array|Arrayable $values): static;
17+
1318
public function whereHas(string $nested, Closure $filter): static;
1419

1520
public function whereDoesntHave(string $nested, Closure $filter): static;
21+
22+
public function whereNull(string $field): static;
23+
24+
public function whereNotNull(string $field): static;
1625
}

src/Raw/Contracts/SortMode.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Greensight\LaravelElasticQuery\Raw\Contracts;
4+
5+
final class SortMode
6+
{
7+
public const MIN = 'min';
8+
public const MAX = 'max';
9+
public const AVG = 'avg';
10+
public const SUM = 'sum';
11+
public const MEDIAN = 'median';
12+
13+
public static function cases(): array
14+
{
15+
return [
16+
self::MAX,
17+
self::MIN,
18+
self::AVG,
19+
self::SUM,
20+
self::MEDIAN,
21+
];
22+
}
23+
}

src/Raw/Contracts/SortOrder.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Greensight\LaravelElasticQuery\Raw\Contracts;
4+
5+
final class SortOrder
6+
{
7+
public const ASC = 'asc';
8+
public const DESC = 'desc';
9+
10+
public static function cases(): array
11+
{
12+
return [
13+
self::ASC,
14+
self::DESC,
15+
];
16+
}
17+
}

src/Raw/Contracts/SortableQuery.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66

77
interface SortableQuery extends BoolQuery
88
{
9-
public function sortBy(string $field, string $order = 'asc', ?string $mode = null): static;
9+
public function sortBy(string $field, string $order = SortOrder::ASC, ?string $mode = null): static;
1010

11-
public function minSortBy(string $field, string $order = 'asc'): static;
11+
public function minSortBy(string $field, string $order = SortOrder::ASC): static;
1212

13-
public function maxSortBy(string $field, string $order = 'asc'): static;
13+
public function maxSortBy(string $field, string $order = SortOrder::ASC): static;
1414

15-
public function avgSortBy(string $field, string $order = 'asc'): static;
15+
public function avgSortBy(string $field, string $order = SortOrder::ASC): static;
1616

17-
public function sumSortBy(string $field, string $order = 'asc'): static;
17+
public function sumSortBy(string $field, string $order = SortOrder::ASC): static;
1818

19-
public function medianSortBy(string $field, string $order = 'asc'): static;
19+
public function medianSortBy(string $field, string $order = SortOrder::ASC): static;
2020

2121
public function sortByNested(string $field, Closure $callback): static;
2222
}

0 commit comments

Comments
 (0)