From e09f27521d870abf5cc70f2e7b84b1ef77ab109c Mon Sep 17 00:00:00 2001 From: Christopher Lorke Date: Mon, 24 Dec 2018 10:30:17 +0100 Subject: [PATCH] Added an integration test for a search. --- src/Contract/ElasticsearchDslContract.php | 8 ++- src/Dsl/AbstractDsl.php | 36 ++++++++++- src/Dsl/AbstractSearch.php | 23 +++++-- src/Dsl/Search.php | 14 ++-- src/ElasticsearchDsl.php | 13 +++- src/Facade/ElasticDsl.php | 2 +- src/Model/SearchResult.php | 2 +- tests/Integration/Dsl/SearchTest.php | 79 +++++++++++++++++++++++ tests/Integration/IntegrationTestCase.php | 33 ++++++++++ 9 files changed, 192 insertions(+), 18 deletions(-) create mode 100644 tests/Integration/Dsl/SearchTest.php create mode 100644 tests/Integration/IntegrationTestCase.php diff --git a/src/Contract/ElasticsearchDslContract.php b/src/Contract/ElasticsearchDslContract.php index a4a8d92..ba850df 100644 --- a/src/Contract/ElasticsearchDslContract.php +++ b/src/Contract/ElasticsearchDslContract.php @@ -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 diff --git a/src/Dsl/AbstractDsl.php b/src/Dsl/AbstractDsl.php index 3cd4ec9..1134a35 100644 --- a/src/Dsl/AbstractDsl.php +++ b/src/Dsl/AbstractDsl.php @@ -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 @@ -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 * @@ -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; diff --git a/src/Dsl/AbstractSearch.php b/src/Dsl/AbstractSearch.php index 20be5b0..6b8da61 100644 --- a/src/Dsl/AbstractSearch.php +++ b/src/Dsl/AbstractSearch.php @@ -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 { @@ -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 { @@ -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 { diff --git a/src/Dsl/Search.php b/src/Dsl/Search.php index 998d8c9..03a1076 100644 --- a/src/Dsl/Search.php +++ b/src/Dsl/Search.php @@ -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()); } /** @@ -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()); } /** @@ -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()); } /** @@ -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()); } /** @@ -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()); } /** @@ -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()); } /** @@ -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()); } } diff --git a/src/ElasticsearchDsl.php b/src/ElasticsearchDsl.php index f0a39e1..bccdff9 100644 --- a/src/ElasticsearchDsl.php +++ b/src/ElasticsearchDsl.php @@ -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 ]); } diff --git a/src/Facade/ElasticDsl.php b/src/Facade/ElasticDsl.php index be429b5..519c7e8 100644 --- a/src/Facade/ElasticDsl.php +++ b/src/Facade/ElasticDsl.php @@ -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 diff --git a/src/Model/SearchResult.php b/src/Model/SearchResult.php index 2f97361..727c59a 100644 --- a/src/Model/SearchResult.php +++ b/src/Model/SearchResult.php @@ -1,5 +1,5 @@ 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)); + } + } +} diff --git a/tests/Integration/IntegrationTestCase.php b/tests/Integration/IntegrationTestCase.php new file mode 100644 index 0000000..03f3f8e --- /dev/null +++ b/tests/Integration/IntegrationTestCase.php @@ -0,0 +1,33 @@ +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]); + } + } +}