Skip to content

Commit

Permalink
1.10.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
Olek Kaim committed Aug 26, 2023
1 parent 9b84c7d commit f5e8172
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/Builder/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Olekjs\Elasticsearch\Builder;

use Closure;
use Illuminate\Support\Traits\Conditionable;
use LogicException;
use Olekjs\Elasticsearch\Client;
Expand Down Expand Up @@ -55,7 +56,9 @@ class Builder implements BuilderInterface

private array $body = [];

public function __construct(private readonly ClientInterface $client)
private array $nested = [];

public function __construct(private readonly ClientInterface $client = new Client())
{
}

Expand Down Expand Up @@ -174,6 +177,21 @@ public function whereRange(string $field, int|float $value, string $operator): s
return $this;
}

public function whereNested(string $path, Closure $closure): self
{
/** @var BuilderInterface $builder */
$builder = $closure(new self());

$this->nested[] = [
'nested' => [
'path' => $path,
'query' => $builder->getQuery(),
]
];

return $this;
}

/**
* @throws LogicException
*/
Expand Down Expand Up @@ -423,10 +441,25 @@ public function getSelect(): array
return $this->select;
}

public function getNested(): array
{
return $this->nested;
}

public function performSearchBody(): void
{
if (isset($this->nested)) {
foreach ($this->nested as $nested) {
$this->body['query']['bool']['must'][] = $nested;
}
}

if (isset($this->query)) {
$this->body['query'] = $this->query;
if (isset($this->nested)) {
$this->body['query']['bool']['must'][] = $this->query;
} else {
$this->body['query'] = $this->query;
}
}

if (isset($this->select)) {
Expand Down
5 changes: 5 additions & 0 deletions src/Contracts/BuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Olekjs\Elasticsearch\Contracts;

use Closure;
use LogicException;
use Olekjs\Elasticsearch\Builder\Builder;
use Olekjs\Elasticsearch\Dto\BulkResponseDto;
Expand Down Expand Up @@ -55,6 +56,8 @@ public function whereBetween(string $field, array $values): self;

public function whereRange(string $field, int|float $value, string $operator): self;

public function whereNested(string $path, Closure $closure): self;

/**
* @throws LogicException
*/
Expand Down Expand Up @@ -142,5 +145,7 @@ public function getSort(): array;

public function getSelect(): array;

public function getNested(): array;

public function performSearchBody(): void;
}
133 changes: 133 additions & 0 deletions tests/Unit/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Olekjs\Elasticsearch\Builder\Builder;
use Olekjs\Elasticsearch\Bulk\Bulk;
use Olekjs\Elasticsearch\Client;
use Olekjs\Elasticsearch\Contracts\BuilderInterface;
use Olekjs\Elasticsearch\Dto\BulkResponseDto;
use Olekjs\Elasticsearch\Dto\FindResponseDto;
use Olekjs\Elasticsearch\Dto\IndexResponseDto;
Expand Down Expand Up @@ -605,4 +606,136 @@ public function testWithAggregationMethod(): void
$builder->getBody()
);
}

/**
* @dataProvider whereNestedDataProvider
*/
public function testWhereNestedMethod(\Closure $builder, array $actual): void
{
$expected = $builder();

$this->assertSame($expected, $actual);
}

public static function whereNestedDataProvider(): iterable
{
yield [
function () {
$builder = Builder::query();

$builder->whereNested('nested_path', function (BuilderInterface $builder) {
return $builder->whereIn('test', [1, 2, 3]);
});

$builder->performSearchBody();

return $builder->getBody();
},
[
'query' => [
'bool' => [
'must' => [
[
'nested' => [
'path' => 'nested_path',
'query' => [
'bool' => ['filter' => [['terms' => ['test' => [1, 2, 3]]]]]
]
]
]
]
]
]
],
];
yield [
function () {
$builder = Builder::query();

$builder->whereNested('nested_path', function (BuilderInterface $builder) {
return $builder
->where('test', 1)
->where('test_2', 2);
});

$builder->performSearchBody();

return $builder->getBody();
},
[
'query' => [
'bool' => [
'must' => [
[
'nested' => [
'path' => 'nested_path',
'query' => [
'bool' => [
'filter' => [
['term' => ['test' => 1]],
['term' => ['test_2' => 2]],
]
]
]
]
]
]
]
]
],
];
yield [
function () {
$builder = Builder::query();

$builder->where('test_1', 1);

$builder->whereNested('nested_path', function (BuilderInterface $builder) {
return $builder->where('test_2', 2);
});

$builder->where('test_3', 3);

$builder->performSearchBody();

return $builder->getBody();
},
[
'query' => [
'bool' => [
'must' => [
[
'nested' => [
'path' => 'nested_path',
'query' => [
'bool' => [
'filter' => [
['term' => ['test_2' => 2]],
]
]
]
]
],
[
'bool' => [
'filter' => [
[
'term' => [
'test_1' => 1,
]
],
[
'term' => [
'test_3' => 3,
]
]
]
]
]
]
]
]
],
];
}
}

0 comments on commit f5e8172

Please sign in to comment.