Skip to content

Added an integration test for a search. #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Contract/ElasticsearchDslContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ public function getEsClient() : Client;
* Search
*
* @param OngrSearch|null $search
* @param string|null $esIndex
* @param string|null $esType
* @return Search
*/
public function search(?OngrSearch $search = null) : Search;
public function search(
?OngrSearch $search = null,
?string $esIndex = null,
?string $esType = null
): Search;

/**
* Suggestion
Expand Down
36 changes: 35 additions & 1 deletion src/Dsl/AbstractDsl.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Triadev\Es\Dsl\Dsl\Query\Specialized;
use Triadev\Es\Dsl\Dsl\Query\TermLevel;
use Triadev\Es\Dsl\Facade\ElasticDsl;
use Triadev\Es\Dsl\Model\SearchResult;

/**
* Class AbstractDsl
Expand Down Expand Up @@ -158,6 +159,35 @@ public function bool(\Closure $search) : AbstractDsl
return $this;
}

/**
* Get
*
* @return SearchResult
*/
public function get() : SearchResult
{
return new SearchResult($this->getRaw());
}

/**
* Get raw search result
*
* @return array
*/
public function getRaw() : array
{
$params = [
'index' => $this->getEsIndex(),
'body' => $this->toDsl()
];

if ($this->getEsType()) {
$params['type'] = $this->getEsType();
}

return ElasticDsl::getEsClient()->search($params);
}

/**
* Call
*
Expand All @@ -179,7 +209,11 @@ public function __call(string $name, array $arguments) : ?AbstractDsl
];

if (in_array($name, $validFunctions)) {
return ElasticDsl::search($this->search)->$name();
return ElasticDsl::search(
$this->search,
$this->getEsIndex(),
$this->getEsType()
)->$name();
}

return null;
Expand Down
23 changes: 19 additions & 4 deletions src/Dsl/AbstractSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\Search as OngrSearch;
use Triadev\Es\Dsl\Dsl\Query\Compound;
use Triadev\Es\Dsl\Dsl\Query\Fulltext;
use Triadev\Es\Dsl\Dsl\Query\Geo;
use Triadev\Es\Dsl\Dsl\Query\InnerHit;
use Triadev\Es\Dsl\Dsl\Query\Joining;
use Triadev\Es\Dsl\Dsl\Query\Specialized;
use Triadev\Es\Dsl\Dsl\Query\TermLevel;

abstract class AbstractSearch
{
Expand All @@ -18,17 +25,25 @@ abstract class AbstractSearch
/**
* AbstractDsl constructor.
* @param OngrSearch|null $search
* @param string|null $esIndex
* @param string|null $esType
*/
public function __construct(?OngrSearch $search = null)
{
public function __construct(
?OngrSearch $search = null,
?string $esIndex = null,
?string $esType = null
) {
$this->search = $search ?: new OngrSearch();

$this->esIndex = $esIndex;
$this->esType = $esType;
}

/**
* Overwrite the default elasticsearch index
*
* @param string $index
* @return AbstractSearch
* @return AbstractDsl|Search|TermLevel|Compound|Fulltext|Geo|InnerHit|Joining|Specialized
*/
public function esIndex(string $index) : AbstractSearch
{
Expand All @@ -50,7 +65,7 @@ public function getEsIndex() : string
* Overwrite the default elasticsearch type
*
* @param string $type
* @return AbstractSearch
* @return AbstractDsl|Search|TermLevel|Compound|Fulltext|Geo|InnerHit|Joining|Specialized
*/
public function esType(string $type) : AbstractSearch
{
Expand Down
14 changes: 7 additions & 7 deletions src/Dsl/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Search extends AbstractDsl
*/
public function termLevel() : TermLevel
{
return new TermLevel($this->getCurrentSearch());
return new TermLevel($this->getCurrentSearch(), $this->getEsIndex(), $this->getEsType());
}

/**
Expand All @@ -28,7 +28,7 @@ public function termLevel() : TermLevel
*/
public function fulltext() : Fulltext
{
return new Fulltext($this->search);
return new Fulltext($this->getCurrentSearch(), $this->getEsIndex(), $this->getEsType());
}

/**
Expand All @@ -38,7 +38,7 @@ public function fulltext() : Fulltext
*/
public function geo() : Geo
{
return new Geo($this->search);
return new Geo($this->getCurrentSearch(), $this->getEsIndex(), $this->getEsType());
}

/**
Expand All @@ -48,7 +48,7 @@ public function geo() : Geo
*/
public function compound() : Compound
{
return new Compound($this->search);
return new Compound($this->getCurrentSearch(), $this->getEsIndex(), $this->getEsType());
}

/**
Expand All @@ -58,7 +58,7 @@ public function compound() : Compound
*/
public function joining() : Joining
{
return new Joining($this->search);
return new Joining($this->getCurrentSearch(), $this->getEsIndex(), $this->getEsType());
}

/**
Expand All @@ -68,7 +68,7 @@ public function joining() : Joining
*/
public function specialized() : Specialized
{
return new Specialized($this->search);
return new Specialized($this->getCurrentSearch(), $this->getEsIndex(), $this->getEsType());
}

/**
Expand All @@ -78,6 +78,6 @@ public function specialized() : Specialized
*/
public function innerHit() : InnerHit
{
return new InnerHit($this->search);
return new InnerHit($this->getCurrentSearch(), $this->getEsIndex(), $this->getEsType());
}
}
13 changes: 10 additions & 3 deletions src/ElasticsearchDsl.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@ private function getElasticsearch() : ElasticsearchContract
* Search
*
* @param OngrSearch|null $search
* @param string|null $esIndex
* @param string|null $esType
* @return Search
*/
public function search(?OngrSearch $search = null): Search
{
public function search(
?OngrSearch $search = null,
?string $esIndex = null,
?string $esType = null
): Search {
return app()->makeWith(Search::class, [
'search' => $search
'search' => $search,
'esIndex' => $esIndex,
'esType' => $esType
]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Facade/ElasticDsl.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @package Triadev\Es\Dsl\Facade
*
* @method static Client getEsClient()
* @method static Search search(?OngrSearch $search = null)
* @method static Search search(?OngrSearch $search = null, ?string $esIndex = null, ?string $esType = null)
* @method static Suggestion suggest(?OngrSearch $search = null)
*/
class ElasticDsl extends Facade
Expand Down
2 changes: 1 addition & 1 deletion src/Model/SearchResult.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Triadev\Es\Dsl\Dsl;
namespace Triadev\Es\Dsl\Model;

use Illuminate\Support\Collection;

Expand Down
79 changes: 79 additions & 0 deletions tests/Integration/Dsl/SearchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
namespace Tests\Integration\Dsl;

use Tests\Integration\IntegrationTestCase;
use Triadev\Es\Dsl\Facade\ElasticDsl;

class SearchTest extends IntegrationTestCase
{
/**
* Setup the test environment.
*/
public function setUp()
{
parent::setUp();

$this->refreshElasticsearchMappings();

ElasticDsl::getEsClient()->indices()->putMapping([
'index' => 'phpunit',
'type' => 'test',
'body' => [
'properties' => [
'test' => [
'type' => 'keyword'
]
]
]
]);
}

private function buildPayload(array $payload = []) : array
{
return array_merge([
'index' => self::ELASTIC_INDEX,
'type' => self::ELASTIC_TYPE
], $payload);
}

private function createTestDocument()
{
ElasticDsl::getEsClient()->index($this->buildPayload([
'id' => 1,
'body' => [
'test' => 'phpunit'
]
]));

ElasticDsl::getEsClient()->indices()->refresh();
}

/**
* @test
*/
public function it_returns_an_elasticsearch_search_result_object()
{
$this->createTestDocument();

$result = ElasticDsl::search()
->esIndex(self::ELASTIC_INDEX)
->esType(self::ELASTIC_TYPE)
->termLevel()
->term('test', 'phpunit')
->get();

$this->assertIsInt($result->getTook());
$this->assertIsBool($result->isTimedOut());
$this->assertIsFloat($result->getMaxScore());

$this->assertEquals(5, $result->getShards()['total']);
$this->assertEquals(5, $result->getShards()['successful']);
$this->assertEquals(1, $result->getTotalHits());

$this->assertNotEmpty($result->getHits());

foreach ($result->getHits() as $hit) {
$this->assertTrue(is_array($hit));
}
}
}
33 changes: 33 additions & 0 deletions tests/Integration/IntegrationTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace Tests\Integration;

use Tests\TestCase;
use Triadev\Es\Dsl\Facade\ElasticDsl;

abstract class IntegrationTestCase extends TestCase
{
const ELASTIC_INDEX = 'phpunit';
const ELASTIC_TYPE = 'test';

/**
* Refresh elasticsearch mappings
*/
public function refreshElasticsearchMappings()
{
$this->deleteElasticsearchMappings();

ElasticDsl::getEsClient()->indices()->create([
'index' => self::ELASTIC_INDEX
]);
}

/**
* Delete elasticsearch mappings
*/
public function deleteElasticsearchMappings()
{
if (ElasticDsl::getEsClient()->indices()->exists(['index' => self::ELASTIC_INDEX])) {
ElasticDsl::getEsClient()->indices()->delete(['index' => self::ELASTIC_INDEX]);
}
}
}