generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from chrispappas/multi-builder
Add MultiBuilder to support multi-search API
- Loading branch information
Showing
5 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Spatie\ElasticsearchQueryBuilder; | ||
|
||
use Elastic\Elasticsearch\Client; | ||
use Elastic\Elasticsearch\Response\Elasticsearch; | ||
use Http\Promise\Promise; | ||
|
||
class MultiBuilder | ||
{ | ||
protected ?array $builders = []; | ||
|
||
public function __construct(protected Client $client) | ||
{ | ||
} | ||
|
||
public function addBuilder(Builder $builder, ?string $indexName = null): static | ||
{ | ||
$this->builders[] = [ | ||
'index' => $indexName ?? $builder->getIndex(), | ||
'builder' => $builder, | ||
]; | ||
|
||
return $this; | ||
} | ||
|
||
public function getPayload(): array | ||
{ | ||
$payload = []; | ||
|
||
foreach ($this->builders as $builderInstance) { | ||
['index' => $index, 'builder' => $builder] = $builderInstance; | ||
$payload[] = $index ? ['index' => $index] : []; | ||
$payload[] = $builder->getPayload(); | ||
} | ||
|
||
return $payload; | ||
} | ||
|
||
public function search(): Elasticsearch|Promise | ||
{ | ||
$payload = $this->getPayload(); | ||
|
||
$params = [ | ||
'body' => $payload, | ||
]; | ||
|
||
return $this->client->msearch($params); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace Spatie\ElasticsearchQueryBuilder\Tests\Builders; | ||
|
||
use Elastic\Elasticsearch\Client; | ||
use Elastic\Transport\TransportBuilder; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\LoggerInterface; | ||
use Spatie\ElasticsearchQueryBuilder\Builder; | ||
use Spatie\ElasticsearchQueryBuilder\MultiBuilder; | ||
use Spatie\ElasticsearchQueryBuilder\Queries\TermQuery; | ||
|
||
class MultiBuilderTest extends TestCase | ||
{ | ||
private MultiBuilder $multiBuilder; | ||
|
||
private Client $client; | ||
|
||
protected function setUp(): void | ||
{ | ||
$transport = TransportBuilder::create() | ||
->setClient(new \Http\Mock\Client()) | ||
->build(); | ||
|
||
$logger = $this->createStub(LoggerInterface::class); | ||
|
||
$this->client = new Client($transport, $logger); | ||
|
||
$this->multiBuilder = new MultiBuilder($this->client); | ||
} | ||
|
||
public function testEmptyPayloadGeneratesCorrectly(): void | ||
{ | ||
$this->assertEmpty($this->multiBuilder->getPayload()); | ||
} | ||
|
||
public function testSingleBuilderPayloadGeneratesCorrectly(): void | ||
{ | ||
$this->multiBuilder->addBuilder( | ||
(new Builder($this->client))->addQuery(TermQuery::create('test', 'value')) | ||
); | ||
|
||
$payload = $this->multiBuilder->getPayload(); | ||
|
||
$this->assertNotEmpty($payload); | ||
|
||
$this->assertCount(2, $payload); | ||
|
||
$this->assertEquals([], $payload[0]); | ||
|
||
$this->assertEquals([ | ||
'query' => [ | ||
'bool' => [ | ||
'must' => [ | ||
['term' => ['test' => 'value']], | ||
], | ||
], | ||
], | ||
], $payload[1]); | ||
} | ||
|
||
public function testMultipleBuilderPayloadGeneratesCorrectly(): void | ||
{ | ||
$this->multiBuilder->addBuilder( | ||
(new Builder($this->client)) | ||
->index('firstIndex') | ||
->addQuery(TermQuery::create('keyword', 'value'), 'filter'), | ||
); | ||
$this->multiBuilder->addBuilder( | ||
(new Builder($this->client)) | ||
->addQuery(TermQuery::create('keyword', 'value'), 'filter'), | ||
'secondIndex' | ||
); | ||
|
||
$payload = $this->multiBuilder->getPayload(); | ||
|
||
$this->assertNotEmpty($payload); | ||
$this->assertCount(4, $payload); | ||
|
||
$index = $payload[0]; | ||
$this->assertEquals(['index' => 'firstIndex'], $index); | ||
|
||
$body = $payload[1]; | ||
$this->assertEquals([ | ||
'query' => [ | ||
'bool' => [ | ||
'filter' => [['term' => ['keyword' => 'value']]], | ||
], | ||
], | ||
], $body); | ||
|
||
$index = $payload[2]; | ||
$this->assertEquals(['index' => 'secondIndex'], $index); | ||
|
||
$body = $payload[3]; | ||
$this->assertEquals([ | ||
'query' => [ | ||
'bool' => [ | ||
'filter' => [['term' => ['keyword' => 'value']]], | ||
], | ||
], | ||
], $body); | ||
} | ||
} |